17

is it possible to use rm to remove files and directories matching a pattern recursively without using other commands?

warren
  • 10,322
user15586
  • 657

5 Answers5

35

To directly answer your question, "no - you can't do what you describe with rm".

You can, however, do it you combine it with find. Here's one of many ways you could do that:

 # search for everything in this tree, search for the file pattern, pipe to rm
 find . | grep <pattern> | xargs rm

For example, if you want to nuke all *~ files, you could so this:

 # the $ anchors the grep search to the last character on the line
 find . -type f | grep '~'$ | xargs rm

To expand from a comment*:

 # this will handle spaces of funky characters in file names
 find -type f -name '*~' -print0 | xargs -0 rm
warren
  • 10,322
8

"without using other commands"

No.

Drakia
  • 655
3

Using Bash, with globstar set, yes:

rm basedir/**/my*pattern*

Try it with e.g. ls -1 first, before rm to list the files you match.

You set options through e.g. shopt -s globstar.


Alternatively, a shorter find variant:

find -type f -name 'my*pattern*' -delete

or for GNU find:

find -type f -name 'my*pattern*' -exec rm {} +

or another alternative for non-GNU find (a bit slower):

find -type f -name 'my*pattern*' -exec rm {} \;

To also remove directories, as you ask for: just change rm into rm -r in the above commands and skip matching on only -type f in the find commands.

0

If you use zsh(1), turn on "extended globbing" with setopt extendedglob in .zshrc. Prefixing the pattern with '**/' will then delete recursively:

% rm -rf **/< pattern >

However, if there are a lot of files to delete you should resort to find(1) with xargs(1) or -exec, and I also recommend doing that in shell scripts.

kaleissin
  • 116
0

I would have asuumed " rm -rf " where is a combination of file names, and matching patterns such as * and ? etc (eg todays_log_2009????.log) . That will start from current Dir and work down recursively removing files that macth that pattern.

ianfuture
  • 1,010