Delete files older than 3 years

I need to delete any file in a directory older than 2 years. It is very important that I keep the latest files and delete old files.

I searched and found this.

find /path/to/files* -mtime +365 -exec rm {} \; 

Is it possible to simply multiply a number?

 find /path/to/files* -mtime +1095 -exec rm {} \; 

Is there a way to add a switch that will print the file name on the screen when it removes it? To make sure he does what I expect?

I also found this:

 find /rec -mtime +365 -print0 | xargs -0 rm -f 

Is there a difference between the two? Better than the other? What I read says xargs is faster. Can I multiply mtime by the second or third year?

And finally, can I put the code as it is in the cron job, which can work daily?

Thanks!

+5
source share
3 answers

Is it possible to simply multiply a number?

 find /path/to/files -mtime +1095 -exec rm {} \; 

Yes. And for the echo before deleting

 find /path/to/files -mtime +1095 -print 

Then the version with -exec rm {} \; will delete files (when you are ready).

+4
source

To answer the second part of your question.

Yes, there is a big difference in using -exec or xargs. -exec starts a new rm process for each file found. This creates a lot of overhead and can seriously slow down systems if you delete a large number of files.

xargs only creates as many rm processes as necessary, since it creates a command line containing as many files as possible. Thus, only a few rm processes are created.

But both are better than -delete, because delete is unsave

+1
source
 find /path/to/files* -mtime +1095 -exec rm {} \; 

This should work fine, you can run this run by simply specifying the files found by the command:

 find /path/to/files* -mtime +1095 -exec ls {} \; 

To be safe, although I would also add in -type style so that other things are not deleted:

 find /path/to/files* -type f -mtime +1095 -exec rm {} \; 
0
source

Source: https://habr.com/ru/post/1213436/


All Articles