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?

link|improve this question

71% accept rate
"... 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
feedback

2 Answers

up vote 5 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.

link|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
feedback

ls | grep regex | xargs rm

link|improve this answer
2  
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 at 20:38
feedback

Your Answer

 
or
required, but never shown

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