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?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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

Solution 2

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.

Solution 3

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 

Solution 4

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

Use xdev option to delete files only within device boundary.

Solution 5

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

rm -rf dontdelete/*

Solution 6

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.

Solution 7

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/!(.|..)

Solution 8

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 before you do it.

An alternative which makes sure your ‘cd’ command works before you issue the ‘rm’ is to use

cd dontDeleteMe && rm -rf *

Solution 9

I am not sure why this is so complex, help me if i am wrong

cd DoNotDeleteDir #<- this is just to make sure we are inside
find . | xargs rm -rf

That’s it

Solution 10

  1. Easiest thing for me – a windows expert but an ubuntu newbie
  2. Click the Files icon on the launcher
  3. Navigate to the directory where the
    files and folders are located that you want to delete
  4. Right Click in a blank area of the window next to the files and click “Open in Terminal” – leave the Files window open
  5. A terminal window will open and will be “set” to the folder you located
  6. You can type “dir” (disregard quotes when i say type) and press enter for the terminal to show a list of files and folders – just to prove you are “in” the right folder
  7. type “rm -rf *” and press enter
  8. depending on size of folders/files to delete system will pause
  9. When terminal prompt returns, the Files window you had opened will now say “Folder is Empty”
  10. I had success with this method and it gave me comfort to see the files/folder both in the Files window and as a result of the Dir command in the terminal window
  11. I was also comforted that the Files window displayed the folder now empty – especially since I had been chasing these files around looking for the trash folder that they were in
  12. Thanks to everyone else who submitted answers – it was very informative

Solution 11

rm -rf ${PWD}/*

will clear the present working directory

Solution 12

I would use find as suggested by guciek’s answer, and add -xdev to make sure it doesn’t descend into other filesystems:

find 'dontDeleteMe' -xdev -mindepth 1 -deletea

(I added quotes in case the real path contains spaces)

An alternative with Bash and rm could be

rm -rf 'dontDeleteMe'/{*,.[!.]*}

This would also remove hidden files, without producing errors. See this answer for details.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply