Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I delete files in a directory that match a given regexp, or a similar solution, using a Mac terminal?

share|improve this question
by mac terminal you mean OS X? if so I'm confused why this is tagged Linux. – xenoterracide Aug 22 '10 at 15:52
removing filesystems tag and adding shell... filesystems should be more on a discussion about things like ext4, fat32, hfs, etc, and this seems to be asking about how to manipulate files on a shell. – xenoterracide Aug 22 '10 at 15:54
@xenoterracide Thanks for refining my question! – adardesign Aug 22 '10 at 17:19
removing terminal tag too.. think the 'terminal' tag should be reserved for questions regarding xterms, VT's and real connected terminals. – xenoterracide Aug 22 '10 at 17:44
up vote 10 down vote accepted

Use the find command.

Find all files (recursively) matching a regex: find . -type f -regex '/ex/'
Find all files (recursively) matching a regex and delete them: find . -type f -regex '/ex/' -exec rm {} \;

The brackets store the found pathname, and the backslash escapes the semicolon because it's passed to find; without escaping it, it would be consumed by the shell. If that went over your head, read the first two chapters of "Learning the Bash Shell".

Check the man pages for find for more options. There are a lot more ways to search.

share|improve this answer
nice answer, I just think this command will be easier to read rm $(find . -regex '/ex/') – phunehehe Aug 22 '10 at 16:27
use rm '{}' + the + is more xargs like behavior or something... really all I remember is that it's more efficient than doing \;. – xenoterracide Aug 22 '10 at 16:54
@phunehehe I think the shell expansion you're doing is weird... and I'd question it's efficiency... I'd also wonder if it has a argument limit... (which is one of the things xargs was written to work around) – xenoterracide Aug 22 '10 at 16:57
@phunehehe: The major problem with rm $(...) is that it will not work if any of the file names contains whitespace characters or globbing characters \\[*?, because the shell expands them after substituting the $(...). – Gilles Aug 22 '10 at 20:50
They added the -delete action to GNU find in October 2004, which wraps up the common "-exec rm {} \;" incantation. There's a section in the docs (gnu.org/software/findutils/manual/html_mono/…) where they walk through the reasons it's a better idea to use this than the old portable way, if you can get away with it. It answers the comment immediately above, having to do with whitespace, among many other concerns. Good reading. – Warren Young Aug 23 '10 at 3:53

Very similar to jorelli's answer. This is what I use:

find . -regex '/ex/' -print0 | xargs -0 rm

The -print0 and -0 arguments cause find to output a char 0 separated list and xargs to perform the rm command in each element of the list, so paths with spaces are not a problem.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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