linuxpeter.de
  Batch, Scripts, Tools & Tips > Linux scripts and tools  
 
Linux scripts and tools
Index Debian von Englisch auf Deutsch umstellen >>
Delete files by age (a given number of days) Little one-liner to clean up backup folders

The problem:

You have a running backup or logging, that creates the same type of files in a given timeframe.
In my example, it was a mysql-backup, that created ".sql.gz" files periodically.
Those files are written on tape, so there is no need to keep them longer than necessary.

The solution:

A little "one-liner", that can be added to a existing backup-script. In my case, I decided to run it in a separate script and all folders, where I have old files, that can be deleted.

The one-liner:

find /backup-folder -name '*.sql.gz' -mtime +5 -exec rm {} \;

If you want to run a test and verify the results first, you can run the first part without erasing files:

find /backup-folder -name '*.sql.gz' -mtime +5

How it works:

"find" creates a list of all files, that end on ".sql.gz" in the folder "/backup-folder", that are older than 5 days. The results are passed on to the "rm" command. The curly brackets ("{}") are a placeholder for these results.
It works like a loop, where every result of find, that fits the conditions, will be sent to rm, where it will be deleted.

Little hint for the time-preiod definition:

Numeric arguments(... for the -mtime) can be specified as

+n for greater than n,
-n for less than n,
n for exactly n.

So, you can change values to keep old files and only delete new ones ...

Hope it helps ...
 

 



Last updated by Bikerpete (admin)  on Nov 11 2008  at 8:50 PM
Index Debian von Englisch auf Deutsch umstellen >>


Info

Buy me a coffee ...


 
Top! Top!