Delete files with regular expression

142,295

Solution 1

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.

Solution 2

Or using find:

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

This solution will deal with weird file names.

Solution 3

See the filename expansion section of the bash man page:

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

Solution 4

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

Solution 5

This works on my mac:

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

Share:
142,295

Related videos on Youtube

gdoron is supporting Monica
Author by

gdoron is supporting Monica

Doron Grinzaig

Updated on September 18, 2022

Comments

  • gdoron is supporting Monica
    gdoron is supporting Monica over 1 year

    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?

  • Frg
    Frg over 12 years
    The -d"\n switch fixes the spaces problem.
  • bluescrubbie
    bluescrubbie over 10 years
    Note - some distros (like Mac OS) don't have a grep -P (Perl regex). grep -E may work in this case.
  • jozxyqk
    jozxyqk about 10 years
    I prefer using -I with xargs and always test with non-lethal commands first: xargs -d"\n" -I {} echo "{}"
  • JAMESSTONEco
    JAMESSTONEco about 9 years
    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.
  • Marco Sulla
    Marco Sulla about 8 years
    Furthermore you have more control on what you delete, for example adding -type f
  • Kamil Maciorowski
    Kamil Maciorowski over 7 years
    Parsing ls? See this question which points to this article. Because of the pitfalls you may rm what you don't want to.
  • 8bittree
    8bittree over 7 years
    @Frg Beware of newlines in file names. Or other strange characters.
  • 8bittree
    8bittree over 7 years
    This doesn't seem to add much beyond what Dennis's 4 year old answer already says.
  • Nishanth Matha
    Nishanth Matha over 7 years
    worked nicely for me.
  • glenn jackman
    glenn jackman over 7 years
    "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.
  • Alex
    Alex almost 7 years
    Can this be used to delete files and folders? It does not work for non empty folders.
  • cYrus
    cYrus almost 7 years
    @Alex nope, the directory must be empty (it wasn't an OP requirement anyway), you can use the xargs approach with rm -f.
  • Janac Meena
    Janac Meena over 6 years
    This was the simplest, yet complete answer to the question.
  • αғsнιη
    αғsнιη over 5 years
    this won't work if ls or grep aliased with --color=always option and you will need to do \ls | \grep ...
  • Itay
    Itay almost 5 years
    Will this work if the folder has a lot of files?
  • qwr
    qwr over 4 years
    also obligatory link to article about parsing ls
  • qwr
    qwr over 4 years
    This is not the most robust solution (spaces in filenames), especially considering find does all this in one command.
  • Gergely Lukacsy
    Gergely Lukacsy almost 4 years
    This one doesn't work in it's current form. You need to define -regextype egrep to make {x,y} type quantifiers work.