How do you delete all content of directory but not delete the directory itself in Linux command line terminal?
To remove everything under $your_dir/
, including hidden files and directories, but without removing the $your_dir
directory itself, you can use find
:
find "$your_dir" -mindepth 1 -delete
An alternative with rm
in Bash would be:
rm -rf "$your_dir"/{*,.[!.]*}
The second part in the curly braces ( .[!.]*
) takes care of hidden files which start with a dot, but only if they don't start with 2 dots. This avoids trying to remove .
and ..
, but would still aslo remove a file named .x
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With