in my folder I have file which suffix is date when this file was created for example:
file.log2012-08-21, file.log2012-08-20 ... Now how I can move file which is older then 2012-08-20 ? I know how I can do this day by day: mv file.log2012-08-19 /old/
but I dont know when to stop ... is there some parameter in command mv to do this easier ?
You could use find
with the -mtime
parameter. This assumes that the file suffix as mentioned above matches the datestamp on the file.
-mtime +1
means find files more than 1 day old-mtime -1
means find files less than 1 day old-mtime 1
means find files 1 day oldExample (updated):
find . -type f -mtime +1 -name "file.log*" -exec mv {} /old/ \;
or if you only want to find
in the current directory only add -maxdepth 1
(otherwise, it will search recursively):
find . -maxdepth 1 -type f -mtime +1 -name "file.log*" -exec mv {} /old/ \;
Assuming your log files are not modified after their last line is written:
find . ! -newer file.log2012-08-20 | xargs -r -IX mv X /old/
Note: with this command file.log2012-08-20 would be moved as well. If you don't want it, use the previous file:
find . ! -newer file.log2012-08-19 | xargs -r -IX mv X /old/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With