Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

What is the proper way to delete all the contents of a directory, without said directory?
For example, I have the following directory structure:


I want to delete the folders bar1 and bar2 and the file x.txt, without deleting foo.

Also please consider file names with special characters that may be ignored or raise an error.

Edit #1 (30 Sep, 18:26 UTC):
I think that my question isn't a duplicate of this question, because that I asked to delete everything inside a directory, while the other question didn't and the answers didn't include such solution.

share|improve this question

marked as duplicate by slm, Anthon, rahmu, jasonwryan, derobert Sep 27 '13 at 20:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 5 down vote accepted

If you know the directory you can do using one of these commands:

$ find foo/* -delete

$ rm -fr foo/*

$ find foo/* -exec rm -fr {} +

If you have files that start with a dot (.) then you'll need a modified version of rm.

$ rm -fr foo/{*,.*}


$ ls -l foo
drwxrwxr-x    6 saml saml  4096 Sep 27 09:43 .
drwx------. 263 saml saml 32768 Sep 27 09:42 ..
drwxrwxr-x    2 saml saml  4096 Sep 27 09:43 dir1
drwxrwxr-x    2 saml saml  4096 Sep 27 09:43 dir2
drwxrwxr-x    2 saml saml  4096 Sep 27 09:43 dir space1
drwxrwxr-x    2 saml saml  4096 Sep 27 09:43 dir space2
-rw-rw-r--    1 saml saml     0 Sep 27 09:43 .dot space1
-rw-rw-r--    1 saml saml     0 Sep 27 09:43 .dot space2
-rw-rw-r--    1 saml saml     0 Sep 27 09:43 file1
-rw-rw-r--    1 saml saml     0 Sep 27 09:43 file2
-rw-rw-r--    1 saml saml     0 Sep 27 09:43 file space1
-rw-rw-r--    1 saml saml     0 Sep 27 09:43 file space2

$ rm -fr foo/{*,.*}
rm: cannot remove directory: `adir/.'
rm: cannot remove directory: `adir/..'

$ ls -l foo/
total 0

Repeating with a fresh directory of files:

$ find foo/* -delete

Dealing with special characters

If you have a directory named foo tastic that has a space you can quote it but still use a wildcard:


$ rm -fr "foo tastic"/*

Special Characters

I'll often use this trick to see what the shell thinks of my filename concoctions prior to running them. In the sample directory I used above:

$ ls -1 "file "* file space1 file space2

$ ls -1d "dir "* dir space1 dir space2

By doing these tests before hand you can get a sense of what files/directories will be in play when a command containing the glob is used.

share|improve this answer
Thank you! What is the meaning of {*,.*} in the first example? Wouldn't rm -fr foo/* be dangerous? Because of the wildcard * – Dor Sep 27 '13 at 13:52
It deletes files that match * or .*. Sometimes there are files that start with a . (.). These won't match the * glob. – slm Sep 27 '13 at 13:57
Could you also please use quotes? I've tried rm -R "./foo/*" but it gives an error. – Dor Sep 27 '13 at 14:00
@Dor - No you can't quote that because of the *. If you have files with crazy characters I'd use the find variant. – slm Sep 27 '13 at 14:02
rm -R "./foo a space"/* – slm Sep 27 '13 at 14:03

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