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
Best Answer
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:-exec rm -f {} \;
(or, equivalently,-exec rm -f {} ';'
)This will run
rm -f
on each file; e.g.,-exec rm -f {} +
This will run
rm -f
on many files at once; e.g.,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.)-delete
This tells
find
itself to delete the files, without runningrm
. 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: