Reputation: 4186
I would like to delete all the emacs backup (~) files from subfolders.
I am aware that I can cd
in every single folder and delete them using rm *~
(e.g. for backup file test.cpp~).
How I can delete these files with one command, without cd'ing in every folder?
(I tried rm -r *~
and rm -rf *~
but they don't seem to work)
Upvotes: 5
Views: 12152
Reputation: 554
find /path/to/directory/ -type f -name '*filtercondition*' -delete
Above command will find the file recursively in the folder matching pattern and delete Files only
Upvotes: 4
Reputation: 9437
First, the best way to delete these files is to not create them (or rather, create them in a central place): https://stackoverflow.com/a/151946/245173
Here's a way to use find
from Emacs to gather a list of files and selectively do operations on them. M-x find-name-dired RET /path/to/files RET *~ RET
should get all of the backup files under /path/to/files/
and put them in a dired buffer. Then you can do normal dired things like mark files with m
, invert the selection with t
, and delete the selection with D
.
Upvotes: 0
Reputation: 4289
You can do this with find
and exec
. Here's an example that does what you want to do:
find -name '*~' -exec rm {} \;
Let's break it down how this works. The find
command will recurse through the directory it's executed from, and by default it will print out everything it finds. Using -name '*~'
tells us only to select entries whose name matches the regex *~
. We have to quote it because otherwise the shell might expand it for us. Using -exec rm {}
will execute rm
for each thing it finds, with {}
as a placeholder for the filename. (The final ;
is something required to tell find
that this is where the command ends. It's not really a big deal but it'll whine and do nothing if you don't use it. The \
is to escape it because ;
is a special shell character.)
Upvotes: 10
Reputation: 670
You would use find:
find ./ -name *~ -exec rm {} \;
This command will recursively list all files, that match the pattern given for the name. Then it'll execute the command provided for each one of them, substituting the curly braces with the filename.
The only tricky part is the semicolon, as that is closing the command, but it must be protected from bash, hence the backslash.
See https://linux.die.net/man/1/find for more options.
Upvotes: 2