Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete only directories and leave files untouched

Tags:

directory

unix

rm

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

like image 726
kopelkan Avatar asked May 10 '11 16:05

kopelkan


People also ask

How do I delete all folders and keep files?

In the search box (top right), type NOT kind:folder . You will now see a list of all files in your current folder and all its subfolders. Use Control-A to select all the files. Now you can move them all to another folder.

How do I delete only folders?

Removing Directories with rmTo delete an empty directory, use the -d ( --dir ) option and to delete a non-empty directory, and all of its contents use the -r ( --recursive or -R ) option.

How do you delete all files in a directory without deleting the directory?

Open the terminal application. To delete everything in a directory run: rm /path/to/dir/* To remove all sub-directories and files: rm -r /path/to/dir/*

How do you remove a directory even if it is not empty?

To remove a directory that is not empty, use the rm command with the -r option for recursive deletion. Be very careful with this command, because using the rm -r command will delete not only everything in the named directory, but also everything in its subdirectories.


1 Answers

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.

like image 104
Cas Avatar answered Sep 28 '22 10:09

Cas