Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move old file to another folder

Tags:

linux

shell

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 ?

like image 502
hudi Avatar asked Dec 16 '22 20:12

hudi


2 Answers

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 old

Example (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/ \;
like image 88
rkyser Avatar answered Jan 02 '23 21:01

rkyser


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/
like image 28
Stephane Rouberol Avatar answered Jan 02 '23 21:01

Stephane Rouberol