I'm wondering if there's a way on Ubuntu to delete all of the subfolders in a folder but keep all the files?

  • 7
    Are you asking how to move existing files from sub-folders in to the parent folder, then delete the sub-folders? – SimplySimon Sep 11 '13 at 15:31

a combination of the two answers above might work.

step 1.

find oldLocation -type f -exec mv {} newLocation \;   #find and copy all files.

step 2.

rm -frv oldLocation 

You might want to reword your questions, it's not very clear what the behavior you want is.


I'm going to assume you have a folder like this:

 subdir1  <--directory
 subdir2  <--directory
 file.txt <--file
 foo.mp3  <--file
 bar.pdf  <--file

...and you simply want to delete the two subdirectories and all they contain, leaving the files untouched. Simply use:

rm -r */

The trailing slash ensures that it will only match directories (and links to directories).

Your question is pretty vague though, please edit it to make it clear what you actually want.


Um, I assume that you want to keep the files in some new folder, right?

Caution: make sure you are in the correct directory. Make sure you do a test run first. Make sure there are no files with identical names in different directories.

mv */* /path/to/new/folder
rmdir *

Things get a tad more complicated if you have more than one level, e.g. you have sub-sub-sub-folders and you want to move the regular files and remove the directories. In that case,

find . -type f -exec mv {} /path/to/new/folder \;
find . -type d -exec rm -rf {} \;

Let's make the challenge even more interesting. Say, we want move the files, but we want to change the names such that they reflect the directory structure. For example, if, in the current directory there is a file foo.jpg in ./blah/pah/oh/, then we want to rename it to blah_pah_oh_foo.jpg. For safety reasons, we can copy it instead of moving.

for i in `find . -type f -print` ; do \
  newname=`echo $i | sed 's/^\.\///;s/\//_/g'` ; \
  echo moving $i to $newname ; \
  mv $i $newname ; \

Assuming you have a directory where you want to delete all subdirectories and any files and directories they contain but keep the files that were in the original directory, you can do

find your_directory -maxdepth 1 -type d -exec rm -rf {} \;

So given a tree like

|_ _ _ _ subdir1
|           |_ _ _ _ file1
|           |_ _ _ _ file2
|_ _ _ _ subdir2
|_ _ _ _ file3
|_ _ _ _ file4


find /path/to/dir1 -maxdepth1 -type d -exec rm -rf {} \;

will leave you with

|_ _ _ _ file3
|_ _ _ _ file4

I know it's possible to delete all files but keep the directories by this command:

find . -name *.* -exec rm -f {} \;

But I don't believe there to be a opposite way.


The question is impossible. You can't delete a directory without harming the files inside of it.

  • 4
    It is possible. – Zlatan Sep 11 '13 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.