Search code examples
regexlinuxfindrm

How do I delete folders using regex from Linux terminal


Say I have folders:

img1/
img2/

How do I delete those folders using regex from Linux terminal, that matches everything starts with img?


Solution

  • Use find to filter directories

    $ find . -type d -name "img*" -exec rm -rf {} \;
    

    As it was mentioned in a comments this is using shell globs not regexs. If you want regex

    $ find . -type d -regex "\./img.*" -exec rm -rf {} \;