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 {} \;
-maxdepth 1
to find
like that find . -type d -maxdepth 1 -regex "\./img.*" -exec rm -rf {} \;
it will be faster. –
Substantial find . -type d -regex "\./2016\.*"
does not return anything ... any idea? –
Bolection find . -type d -regex ".*/2016.*"
–
Pariah {}
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) –
Pariah $ find . -type d -regex "^\./img.*$" -exec rm -rf {} +
–
Perigynous 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*/
^img.*
. Without ^
character name may have img for example on the middle. –
Proponent 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.
© 2022 - 2024 — McMap. All rights reserved.