is it possible to use rm to remove files and directories matching a pattern recursively without using other commands?
5 Answers
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
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.
- 24,865
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.
- 116
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.
- 1,010