Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 16 down vote accepted

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

share|improve this answer
Beware of spaces in file names. – slhck Feb 22 '12 at 18:27
The -d"\n switch fixes the spaces problem. – Frg Feb 22 '12 at 19:14
Note - some distros (like Mac OS) don't have a grep -P (Perl regex). grep -E may work in this case. – bluescrubbie Oct 2 '13 at 20:59
I prefer using -I with xargs and always test with non-lethal commands first: xargs -d"\n" -I {} echo "{}" – jozxyqk Mar 24 '14 at 5:40

Or using find:

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

This solution will deal with weird file names.

share|improve this answer
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

See the filename expansion section of the bash man page:

rm A*[0-9][0-9]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.