How to remove all files and subdirectories in a directory WITHOUT deleting the directory in bash?

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?


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

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

find somedir -mindepth 1 -delete

This will match all files and directories within somedir and its (grand-)children including "hidden" dot files but excluding somedir itself because of -mindepth 1, then -delete them.


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 

find /dontDeleteMe/ -xdev -depth -mindepth 1 -exec rm -Rf {} \;

Use xdev option to delete files only within device boundary.


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

rm -rf dontdelete/*