Shell – Remove all files created before a certain date

filesfindkshshell

I have a directory containing a high number of files (like logs for every day of the year).
I would like to remove all files created before let's say 22/11. How can I achieve that ? Must I use find then exec -rm? I'm using ksh.

Best Answer

Using find is still the preferred way of deleting files. See http://mywiki.wooledge.org/UsingFind for more.

One way of doing this is to create a file with the time-stamp in it. e.g

touch -t 201311220000 /tmp/timestamp

Now delete the files GNUfind (assuming in the current directory) that match the time-stamp e.g:

find . -type f ! -newer /tmp/timestamp -delete  

or non GNU find

find . -type f ! -newer /tmp/timestamp -exec rm {} \;
Related Question