How to delete all hidden files and directories using Bash?
The obvious solution produces an exit code of 1:
bash$ rm -rf .*
rm: cannot remove directory `.'
rm: cannot remove directory `..'
bash$ echo $?
1
One possible solution will skip the "." and ".." directories but will only delete files whose names are longer than 3 characters:
bash$ rm -f .??*
Solution 1:
rm -rf .[^.] .??*
Should catch all cases. The .??* will only match 3+ character filenames (as explained in previous answer), the .[^.] will catch any two character entries (other than ..).
Solution 2:
find -path './.*' -delete
This matches all files in the current directory which start with a .
and deletes these recursively. Hidden files in non-hidden directories are not touched.
In case you really wanted to wipe everything from a directory, find -delete
would suffice.