Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

share|improve this question
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
add comment

6 Answers

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

dir
 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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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 ; \
done
share|improve this answer
add comment

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

/path/to/dir1
|_ _ _ _ subdir1
|           |_ _ _ _ file1
|           |_ _ _ _ file2
|
|_ _ _ _ subdir2
|
|_ _ _ _ file3
|
|_ _ _ _ file4

running

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

will leave you with

/path/to/dir1    
|
|_ _ _ _ file3
|
|_ _ _ _ file4
share|improve this answer
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
3  
It is possible. –  ZDroid Sep 11 '13 at 16:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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