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.
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89e4e8e1ecfae1c9e0e7e5b9b9bebbb9">[email protected]</a>:/var/dtpdev/tmp/ > ls A1 A2 A3 A4 A5 A6 A7 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e030f060b1d062e0700025e5e595c5e">[email protected]</a>:/var/dtpdev/tmp/A1/ > ls B1 B2 B3 B4 file1.txt file2.csv
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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 runrm -fon 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; …
-
-exec rm -f {} +
This will runrm -fon 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 runrm -fa few times if you have thousands of files.) -delete
This tellsfinditself to delete the files, without runningrm.
This may be infinitesimally faster than the-execvariants,
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 {} +
Method 2
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.
Method 3
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.
Method 4
Or do it with a loop:
for x in $(find /var/dtpdev/tmp/ -type f -mtime +15); do rm "$x"; done
Method 5
Try:
find /path/to/files/ -cmin +240 -type f -name *.php -delete
OR
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0