Bash script to delete files older than x days with subdirectories (6 Solutions!!)
Channel: Roel Van de Paar
Date: 19/08/2020

Bash script to delete files older than x days with subdirectories


The Question: I'm trying to delete a ton of files older than x days.
Now I have a script to do that
find /path/to/files* -mtime +10 -exec rm {} \;
But this will also delete the subdirectories. There are a ton of folders but I
would like to keep them, and delete the files older than 10 days within the
said folders.
Is there a way to do this?

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

== This solution helped 1 person ==
You can easily do this with the find command
$ find -type f
Which restricts the results to be of the type file

== This solution helped 50 people ==
**** type option for filtering results ****
find accepts the type option for selecting, for example, only files.
find /path/to/files -type f -mtime +10 -delete
Leave out -delete to show what it'd delete, and once you've verified that, go
ahead and run the full command.
That would only run on files, not directories. Use -type d for the inverse,
only listing directories that match your arguments.
----
**** Additional options ****
You might want to read man find, as there are some more options you could need
in the future. For example, -maxdepth would allow you to only restrict the
found items to a specific depth, e.g. -maxdepth 0 would not recurse into
subdirectories.
**** Some remarks ****
 * I wonder how the command would have removed a folder, since you can't
 remove a folder with rm only. You'd need rm -r for that.
 * Also, /path/to/files* is confusing. Did you mean /path/to/files/ or are
 you expecting the wildcard to expand to several file and folder names?
 * Put the {} in single quotes, i.e. '{}' to avoid the substituted file/
 directory name to be interpreted by the shell, just like we protect the
 semicolon with a backslash.

== This solution helped 5 people ==
As in previous answers (+1 for both) the trick is to use -type f predicate.
Note, that instead of -exec rm '{}' you can also use -delete predicate. But
don't do that. With -exec rm '{}' you can (and should) first do -exec echo rm '
{}' to verify that this is really what do you want. After that rerun the
command without the echo.
Using -delete is faster (no extra fork() and execve() for each file), but this
is risky because -delete works also as a condition, so:
# delete *.tmp files
find . -type f -name '*.tmp' -delete
but if you ONLY swap arguments:
# delete ALL files
find . -type f -name '*.tmp' -delete
If you ever need find and rm to work faster for tons of files, check out the
find ... | xargs ... rm UNIX idiom.

To speed up the command use '+' instead of ;
find /path/to/files* -type f -mtime +10 -exec rm '{}' '+'
This will run rm only once at the end instead of each time a file is found.
(rm may be run several times if your have really tons of files because of the
command line length limitation, but this is still almost as fast as using -
delete)

With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to user user52573 (https://superuser.com/users/52573), user slhck (https://superuser.com/users/48078), user olibre (https://superuser.com/users/112297), user Nikhil Shinde (https://superuser.com/users/373702), user Michal Srajer (https://superuser.com/users/76747), user JoyIan Yee-Hernandez (https://superuser.com/users/112318), user Jens Erat (https://superuser.com/users/102155), user Bernhard (https://superuser.com/users/112255), and the Stack Exchange Network (http://superuser.com/questions/375011). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.