Linux: remove all directories with same name

I ran into an issue with a piece of software that created hundreds of directories amidst my music and video collections.  However, all of the directories had the exact same name.  Rather than delete all of them individually, I figured there had to be a way to delete them all with a single command.  Turns out there is.  I found the directions here.

Step 1) Open a terminal in Linux.

Step 2) Navigate to the parent directory. In other words, find a folder that contains all of the offending folders.  For example:

$ cd /home/ryan/Desktop

Step 3) Use the “find” command combined with the remove directory command to delete all of the folders with the same name.  Here’s the command:

$ find -type d -name name_of_directories -exec rm -rf {} \;


Here’s what the above does.  “find” calls the “find” program, telling the computer to look for something.  “-type d” tells the find program to look for directories (d) or folders rather than files.  Next is the name of the directories or folders you want to find (replace that with the name you’re searching for).  “-exec” tells the computer to execute something when it finds a directory that has the name you’re searching for.  The computer will execute the “rm” command, which means “remove” the directory.  The “-rf” modifier tell it to remove everything below the folder as well (-r is recursive), so it will delete folders with files inside them as well.  The last part “{} \;” tells the computer to iterate this for every folder it finds.

It took me a few tries to get this to work, but when it did, it was pretty slick.  Saved me hours of having to manually delete everything.  Also, just a heads up: when the command works, it will give you a list of all the folders it deleted but will say something to the effect that it could not find the folders, which is a very awkward way of saying, “I just deleted this folder; it used to be there, but now it’s not.”

 883 total views,  1 views today