0

I would like to rm all files that end in ~ in both my current directory and all directories inside my current directory. I was under the impression that the flag -r or -R would do this; however, the following commands only remove files ending in ~ in my current directory and not the other directories inside my current directory:

rm *~ -r
rm *~ -R

Is it even possible to get rm to perform as I want it to as explained here and, if so, how?

I use tcsh.

migrated from stackoverflow.com May 6 '14 at 4:00

This question came from our site for professional and enthusiast programmers.

  • 1
    FYI, what you're trying doesn't work because rm doesn't expand patterns like *~ to lists of filenames itself; the shell is responsible for doing that before rm is ever started. For that reason, arguments to rm like -r can't change how that process works. – Charles Duffy May 5 '14 at 19:58
  • 1
    ...as another aside, options (like -r) should go before positional arguments (like the filenames given here). Some tools support doing it the other way around, and others don't; assuming that no more option parsing is done after the first positional argument is the safe and compatible approach. – Charles Duffy May 5 '14 at 23:26
4

The compatible approach is to tell find to construct a file list and pass it to rm. Using the newish (added to POSIX around 2005, if memory serves) -exec ... {} + syntax will make this both efficient and safe:

find . -name '*~' -exec rm -f {} +

For newer (4.x) bash, or most releases of zsh, you can also tell the shell to do recursive globbing itself:

shopt -s globstar # enable recursive globbing with ** on bash
rm -f **/*~
  • Don't you need a slash at the end of find? – Brian May 5 '14 at 19:54
  • @staticx, ...perhaps you're thinking of {} \;? I'm using +, not ;, and it's specifically the semicolon character which requires escaping. (More pertinantly, the semicolon character also selects a less-efficient mode of operation, invoking rm once per file found). – Charles Duffy May 5 '14 at 19:55
  • Yes, that is what I am thinking about.. sorry. – Brian May 5 '14 at 19:55
  • Interesting, I just read in the man pages about the differences. I guess old habits die hard. It also looks like -execdir is the more secure way to run the exec command. – Brian May 5 '14 at 19:59
  • @staticx, -execdir is a GNU extension, not a POSIX-standardized option. – Charles Duffy May 5 '14 at 20:00
0

You can remove all files from every directory using below command:

find -name "*~" -print0 | xargs -0 rm

where find -name "*~" finds every file which is having name with "~" at last position, -print0 list down all of file in one line, xargs -0 rm which takes list of all file in input and works as rm . And if you want to even delete every directory which is having name with "~" at the end then also you can do it by replacing rm with rm -rf.

  • This works fine, but has no advantages over the -exec rm {} + approach other than compatibility with older find implementations. – Charles Duffy May 6 '14 at 11:54
  • That's the point. – ravibhuva9955 May 6 '14 at 11:55
  • Funny that you'd have someone with the xargs -0 GNUism, but not find -exec {} + (which GNU added long before the standard required it). – Charles Duffy May 6 '14 at 12:09
  • ...see pubs.opengroup.org/onlinepubs/009695399/utilities/xargs.html if you have questions on that count. – Charles Duffy May 6 '14 at 12:11
  • i don't have any question. It's for fun only. :) – ravibhuva9955 May 6 '14 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.