Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

3 Answers 3

up vote 14 down vote accepted

In bash you can use:

rm FOO1{3..5}

or

rm FOO1{3,4,5}

to delete FOO13, FOO14 and FOO15.

Bash expansions brace are documented here.

share|improve this answer
1  
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 menon Oct 25 '13 at 11:00

ls | grep regex | xargs rm

share|improve this answer
4  
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
1  
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

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

Your Answer

 
discard

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.