Delete files with regular expression

46

26

I Tried to delete files that starts with A and ends with 2 numbers but It doesn't do a thing.
What I tried:

rm ^A*[0..9]2$

Where am I wrong?

gdoron is supporting Monica

Posted 2012-02-22T18:07:35.050

Reputation: 580

Answers

58

You can use the following command to delete all files matching your criteria:

ls | grep -P "^A.*[0-9]{2}$" | xargs -d"\n" rm

How it works:

  1. ls lists all files (one by line since the result is piped).

  2. grep -P "^A.*[0-9]{2}$" filters the list of files and leaves only those that match the regular expression ^A.*[0-9]{2}$

    • .* indicates any number of occurrences of ., where . is a wildcard matching any character.

    • [0-9]{2} indicates exactly two occurrences of [0-9], that is, any digit.

  3. xargs -d"\n" rm executes rm line once for every line that is piped to it.

Where am I wrong?

For starters, rm doesn't accept a regular expression as an argument. Besides the wildcard *, every other character is treated literally.

Also, your regular expression is slightly off. For example, * means any occurrences of ... in a regular expression, so A* matches A, AA, etc. and even an empty string.

For more information, visit Regular-Expressions.info.

Dennis

Posted 2012-02-22T18:07:35.050

Reputation: 42 934

1

Parsing ls? See this question which points to this article. Because of the pitfalls you may rm what you don't want to.

– Kamil Maciorowski – 2016-11-15T11:57:42.107

@Frg Beware of newlines in file names. Or other strange characters. – 8bittree – 2016-11-16T19:43:27.850

this won't work if ls or grep aliased with --color=always option and you will need to do \ls | \grep ... – αғsнιη – 2018-10-12T04:42:45.347

This is not the most robust solution (spaces in filenames), especially considering find does all this in one command. – qwr – 2020-02-25T21:58:38.247

3Beware of spaces in file names. – slhck – 2012-02-22T18:27:59.500

1The -d"\n switch fixes the spaces problem. – Frg – 2012-02-22T19:14:43.050

1Note - some distros (like Mac OS) don't have a grep -P (Perl regex). grep -E may work in this case. – bluescrubbie – 2013-10-02T20:59:30.987

1I prefer using -I with xargs and always test with non-lethal commands first: xargs -d"\n" -I {} echo "{}" – jozxyqk – 2014-03-24T05:40:45.453

53

Or using find:

find your-directory/ -name 'A*[0-9][0-9]' -delete

This solution will deal with weird file names.

cYrus

Posted 2012-02-22T18:07:35.050

Reputation: 18 102

6This is a great solution. I prefer it because it is simpler and you can omit the -delete flag at the end first to see if your regex is correct before mass deleting your files. – JAMESSTONEco – 2015-04-14T21:31:39.100

1Furthermore you have more control on what you delete, for example adding -type f – Marco Sulla – 2016-06-08T08:12:21.130

Can this be used to delete files and folders? It does not work for non empty folders. – Alex – 2017-09-09T11:46:03.727

@Alex nope, the directory must be empty (it wasn't an OP requirement anyway), you can use the xargs approach with rm -f. – cYrus – 2017-09-09T13:54:48.630

also obligatory link to article about parsing ls – qwr – 2020-02-25T21:48:41.877

11

See the filename expansion section of the bash man page:

rm A*[0-9][0-9]

glenn jackman

Posted 2012-02-22T18:07:35.050

Reputation: 18 546

worked nicely for me. – Nishanth Matha – 2017-03-15T06:09:42.320

This was the simplest, yet complete answer to the question. – Janac Meena – 2017-10-17T17:22:06.930

Will this work if the folder has a lot of files? – Itay – 2019-09-03T18:05:06.157

2

The solution with regexp is 200 times better, even with that you can see which file will be deleted before using the command, cutting off the final pipe:

ls | grep -P "^A.*[0-9]{2}$"

Then if it's correct just use:

ls | grep -P "^A.*[0-9]{2}$" | xargs -d "\n" rm

This is 200 times better because if you work with Unix it's important to know how to use grep. It's very powerful if you know how to use it.

Salvatore

Posted 2012-02-22T18:07:35.050

Reputation: 29

1

This doesn't seem to add much beyond what Dennis's 4 year old answer already says.

– 8bittree – 2016-11-16T19:48:06.853

1"200 times" is a pretty specific. Lots of other commands are very powerful too, all you need to do is learn how to use them. – glenn jackman – 2017-03-15T11:22:14.943

1

This works on my mac:

rm $(ls | grep -e '^A*[0..9]2$')

Ray

Posted 2012-02-22T18:07:35.050

Reputation: 11

1

find command works with regexes as well.

Check which files are gonna to be deleted

find . -regex '^A.*[0-9]{2}$'

Delete files

find . -regex '^A.*[0-9]{2}$' -delete

Petr Javorik

Posted 2012-02-22T18:07:35.050

Reputation: 156