Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

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 {} \;
share|improve this answer
    
What about this ? Does it work ? find -type f -mtime +5 -exec rm -f {} \; –  user1058398 Nov 27 '13 at 13:56
    
@user1058398 That will delete files older than 5 days from now. –  val0x00ff Nov 27 '13 at 14:23

With GNU or some BSD finds:

find . ! -newermt 2013-11-22 ! -type d -delete

Note that it checks the last modification time of the files. On some BSDs, you can use -newerBt in place of -newermt to check the file's inode birth time if available instead.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.