Super User is a question and answer site for computer enthusiasts and power users. 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

Lets say I have 20 files named FOOXX, where XX is the number of the file, eg 01, 02 etc.

At the moment, if I want to delete all files lower than the number 10, this is easy and I just use a wildcard, eg rm FOO0*

However, if I want to delete specific files ina range, eg 13-15, this becomes more difficult.

rm FPP[13-15] does not work, and asks me if I wish to delete all files. Likewse rm FOO1[3-5] wishes to delete all files that begin with FOO1

So, what is the best way to delete ranges of files like this?

I have tried with both bash and zsh, and I don't think they differ so much for such a basic task?

share|improve this question
"... rm FOO1[3-5] wishes to delete all files that begin with FOO1" This makes no sense, and certainly isn't the case here. – Ignacio Vazquez-Abrams May 6 '10 at 12:23
@Ignacio yeah- I'd like to see the character set has that collation order! – kmarsh May 6 '10 at 12:52
up vote 24 down vote accepted

In bash you can use:

rm FOO1{3..5}


rm FOO1{3,4,5}

to delete FOO13, FOO14 and FOO15.

Bash expansions brace are documented here.

share|improve this answer
Or even rm FOO{13..15}. – Ignacio Vazquez-Abrams May 6 '10 at 12:24
Is this also true for ZSH? – Jack May 6 '10 at 12:51
@Jack: Yes, it is. – Dennis Williamson May 6 '10 at 13:07
@Jack: bash commands form subset of z shell commands. – abhinavkulkarni Aug 14 '13 at 0:36
So I need to learn both regex and globbing syntax. Boohoo. :) – Aditya M P Oct 25 '13 at 11:00

For future readers, the find command can also delete files. I settled on this for a similar problem:

find . -type f -regex '...' -delete

but the chosen answer is the simplest anser to this question.

share|improve this answer

ls | grep regex | xargs rm

share|improve this answer
You should use find -regex ... -print0 | xargs -0 ... for this, otherwise it fails for filenames with spaces. – Dennis Williamson May 6 '10 at 13:02
Of course, if you're going to use find then you may as well just use -exec. – Ignacio Vazquez-Abrams May 6 '10 at 13:31
In my case the files had spaces, and changing the delimiter fixes the spaces issue: ls | grep regex | xargs --delimiter='\n' rm – Anake Apr 1 '12 at 20:38
-1 for attempting to parse ls – evilsoup May 24 '13 at 21:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .