Moving files based on file creation

Hi,

I have a directory having so many number of files. Now I want to move the files which are older than one month (lets say) from this directory to another directory (say BKP dir).

Simply, if file is olderthan one month move it from source1 dir to BKP1 dir.

My file names doesn't have any time stamps in their names, so we have to move based on the file creation date.

System: AIX - Korn shell.

find /path/to/source1 -type f -mtime +30 -exec mv {} /path/to/BKP1 \;

Try that

BTW if you don't want to move files in sub-directories, then use -prune option:-

find . -mtime +30 ! -name . -prune -type f -exec mv {} /path/to/BKP1/ \;

Will these two work, if I have spaces in between the file name ?
Eg:

2012 cust Info.txt

Not as given

find /path/to/source1 -type f -mtime +30 -exec mv "{}" /path/to/BKP1 \;

Note the change from {} to "{}"

1 Like

Thanks for your reply, adding one constraint to this. Please don't mind.

I have one text file (say dont_move.txt), where some file names are written into that in a line by line manner. So now I need to perform the above code if the name is not in the file (dont_move.txt).

If a file is olderthan 30 days and that name is entered into dont_move.txt, then we need to skip that.

Eg: dont_move.txt

cust data.txt
2012 cust Info.Z
2012_abc.dat
2011_abc.txt

Thanks,

Try

find /path/to/source1 -type f -mtime +30 | grep -vf dont_move.txt| xargs -I{} echo mv -t /path/to/BKP1 \"{}\"

As your file names contain spaces, you will need to fiddle around with the escaped double quotes around the final {}. Remove echo as you see command fit your needs.

The double quotes in this find command's -exec primary make absolutely no difference. The commands:

find /path/to/source1 -type f -mtime +30 -exec mv "{}" /path/to/BKP1 \;

and:

find /path/to/source1 -type f -mtime +30 -exec mv {} /path/to/BKP1 \;

and:

find /path/to/source1 -type f -mtime +30 -exec mv {} /path/to/BKP1 +

will all produce the same results (but the last one will run faster since mv will be executed less often to move the files). This works for any files on the system even if the filenames contain spaces, tabs, and newlines.

However, if you use something like:

find /path/to/source1 -type f -mtime +30 -print | xargs -I {} mv {} /path/to/BKP1

it won't work if any filenames contain a newline. On some systems you can get around this using -print0 in find instead of -print and then use a special option in the program reading filenames from the piped output of find, but these options are not in the standards (so they are not portable).