Say I have folders:
img1/
img2/
How do I delete those folders using regex from Linux terminal, that matches everything starts with img?
Say I have folders:
img1/
img2/
How do I delete those folders using regex from Linux terminal, that matches everything starts with img?
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 {} \;
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*/
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.