Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete contents but not directory in Linux?

Tags:

linux

ubuntu

How do you delete all content of directory but not delete the directory itself in Linux command line terminal?

like image 732
djangoman Avatar asked Dec 23 '22 18:12

djangoman


1 Answers

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

like image 167
mivk Avatar answered Dec 26 '22 08:12

mivk