rm -rf all files and all hidden files without . & .. error

* matches all non-dot-files, .[!.]* matches all dot files except . and files whose name begins with .., and ..?* matches all dot-dot files except ... Together they match all files other than . and ... If any of these three patterns matches nothing, it expands to itself; rm -f doesn't care about non-existent arguments, so this doesn't matter.

rm -rf ..?* .[!.]* *

You can also use find. This is more complex but has the advantage of working even if there are so many files that the wildcards above would expand beyond your system's command line length limit.

find . -name . -o -prune -exec rm -rf -- {} +

You may find it clearer to remove and recreate the directory. This has the advantage (or downside, as the case may be) of resulting in an empty directory even if another program is concurrently creating files in the original directory.


You could always send error messages to /dev/null

rm -rf /some/path/.* 2> /dev/null

You could also just

rm -rf /some/path/
mkdir /some/path/

...then you won't have to bother with hidden files in the first place.


Just realised this is the most convenient way in most Linux distros:

ls -A1 | xargs rm -rf

where

-A = list everything except . and ..

-1 = put every item in one line

Tags:

Rm

Wildcards