9

Say I have folders:

img1/
img2/

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

shgnInc
  • 2,054
  • 1
  • 23
  • 34
c 2
  • 1,127
  • 3
  • 13
  • 21

3 Answers3

30

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 {} \;
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • 2
    one way to improve this could consist in adding `-maxdepth 1` to `find` like that `find . -type d -maxdepth 1 -regex "\./img.*" -exec rm -rf {} \;` it will be faster. – user827992 Aug 12 '12 at 19:48
  • I used your command quite a lot to clean the old backups on our production servers, but I can't figure out a regex to get all folders except the first of the month (means I want to wipe out every backup folder except 1 for each month). The folders name are defined like this: "yyyy.mm.dd.hh.MM.ss" (ex: 2016.07.29.03.00.19). Even `find . -type d -regex "\./2016\.*"` does not return anything ... any idea? – MrYoshiji Sep 08 '16 at 13:52
  • try `find . -type d -regex ".*/2016.*"` – Diego Torres Milano Sep 08 '16 at 14:33
  • 1
    `{}` is replaced by the file name and `\;` marks the end of the command passed to exec (if you don't escape it it would be taken by the shell) – Diego Torres Milano Dec 18 '17 at 19:21
  • [Other discussions](https://unix.stackexchange.com/a/245287/184553) also mention `$ find . -type d -regex "^\./img.*$" -exec rm -rf {} +` – Epigene Nov 07 '18 at 12:39
20

you could use

rm -r img*

that should delete all files and directories in the current working directory starting with img

EDIT:

to remove only directories in the current working directory starting with img

rm -r img*/
olly_uk
  • 11,559
  • 3
  • 39
  • 45
  • 2
    this will also delete any file that will match the expression, this command delete everything that match the expression, not only directories. – user827992 Aug 12 '12 at 19:19
  • the OP says 'that matches everything starts with img?' – olly_uk Aug 12 '12 at 19:21
  • 1
    If name must start img you should use `^img.*`. Without `^` character name may have img for example on the middle. – user902691 Aug 12 '12 at 19:35
  • 1
    as my solution was not using full regex just shell glob wildcard, as mentioned in other comment, that doesnt happen in this case – olly_uk Aug 12 '12 at 19:37
2

In the process of looking for how to use regexps to delete specific files inside a directory I stumbled across this post and another one by Mahmoud Mustafa: http://mah.moud.info/delete-files-or-directories-linux

This code will delete anything including four consecutive digits in 0-9, in my case folders with months and dates ranging from Jan2002 - Jan2014:

rm -fr `ls | grep -E [0-9]{4}`

Hope that helps anyone out there looking around for how to delete individual files instead of folders.

aaron
  • 6,339
  • 12
  • 54
  • 80