How to delete folders with a specific name

On Linux, how do I delete folders with a specific name that are deeply nested in the folder hierarchy?

The following paths are under the folder, and I would like to delete all folders named a .

 1/2/3/a 1/2/3/b 10/20/30/a 10/20/30/b 100/200/300/a 100/200/300/b 

Which Linux command should be used from the parent folder?

+113
linux unix rm
Oct 23
source share
10 answers

If the target directory is empty, use search, a filter with directories only, a filter by name, run the rmdir command:

 find -type d -name a -exec rmdir {} \; 

If you want to delete its contents recursively, replace -exec rmdir {} \; on -delete or -prune -exec rm -rf {} \; . Other answers include details about these loan versions, too.

+124
Oct 23
source share

Use find for the name "a" and run the rm command to remove the names specified according to your wishes, as follows:

 find . -name a -exec rm -rf {} \; 

First check it out using ls for the list:

 find . -name a -exec ls {} \; 

To only delete directories, not simple files, use the -type d argument (as noted in the comments):

 find . -name a -type d -exec rm -rf {} \; 

"{}" is a substitution for each file "a" found - the exec command is executed for each by substitution.

+137
Oct 23
source share

This also works - it will delete all folders called "a" and their contents:

 rm -rf `find -type d -name a` 
+51
Oct. 16 '14 at 16:54
source share

To remove all directories named foo , run:

 find -type d -name foo -a -prune -exec rm -rf {} \; 

Other answers are missing an important thing: the -prune option. Without -prune GNU search will delete a directory with a matching name, and then try to repeat it to find more directories that match. The -prune option specifies that it does not return to the directory that matches the conditions.

+22
Dec 03 '16 at 8:01
source share

In the end, I decided to delete my node_modules folders before backing up my work using rsync . The key requirement is that the node_modules folder can be nested, so you need the -prune option.

First I ran this to visually check the folders that need to be deleted:

 find -type d -name node_modules -prune 

Then I ran this to remove them all:

 find -type d -name node_modules -prune -exec rm -rf {} \; 

Thanks pistachio

+14
03 Oct '18 at 16:08
source share
 find ./ -name "FOLDERNAME" | xargs rm -Rf 

Gotta do the trick. WARNING if you accidentally downloaded . or / in xargs rm -Rf , your entire computer will be deleted without the possibility of returning it, requiring reinstalling the OS.

+11
Oct 23
source share

I had over 100 files like

 log-12 log-123 log-34 .... 

the above answers did not help me

but the following command helped me.

 find . -name "log-*" -exec rm -rf {} \; 

I gave -type how . , so it deletes both files and folders, starting with log-

rm -rf deletes folders recursively, even if they have files.

if you want the folders to be alone

 find -type d -name "log-*" -exec rm -rf {} \; 

single files

 find -type f -name "log-*" -exec rm -rf {} \; 
+9
Jun 09 '16 at 13:30
source share

Other:

 "-exec rm -rf {} \;" can be replaced by "-delete" find -type d -name __pycache__ -delete # GNU find find . -type d -name __pycache__ -delete # POSIX find (eg Mac OS X) 
+7
May 12 '17 at 12:03
source share

Combining several answers, here is a command that works on both Linux and MacOS

 rm -rf $(find . -type d -name __pycache__) 
+2
Aug 20 '19 at 15:40
source share

find the path / in / the / folders -maxdepth 1-name "my_ *" -type d -delete

-one
Nov 06 '17 at 12:25
source share



All Articles