Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete folders using regex from Linux terminal

Tags:

regex

linux

find

rm

Say I have folders:

img1/
img2/

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

like image 362
c 2 Avatar asked Aug 12 '12 19:08

c 2


People also ask

How do I delete a directory in Linux terminal?

To permanently remove a directory in Linux, use either rmdir or rm command: For empty directories, use rmdir [dirname] or rm -d [dirname] For non-empty directories, use rm -r [dirname]

How do I delete a folder in terminal?

Delete a Directory ( rm -r ) To delete (i.e. remove) a directory and all the sub-directories and files that it contains, navigate to its parent directory, and then use the command rm -r followed by the name of the directory you want to delete (e.g. rm -r directory-name ).

Which command is used in Linux for deleting files and directories?

Use the rm command to remove files you no longer need. The rm command removes the entries for a specified file, group of files, or certain select files from a list within a directory. User confirmation, read permission, and write permission are not required before a file is removed when you use the rm command.

What is the fastest way to delete a directory in Linux?

There are two Linux commands you can use to remove a directory from the terminal window or command line: The rm command removes complete directories, including subdirectories and files. The rmdir command removes empty directories.


1 Answers

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 {} \;
like image 132
Diego Torres Milano Avatar answered Sep 20 '22 06:09

Diego Torres Milano