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.

  • 1
    The usual caveat is that Unix filesystems don't generally contain a record of when the file was created - only the times that the inode and the content were last modified are available. – Toby Speight Oct 6 '16 at 15:54

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 {} \;
  • 2
    What about this ? Does it work ? find -type f -mtime +5 -exec rm -f {} \; – user1058398 Nov 27 '13 at 13:56
  • 1
    @user1058398 That will delete files older than 5 days from now. – Valentin Bajrami Nov 27 '13 at 14:23
  • Does it also delete folder along with files? @val0x00ff – alper Jun 15 '17 at 6:47
  • 1
    @Avatar no because the type -f limits the action to the files only. – Valentin Bajrami Jun 15 '17 at 8:20

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.

find /path/to/directory/ -mtime +<number of days> -name '<file name>' -exec rm -rf {} \;


find /Netap_fileshare_backup/SQL/DB_backups/xeo/ -mtime +15 -name 'ORA_XEO*' -exec rm -rf {} \;

In this case it will remove all files that start whith "ORA_XEO" with more than 15 days.

  • Though better watch it with the rm -rf, the first command would remove everything contained in any directories that are older than those 15 days. (Also, why the parenthesis around -name?) – ilkkachu Oct 6 '16 at 14:01
  • You're right, we dont need to put "(" ")". – calafate Oct 6 '16 at 15:00
  • but if we want to add a condition it can be usefull. for example if we want to remove all files that ends with ".jar" or ".cp", and start with "ex". example.jar - it will be removed example.cp - it will be removed example.tar - it wont be removed – calafate Oct 6 '16 at 15:03
  • in this case we can use: find /path/to/directory/ -mtime +<number of days> \( -name '*.jar' -o -name '*.cp' \) -name 'ex*' -exec rm -rf {} \; – calafate Oct 6 '16 at 15:07
  • That will remove some files from the <number of days> rather than all files older than <number of days> unless you run it exactly at midnight and it finishes within a second. With GNU find you should use the -daystart modifier to coerce the -mtime value to midnight. – roaima Oct 6 '16 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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