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

Is there a command to remove all files and subdirectories in a directory WITHOUT deleting the directory?

For example if I have directory "dontDeleteMe" with subdirectories "1", "2", "3" and each subdirectory has a few pictures in it, how can I remove the subdirectories "1", "2", and "3" and all the files in the them, without removing the parent directory "dontDeleteMe"?

share|improve this question

9 Answers 9

up vote 31 down vote accepted

To remove everything in a directory without removing the directory, type in:

rm -rfv dontDeleteMe/*

Please note, the /* part is very important. If you put a space before the *, it will delete all your files in your current directory.

Also, be very careful playing with rm, -r and * all in the same command. They can be a disastrous combination.

Update: Okay, I realized if you do have hidden/dot files [filenames with dots at the beginning, e.x. .hidden] then this will leave those files intact.

So really, the simplest solution to the original question is: rm -rfv dontDeleteMe && mkdir dontDeleteMe

Another one would be to use find's -exec option or pipe to xargs (below):

find dontDeleteMe/* -print0 | xargs -0 rm -rv

share|improve this answer
1  
Super thorough, props on the -v option –  user784637 Oct 28 '11 at 4:48
    
Can you give an example of using xargs? What is xargs? –  justingrif Nov 1 '11 at 18:55
    
@justingrif: find dontDeleteMe/* -print0 | xargs -0 rm -rv I believe in most cases this will work, regardless of spaces, and what not. But cd to /tmp/ and make a test directory and try it out. :) –  Matt Nov 1 '11 at 22:42
2  
Re-making a directory does not equal deleting its contents. e.g. umask directory permission differences, owner differences, etc... enzotib's answer would solve the issues with your original syntax –  BroSlow Oct 9 '14 at 20:43
2  
This is not a good answer. Using * is dangerous, and won't work on dotfiles without "dotglob" (see below). rm -rf dontDeleteMe && mkdir dontDeleteMe doesn't ensure that the dir is re-created with the same permissions/owner(s). Instead, use one of the find dontDeleteMe/ -mindepth 1 -delete variants below. –  Nils Toedtmann Jan 15 at 13:31

To delete (in terminal) all files and subdirectories except for the base directory named "dontdelete":

rm -rf dontdelete/*
share|improve this answer
    
This doesn't delete hidden files, so "all files" might be too bold a statement. –  Neftas Jul 25 at 15:55

The only reason rm -r ./* do not always work is because you can have hidden files and/or folder that are not matched by *.

To this end, bash provide an option to make * match everything, even hidden objects:

cd dont-delete-me
shopt -s dotglob
rm -r ./*

It can be useful to reset dotglob to its default (unset) state, if you keep on using the shell where you executed the above commands:

shopt -u dotglob 
share|improve this answer
find /dontDeleteMe/ -xdev -depth -mindepth 1 -exec rm -Rf {} \;

Use xdev option to delete files only within device boundary.

share|improve this answer
    
GNU find has -delete. It implies -depth and is easier to remember than -depth -exec rm -Rf {} \; –  Nils Toedtmann Jan 15 at 13:38

There is an even simpler answer:

  1. cd dontDeleteMe

  2. rm -rf *

Basic system administration lecture time: Be sure to pay attention to where you are when you use sweeping commands like this.

I can't say that enough. I've had to recover a box because someone wasn't paying attention and typed in rm -rf * while in /.

*nix assumes that if you are root or if you are sudo-ing as root that you know what you are doing. So make sure that you know what you're doing when you do it.

An alternative which makes sure your 'cd' command works before you issue the 'rm' is to use cd dontDeleteMe && rm -rf * .

share|improve this answer
6  
This is a dangerous suggestion. What you want is: cd dontDeleteMe && rm -rf * –  Izkata Oct 26 '11 at 20:32
    
@Izkata Or make sure to check that the cd operation was successful, before issuing the rm command. –  Eliah Kagan Nov 1 '11 at 17:42
1  
@EliahKagan That's exactly what my code snippet does... –  Izkata Nov 1 '11 at 18:00
    
Actually it's not any more or any less dangerous than what you suggest. All the && does is to chain the two commands together so that they execute at the same time. If anything my method is actually SAFER since you have to stop and .look before you execute the rm -rf *. Pausing to double check is always better. –  user30619 Nov 4 '11 at 13:57
4  
@user30619 The && operator doesn't just chain the commands together; it requires that each command be successful before the next one will be executed. If the cd command fails for any reason (directory doesn't exist, privileges, etc) then the rm command won't be executed. I'd call that safer. –  ean5533 Nov 9 '11 at 19:14

You can use find with the -delete flag:

find dontDeleteMe/* -delete

The /* is important as it tells find to search only INSIDE the folder called "dontDeleteMe".

Also ensure that the -delete flag is at the end of the find command.

share|improve this answer
    
This solution doesn't delete any hidden files, unless you enable dotglob. Otherwise, using the -mindepth 1 flag option seems the only way to get it working. –  Neftas Jul 25 at 15:59

There is a simple solution to this kind of problem,but to apply that you should know all stored type file in the folder and subfolder Go to search box type *.pdf, all the pdf file will be listed , Select all and delete. Again go to searc box type *.doc , all the word file will be listed, select all and delete. In this manner you can delete file , without disturbing directory tree.

share|improve this answer
    
That doesn't use bash, I don't think that an acceptable answer. –  NorTicUs Nov 7 '12 at 11:19

Open terminal (Ctrl+Alt+T) ant type this:

find somedir -mindepth 1 -delete
share|improve this answer
    
Welcome to AskUbuntu! While your answer could potentially help the original poster, the lack of information to explain what this would do to help him/her makes your answer incomplete. –  Oyibo Oct 3 '12 at 7:37
2  
This is the best answer. Simple and self-explanatory, not much to explain. –  Nils Toedtmann Jan 15 at 13:33
2  
Lacks explanation, but still the best answer. Also works in OSX (except for the way of opening Terminal, of course). –  noamtm Feb 5 at 14:43
rm -rf  directory/{.*,/*}

What says:

Remove all files starting with . in "directory" and all other files too.

Though as kindly noted by Neftas this solution is not safe!

Safer solution is:

 rm -rf directory/!(.|..)
share|improve this answer
    
This command could potentially be dangerous, since you're also including . and .. directories. This may be avoided by changing {.*} to {.??*}, but then you won't delete hidden files with only one character, such as .a. –  Neftas Jul 25 at 12:13
    
what's about this? rm -rf directory/!(.|..) –  A. Shulzhenko Jul 25 at 12:38
    
That looks like a much better solution than what I came up with. May I suggest you edit your answer? –  Neftas Jul 25 at 12:49
    
suggestion accepted ) –  A. Shulzhenko Jul 25 at 12:55

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.