Bash – How to delete all hidden files and directories using Bash

bashfiles

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 .??*

Best Answer

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 ..).

Related Topic