3

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)

Drew
  • 29,895
  • 7
  • 74
  • 104
Kostas
  • 4,061
  • 1
  • 14
  • 32
  • For the record, it's a good idea to try `rm -f *~` instead of `rm *~` because if there are no backup files, `rm` will whine that it can't find `*~`. Using `-f` will make it not whine at you and exit with error. – Leo Izen Mar 17 '17 at 11:01

4 Answers4

8

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.)

Leo Izen
  • 4,165
  • 7
  • 37
  • 56
  • Why `-exec rm {} \;` instead of just `-delete`? – Randy Morris Mar 17 '17 at 11:53
  • 2
    Short answer: this doesn't remove directories and doesn't need `-type f`. Longer answer is that using `-delete` doesn't work with `-prune` because `-prune` implies `-depth`. It doesn't matter in this case but using `rm {}` makes it more extensible for later. Plus, it also works better with GNU Parallel should one choose to use that later. – Leo Izen Mar 17 '17 at 12:07
  • @RandyMorris also afaik -delete is a GNU (and BSD) extension that isn't in POSIX but I might be wrong about that. – Leo Izen Mar 17 '17 at 12:09
  • Thanks for the explanation. I'm pretty sure removing emacs backup files isn't a task that really warrants the concerns you laid out, but I appreciate the reasoning. – Randy Morris Mar 17 '17 at 12:18
  • You're right that in this case it doesn't matter, but I like find exec anyway. – Leo Izen Mar 17 '17 at 22:25
3

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

MukeshKoshyM
  • 514
  • 1
  • 8
  • 16
2

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.

Dan
  • 630
  • 5
  • 8
  • 1
    This won't work if there are backup files in `./` because you *also* have to protect `*~` from bash. You need to quote `*~` as well. – Leo Izen Mar 17 '17 at 10:59
0

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.

Community
  • 1
  • 1
jpkotta
  • 9,237
  • 3
  • 29
  • 34