Using find and mv syntax


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using find and mv syntax
# 1  
Old 04-03-2018
Using find and mv syntax

I have the following script working currently. The only thing I want to add is to somehow tell the script to find the files using the find and maxdepth variables and then move the files to a corresponding file structure.

example:
file test.txt exists in the /data/user1/upload directory
The script moves the file test.txt to a new filename (appends date/time)
The script also copies the file to /test/archive directory
I want the script to then move the file to the --parents /test directory

Something like:
Code:
find . -maxdepth 7 -type f ! -name "*.*" | xargs mv -p --parents -t /test \+

Am I on the right track?

Or can I add a line outside of the script to the .sh and have it do this after?


Working script:

Code:
thetime=`date +%Y-%m-%d_%H:%M:%S` #
for i in *.*
do
    [[ "$i" =~ \.filepart$ ]] && continue
        extn=${i##*.} # save the extension of the file
                mv "$i" "${i%.*}"$(date "+_%Y-%m-%d_%H:%M:%S.${extn}")
        cp -p "$i" "${i%.*}"$(date "+_%Y-%m-%d_%H:%M:%S.${extn}") /test/archive
done


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 04-03-2018 at 10:40 AM.. Reason: Added CODE tags.
# 2  
Old 04-03-2018
Welcome to the forum.

Some comments:
  • not sure I understand your find ... | xargs syntax. You seem to exclude files with dots in their names? And, the -p and --parent options to the mv command are unknown to me, as is the meaning of the \+ at the end.
  • what be the meaning of defining the thetime variable upfront if you then don't use it (running the risk of cping / mving to two different file names)?
  • Are you aware that your cp operation has no source file to work upon?
  • what be the sense of extracting the "extension" if - with your findcommand above - you operate on non-dotted files only?
# 3  
Old 04-03-2018
This script looks at all files in a directory. If the extension has .filepart (partial upload) it skips that file. It appends the date/time to the original filename while maintaining the original extension.

Is there a command I can run that will search all subdirectories for any files and then move the files it finds to a new root location and maintain the same directory tree as the original path?

Example:

/data/user1/upload/test.txt

new location

/test/user1/upload/test.txt


something like find and then move
Thanks for the quick reply.

Last edited by jwt66; 04-03-2018 at 11:29 AM..
# 4  
Old 04-04-2018
You should have a look at the rsync command, these paramters are of particular interest to you is:

-a recurse sub-folders and preserve permissions/owners
--backup to backup existing files in destination folder before they are replaced with a new version
--suffix to use date as the backup suffix
--exclude=PATTERN to exclude .filepart files
--remove-source-files to remove the source files after copying
-v verbose output
--dry-run just list what will be done without doing anything.

Here is a command the should be close to what you are after:

Code:
rsync -a -v --dry-run --exclude="*.filepart" --remove-source-files --backup --suffix _${thetime} /data /test

Remove --dry-run if you are happy with what will happen.

Note that --remove-source-files will only remove files and does not remove any directories so it can leave you with a number of empty directories in the source after it's done.

Only files that are different in source and destination (eg timestamp or contents) are copied, but --remove-source-files will still clean them up.

Files will only be renamed by --backup if they already exist in destination folder (i.e the 2nd time they are copied).

Remember to make a backup of your files before playing with this script!
# 5  
Old 04-04-2018
Perfect! Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

FIND and GREP syntax

I have a question to this command find . -type f -name ".*txt" -exec grep "text" {}\. The find command will locate a file name with the extension of txt once per round and find the word "text" in the content of the file or the find command will locate all the file names with the extension of... (2 Replies)
Discussion started by: TestKing
2 Replies

2. Shell Programming and Scripting

Help with decoding the 'find' command syntax

Hi folks, I just want to understand what this find command does. The syntax "- - -" is cumbersome and unusual. Please let me know what it is for? find - - - share/ ../share/ -type f ! -path '*/CVS*' (8 Replies)
Discussion started by: royalibrahim
8 Replies

3. UNIX for Dummies Questions & Answers

uanble to find while and for loop syntax in unix

Hi friends, I am unable to find for loop or while loop syntax on ksh shell,same syntax is working in other shell ,please suggest a way through. error :syntax error at line 2: `(' unexpected syntax used : for ((i=1;i<=$v1;i++)) thanks, Jcpratap (9 Replies)
Discussion started by: Jcpratap
9 Replies

4. UNIX for Dummies Questions & Answers

Find/Grep Syntax Question

Hi Folks, I am trying to dig through about 100 directories that have 1 or 2 .jpg images stored in each. I want to copy the .jpg to another file in the root directory. Really my ultimate goal is not to have to dig down into each directory to copy the images individually. I thought I could use a... (2 Replies)
Discussion started by: alpinescott
2 Replies

5. UNIX for Advanced & Expert Users

Help with complex find syntax

I need to modify the find command below to exclude the output of the directory /usr/UDPM/PerfMgmt/shmlck find / \( -fstype ctfs -o -fstype mntfs -o -fstype objfs -o -fstype proc -o ! local \) -prune -o -type f -perm -0002 -print 2>/dev/null I have tried many iterations and placement of... (2 Replies)
Discussion started by: interesting?
2 Replies

6. Shell Programming and Scripting

find syntax

i'm trying to find out how to find in my directory and subdirectories a certain file. once i find that file, i need to select that folder and copy it the location or output it to a new text file. i know how to find the file by using find . -name \*.pl | sort -n but idk how to select... (9 Replies)
Discussion started by: kpddong
9 Replies

7. UNIX for Dummies Questions & Answers

find mtime syntax

Hi guys, I am looking for a way of moving all files out of a directory with a time stamp greater then the one I specify. Can anyone suggest a way of doing so? For example, move all files out of dir1 which were created after 17:00 into dir2. Thanks :) (1 Reply)
Discussion started by: JayC89
1 Replies

8. Shell Programming and Scripting

syntax error not able to find why

Hi Guru's here is part of my shell script where the error is coming. function daemon_start { daemon="" clear :<<COMMENT Meenakshi 25-Jun Code Cleanup IA - code comented - Start echo " `date` ... (1 Reply)
Discussion started by: namishtiwari
1 Replies

9. Shell Programming and Scripting

Can I know find syntax to find given date files

Hi All, Can i use find command to know given date files? If yes, then please let me know the syntax for the same. Thanks in advance for your postive responses Regards, Bachegowda (3 Replies)
Discussion started by: bache_gowda
3 Replies

10. UNIX for Dummies Questions & Answers

Question: non-recursive find syntax

Hello, I am trying to search a directory for all files matching "G*" without looking in sub-directories "success" and "error". I've searched this forum and found the following syntax, but can't make it work: find . \( ! -name success -prune -name error -prune \) -type f -name "G*" Have... (6 Replies)
Discussion started by: alexkav
6 Replies
Login or Register to Ask a Question