How to simply remove everything from a current or specified directory on Linux?
Several approaches:
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 filesrm -fr -- *
rm -fr -- dirname/*
Does not work — it will leave hidden files and will not work with too many filesrm -fr -- * .*
rm -fr -- dirname/* dirname/.*
Don't try this — it will also remove a parent directory, because ".." also starts with a "."rm -fr * .??*
rm -fr dirname/* dirname/.??*
Does not work — it will leave files like ".a", ".b" etc., and will not work with too many filesfind -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.find -delete
find dirname -delete
AFAIK correct for current directory, but used with specified directory will delete that directory also.find -mindepth 1 -delete
find dirname -mindeph 1 -delete
AFAIK correct, but is it the simplest way?
rm -fr * .*
Will work fine with at least GNU rm as it has special code to exclude "." and ".."
http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/remove.c#n440
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.
http://www.freebsd.org/cgi/man.cgi?query=rm&apropos=0&sektion=0&manpath=FreeBSD+7.2-RELEASE&format=html
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:
If you don't want to delete the sub directories, modify your find command to include
-type f
It'll effectively nuke everything, including the directory itself.
Why not a simple
?
It will generate errors for "." & ".." but it will drop everything else.
Here's something that just worked for me:
It may not be the most efficient method, but it worked.