27

I'd like to remove all directories from the pwd but leave the files in the pwd alone. If the content of my pwd is:

mydir1
mydir2
myfile1
myfile2

then I'd like to be left with just

myfile1
myfile2

I assume that I need to use rm -r -i

Am I correct?

Lesmana
  • 19,803
  • 6
  • 45
  • 44
Lee
  • 839
  • 7
  • 16
  • 29

5 Answers5

29

I found this one somewhere:

rm -r */

Seems the easiest way to go. With your example, you would have to confirm each case, if you have 5 files it's OK, but with bigger file structures an interactive mode is't the way to go... Just as a suggestion, if it's important information, make a backup...

Martin
  • 431
  • 1
  • 3
  • 6
16

No that would give you "missing operand" since you didn't specify anything. Putting a * would prompt also for files.

I'd give a try to:

find -mindepth 1 -maxdepth 1 -type d -exec rm -r {} \;

The mindepth 1 will exclude . from the results, the maxdepth 1 will exclude trying to do under the directories that will anyway get deleted (therefore creating a warning). But in practice you could leave them both out if you agree to have a few "innocent" warnings.

qwr
  • 657
  • 8
  • 13
fede.evol
  • 1,918
  • 1
  • 12
  • 6
14

Use

rm -rf ./*/

That avoids interactive mode an deletes only directories in your local directory.

WeSee
  • 336
  • 2
  • 7
  • As [J­de­B­P pointed out](https://superuser.com/q/713741/150988#comment912645_713746) on [Martin's very similar answer](https://superuser.com/q/713741/150988#713746),  if the current (top-level) directory contains symbolic links to other directories, they will also be deleted (even if they aren't in or subordinate to the current directory). – Scott - Слава Україні Dec 21 '18 at 07:07
4

Something like this should work:

find /path -type d -exec rm -rf '{}' \;

-type d looks for only directories

Matthew Williams
  • 4,314
  • 12
  • 26
  • 38
-1
you can also try in this way to delete only all folders not files from any location in linux.

    #delete only all dir and don't touch files
    #!/bin/bash
    for dir in `ls -l | grep ^d | awk '{print $9}'`
    do
    echo "going to delete $dir " `rm -rf $dir`
    done
    ls
linux.cnf
  • 101
  • 1