I have got one folder for log with 7 folders in it. Those seven folders too have subfolders in them and those subfolders have subfolders too. I want to delete all the files older than 15 days in all folders including subfolders without touching folder structrure, that means only files.

mahesh@inl00720:/var/dtpdev/tmp/ > ls
A1  A2  A3  A4  A5  A6  A7

mahesh@inl00720:/var/dtpdev/tmp/A1/ > ls
B1 B2 B3 B4 file1.txt file2.csv

5 Answers 5


You could start by saying find /var/dtpdev/tmp/ -type f -mtime +15. This will find all files older than 15 days and print their names. Optionally, you can specify -print at the end of the command, but that is the default action. It is advisable to run the above command first, to see what files are selected.

After you verify that the find command is listing the files that you want to delete (and no others), you can add an "action" to delete the files. The typical actions to do this are:

  1. -exec rm -f {} \; (or, equivalently, -exec rm -f {} ';')
    This will run rm -f on each file; e.g.,

    rm -f /var/dtpdev/tmp/A1/B1; rm -f /var/dtpdev/tmp/A1/B2; rm -f /var/dtpdev/tmp/A1/B3; …
  2. -exec rm -f {} +
    This will run rm -f on many files at once; e.g.,

    rm -f /var/dtpdev/tmp/A1/B1 /var/dtpdev/tmp/A1/B2 /var/dtpdev/tmp/A1/B3 …

    so it may be slightly faster than option 1.  (It may need to run rm -f a few times if you have thousands of files.)

  3. -delete
    This tells find itself to delete the files, without running rm. This may be infinitesimally faster than the -exec variants, but it will not work on all systems.

So, if you use option 2, the whole command would be:

find /var/dtpdev/tmp/ -type f -mtime +15 -exec rm -f {} +
  • Using -delete is simpler (syntax and no child processes spawned to do delete) but may not work on all systems. rm -f {} + is best, it will delete files in batches if there are enough files to be too much for one command-line. rm -f {} ';' will give command too long error if there are enough files to be too much for one command-line.
    – gaoithe
    Mar 22, 2017 at 12:21
  • How does the -exec rm -f {} + version handle files with spaces in the name? Could an attacker craft a filename that would cause this to delete something you didn't want deleted?
    – pileofrogs
    Jun 25, 2020 at 16:07
  • 2
    @pileofrogs It passes the filenames directly on as arguments to rm. So it's completely safe no matter what characters the filenames contain.
    – geirha
    Sep 9, 2020 at 8:36

Another option could be to pipe the output from find command and use xargs to delete those:

find /var/dtpdev/tmp/ -type f -mtime +15 | xargs rm

But the upvoted answer from Jan still more valid than this one.

  • To list and verify first -ls: find /var/dtpdev/tmp/ -type f -mtime +15 -ls To delete -delete: find /var/dtpdev/tmp/ -type f -mtime +15 -delete Oct 30, 2023 at 3:42


find /path/to/files/  -cmin +240  -type f -name *.php  -delete


find /path/to/files/ -type f -name *.php  -mtime +30 -exec rm {} \;

The first will delete files 4 hours old or older using the built-in delete function of find; and the second will delete files 30 days old or older using an rm within an -exec clause.

  • 1
    1. The OP asked "I want to delete all the files older than 15 days", which your answer doesn't provide. They also did not ask for a restriction to php files. 2. Please use the {} formatting tool to format code as code. 3. If that is your blogspot page, please indicate ownership explicitly, instead of the generic "below link", otherwise you become suspect of simply spamming your site.
    – Jeff Schaller
    Aug 8, 2020 at 21:03

Or do it with a loop:

for x in $(find /var/dtpdev/tmp/ -type f -mtime +15); do rm "$x"; done
  • 2
    This will not work with files with spaces in them.
    – Drav Sloan
    Aug 14, 2015 at 12:49
  • You can use a while loop instead which should take care of files with spaces. find /var/dtpdev/tmp/ -type f -mtime +15 | while read x; do rm "$x"; done Jun 30, 2016 at 7:39

I like

rm -rf `find yadi` 

for its simplicity. Those back ticks say "execute me first". So you are running rm -rf against the results of whatever is between them.

  • 4
    What is yadi? Aug 12, 2022 at 17:17
  • 1
    doesn't work with irregular filenames like containing spaces etc Sep 25, 2023 at 7:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .