Linux Command Line – How to Delete Files with Regular Expressions

bashlinuxregexshellzsh

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?

Best Answer

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.

Related Question