Cron to find multiple file types and automatically delete them
I am trying to put together a command that will find multiple file types and automatically delete them. I want to run this in a cron every 10 minutes on my server.
I would like to find files of type: .swp, .save, .swo, .maintenance and .bak
Here is my command:
find . -type f -name '*.swp' -o -type f -name '*.save' -o -type f -name '*.bak' -o -type f -name '*.swo' -o -type f -name '*.maintenance'
What would I add to this command to automatically delete these files?
I tried adding -delete at the end, but that doesn't get rid of them:
find . -type f -name '*.swp' -o -type f -name '*.save' -o -type f -name '*.bak' -o -type f -name '*.swo' -o -type f -name '*.maintenance' -delete
Solution 1:
find . -type f -name '*.swp' -o -type f -name '*.save' -o -type f -name '*.bak' -o -type f -name '*.swo' -o -type f -name '*.maintenance' -delete
The command is good but you have to specify a path. Using . means to search in the current directory. Depending on the user you're running the cronjob for, that directory can be different.
So I would use something like:
find /full-path -type f \( -name \*.bak -o -name \*.swp -o -name \*.save -o -name \*.maintenance \) -print -delete
-f search only file types (not folders)
-o logical OR operator
-name files containing that in their name (in this case the extension)
If you want to do case sensitive search you can use -iname instead of -name