Friday, 6 June 2014

Delete files older then ... EXPLAINED how to do it in Linux

Lets imagine that you have folder that contains several hundreds, thousands files and from time to time you will have need to delete files older that some time.

For example: you want do delete files older that 30 days.

Steps are:

server#cd location

User find and option -mtime.
 

server#find . -mtime +30

this will list files older that 30 days. If you want to list files younger than 30 days use -mtime -30 option.

I like to use for loops.

server#for i in `find . -mtime +30`;do echo $i;rm -rf $i;done

And that is it!

Many people use this

find . OPTIONS -exec rm -rf {} \;

but I like to see what I am deleting! :)

No comments:

Post a Comment