How to delete files on the command line with regular expressions?
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?
Solution 1:
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.
Solution 2:
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.
Solution 3:
ls | grep regex | xargs rm