How to delete files on the command line with regular expressions?

19

11

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?

Jack

Posted 2010-05-06T12:00:17.113

Reputation: 1 621

"... 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 – 2010-05-06T12:23:46.963

@Ignacio yeah- I'd like to see the character set has that collation order! – kmarsh – 2010-05-06T12:52:24.373

Answers

32

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.

Mike Fitzpatrick

Posted 2010-05-06T12:00:17.113

Reputation: 15 062

2Or even rm FOO{13..15}. – Ignacio Vazquez-Abrams – 2010-05-06T12:24:20.423

Is this also true for ZSH? – Jack – 2010-05-06T12:51:13.703

@Jack: Yes, it is. – Paused until further notice. – 2010-05-06T13:07:41.540

@Jack: bash commands form subset of z shell commands. – abhinavkulkarni – 2013-08-14T00:36:01.893

So I need to learn both regex and globbing syntax. Boohoo. :) – Aditya M P – 2013-10-25T11:00:51.130

13

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.

GregWW

Posted 2010-05-06T12:00:17.113

Reputation: 131

The chosen answer is only good for files that follow a naming convention though. Thanks to your answer, I could just delete all object files in a directory. (in my case I just went with find . -name "*.o" -delete and it worked like a charm) – John Hamilton – 2018-02-24T11:20:30.647

6

ls | grep regex | xargs rm

gogiel

Posted 2010-05-06T12:00:17.113

Reputation: 300

6You should use find -regex ... -print0 | xargs -0 ... for this, otherwise it fails for filenames with spaces. – Paused until further notice. – 2010-05-06T13:02:24.227

1Of course, if you're going to use find then you may as well just use -exec. – Ignacio Vazquez-Abrams – 2010-05-06T13:31:07.480

@evilsoup, +1 for adding that warning, but, who inserts a new line in a file name? – Iulian Onofrei – 2017-02-22T15:20:41.540

@DennisWilliamson, Shouldn't it be find . -regex ... -print0 | xargs -0 ... instead? – Iulian Onofrei – 2017-02-22T15:23:27.660

@IgnacioVazquez-Abrams, How? – Iulian Onofrei – 2017-02-22T15:47:33.033

@IulianOnofreiIn GNU find the current directory is implied. You can omit the . – Paused until further notice. – 2017-02-28T00:29:59.890

@DennisWilliamson So the problem is only on mac, where it throws the error find: illegal option -- r. – Iulian Onofrei – 2017-03-02T14:02:50.960

In my case the files had spaces, and changing the delimiter fixes the spaces issue: ls | grep regex | xargs --delimiter='\n' rm – Anake – 2012-04-01T20:38:42.927

1

-1 for attempting to parse ls

– evilsoup – 2013-05-24T21:18:23.340