How can I recursively delete all files of a specific extension in the current directory?
You don't even need to use rm
in this case if you are afraid. Use find
:
find . -name "*.bak" -type f -delete
But use it with precaution. Run first:
find . -name "*.bak" -type f
to see exactly which files you will remove.
Also, make sure that -delete
is the last argument in your command. If you put it before the -name *.bak argument
, it will delete everything.
See man find
and man rm
for more info and see also this related question on SE:
- How do I remove all .pyc files from a project?
find . -name "*.bak" -type f -print0 | xargs -0 /bin/rm -f
First run the command shopt -s globstar
. You can run that on the command line, and it'll have effect only in that shell window. You can put it in your .bashrc
, and then all newly started shells will pick it up. The effect of that command is to make **/
match files in the current directory and its subdirectories recursively (by default, **/
means the same thing as */
: only in the immediate subdirectories). Then:
rm **/*.bak
(or gvfs-trash **/*.bak
or what have you).