Process files by timestamp

Sometime we need to delete or move files based on when they were created. Heres some snippets that do just that.

Find everything created in local directory in last 24 hours and copy them out

 

            touch -t 01162011 reference

                        create a reference file with mod date 16 Jan 2011

            find . -newer reference -exec cp {} ./my_sub_dir \;

                        copy all files older than reference to a sub directory

scp -r ./my_sub_dir kieran@linuxpc:/home/kieran/test_dir

            take all files saved to <my_sub_dir> and copies them to destination

 

Move everything created in last day to new directory

            find * -mtime -1 -exec mv {} my_sub_dir \;

                        this takes all files in * and moves them to directory my_sub_dir.

 

Delete everything older than x days

            find /opt/kieran/logs/* -type f -mtime +1 -exec rm -f {} \;

 

So whats the deal with the curly braces {}?

Each file that the find command finds, is placed in the {} array, so that they can then be manipulated one at a time by the exec command.

 

Heres some nice links with more detail:

http://www.google.com/url?q=http://kb.iu.edu/data/admm.html&sa=U&ei=WT69ToTREtPw8QPmg9SPBA&ved=0CBsQFjAC&usg=AFQjCNF5H3XFnIhnmwRRdqLflTsQh___aw

 

 

 

Leave a Reply