How to delete only directories and leave files untouched

kopelkan picture kopelkan · May 10, 2011 · Viewed 46.2k times · Source

I have hundreds of directories and files in one directory.

What is the best way deleting only directories (no matter if the directories have anything in it or not, just delete them all)

Currently I use ls -1 -d */, and record them in a file, and do sed, and then run it. It rather long way. I'm looking for better way deleting only directories

Answer

Cas picture Cas · Jul 12, 2011

To delete all directories and subdirectories and leave only files in the working directory, I have found this concise command works for me:

rm -r */

It makes use of bash wildcard */ where star followed by slash will match only directories and subdirectories.