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.,
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 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.)
-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 {} +
-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 '20 at 16:07rm
. So it's completely safe no matter what characters the filenames contain. – geirha Sep 09 '20 at 08:36