Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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

How to simply remove everything from a current or specified directory on Linux?

Several approaches:

  1. rm -fr *
    rm -fr dirname/*
    Does not work — it will leave hidden files — the one's that start with a dot, and files starting with a dash in current dir, and will not work with too many files

  2. rm -fr -- *
    rm -fr -- dirname/*
    Does not work — it will leave hidden files and will not work with too many files

  3. rm -fr -- * .*
    rm -fr -- dirname/* dirname/.*
    Don't try this — it will also remove a parent directory, because ".." also starts with a "."

  4. rm -fr * .??*
    rm -fr dirname/* dirname/.??*
    Does not work — it will leave files like ".a", ".b" etc., and will not work with too many files

  5. find -mindepth 1 -maxdepth 1 -print0 | xargs -0 rm -fr
    find dirname -mindepth 1 -maxdepth 1 -print0 | xargs -0 rm -fr
    As far as I know correct but not simple.

  6. find -delete
    find dirname -delete
    AFAIK correct for current directory, but used with specified directory will delete that directory also.

  7. find -mindepth 1 -delete
    find dirname -mindeph 1 -delete
    AFAIK correct, but is it the simplest way?

share|improve this question
For number 3, it depends on the shell. ZSH never expands . or .. on globs. – Kyle Brandt Aug 19 '09 at 11:45
up vote 8 down vote accepted

rm -fr * .*
Will work fine with at least GNU rm as it has special code to exclude "." and ".."

$ id
uid=65534(nobody) gid=65534(nogroup) groups=65534(nogroup)
$ cd /tmp
$ mkdir rmtest
$ cd rmtest
$ touch .test
$ ls -la
total 8
drwxr-xr-x 2 nobody nogroup 4096 2009-08-19 15:37 .
drwxrwxrwt 7 root   root    4096 2009-08-19 15:37 ..
-rw-r--r-- 1 nobody nogroup    0 2009-08-19 15:37 .test
$ rm -rf .*
rm: cannot remove `.' or `..'
rm: cannot remove `.' or `..'
$ ls -la
total 8
drwxr-xr-x 2 nobody nogroup 4096 2009-08-19 15:37 .
drwxrwxrwt 7 root   root    4096 2009-08-19 15:37 ..

FreeBSD rm man page says "It is an error to attempt to remove the files /, . or ..", so it probably works there too if you specify the force flag to ignore the error.

share|improve this answer
It will not work if there are too many files in this directory. – Tometzky Aug 21 '09 at 9:11

To add to your list, lets say you want to delete everything in the directory foo and all sub directories (that is what your find command does). I always found the simplest was:

#for dir foo with /home/kbrandt/foo
rm -rf /home/kbrandt/foo && mkdir /home/kbrandt/foo

If you don't want to delete the sub directories, modify your find command to include -type f

share|improve this answer
You'll then need to recreate ownership, permissions, labels (for selinux, acl etc.). Another process can have this directory opened. It just isn't simple. – Tometzky Aug 20 '09 at 8:28
 rm -rf directory

It'll effectively nuke everything, including the directory itself.

share|improve this answer
yepp. so why not add "&& mkdir directory"? – innaM Aug 19 '09 at 11:50
There may be ACLs in effect that do not permit deletion of the directory. Also, you might have to preserve any ACL upon deletion and reapply it after creation. – Jan Jungnickel Aug 19 '09 at 12:25

Why not a simple

ls -la <selected dir>| xargs rm -Rf


It will generate errors for "." & ".." but it will drop everything else.

share|improve this answer
Use ls -lA instead ... – reinierpost Sep 25 '09 at 11:35
It will not work, because "ls -lA" shows not only file names, but also permissions etc. "ls -A | xargs rm -Rf" will not work on files with spaces. Also often "ls" is an alias, which makes for example executables to display "*" after file name. This answer is so wrong. – Tometzky Sep 25 '09 at 12:32
Yep, ls is often an alias to a more complex command. But I tried to give a simple answer with simple command. – Pierre-Yves Gillier Sep 25 '09 at 12:36

Here's something that just worked for me:

zip -rm foo *

It may not be the most efficient method, but it worked.

share|improve this answer

Your Answer


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.