87

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
  • 990
  • 1
  • 7
  • 10

6 Answers6

101

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
  • 48,917
  • 12
  • 130
  • 149
  • 3
    Beware of spaces in file names. – slhck Feb 22 '12 at 18:27
  • 1
    The `-d"\n` switch fixes the spaces problem. – Frg Feb 22 '12 at 19:14
  • 2
    Note - some distros (like Mac OS) don't have a `grep -P` (Perl regex). `grep -E` may work in this case. – bluescrubbie Oct 02 '13 at 20:59
  • 1
    I prefer using `-I` with `xargs` and always test with non-lethal commands first: `xargs -d"\n" -I {} echo "{}"` – jozxyqk Mar 24 '14 at 05:40
  • 1
    Parsing `ls`? See [this question](http://unix.stackexchange.com/q/128985/108618) which points to [this article](http://mywiki.wooledge.org/ParsingLs). Because of the pitfalls you may `rm` what you don't want to. – Kamil Maciorowski Nov 15 '16 at 11:57
  • @Frg Beware of newlines in file names. Or other strange characters. – 8bittree Nov 16 '16 at 19:43
  • this won't work if `ls` or `grep` aliased with `--color=always` option and you will need to do `\ls | \grep ...` – αғsнιη Oct 12 '18 at 04:42
  • This is not the most robust solution (spaces in filenames), especially considering `find` does all this in one command. – qwr Feb 25 '20 at 21:58
78

Or using find:

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

This solution will deal with weird file names.

cYrus
  • 21,379
  • 8
  • 74
  • 79
  • 12
    This 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 Apr 14 '15 at 21:31
  • 2
    Furthermore you have more control on what you delete, for example adding `-type f` – Marco Sulla Jun 08 '16 at 08:12
  • Can this be used to delete files and folders? It does not work for non empty folders. – Alex Sep 09 '17 at 11:46
  • @Alex nope, the directory must be empty (it wasn't an OP requirement anyway), you can use the `xargs` approach with `rm -f`. – cYrus Sep 09 '17 at 13:54
  • also obligatory link to article about parsing ls – qwr Feb 25 '20 at 21:48
19

See the filename expansion section of the bash man page:

rm A*[0-9][0-9]
glenn jackman
  • 25,463
  • 6
  • 46
  • 69
3

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
  • 216
  • 1
  • 3
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.

Gaff
  • 18,569
  • 15
  • 57
  • 68
Salvatore
  • 29
  • 1
  • 1
    This doesn't seem to add much beyond what [Dennis's 4 year old answer](http://superuser.com/a/392878/302463) already says. – 8bittree Nov 16 '16 at 19:48
  • 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 Mar 15 '17 at 11:22
2

This works on my mac:

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

Ray
  • 21
  • 1