I was learning bash and wanted to clear a directory of all files. You could just rm -rf directory_path
and remove the whole directory and then mkdir directory_path
to recreate the directory, but i wanted to only remove all the files in one simple command.
P.S. hidden files/directories are those that only have an extension (example: “.git”, “.gitignore”)
I tried to check if anybody had already documented the solution to this simple problem but couldn’t find anyone. But I found 2 separate solutions one for removing non-hidden files/directories and one for hidden and tried to combine them.
1
Solution:
You can select all non-hidden files and directories with
*
and hidden ones with.*
. So to remove all files and directories userm -rf * .*
If you are not currently
cd
at the directory you will need to write:rm -rf [directory_path]/* [directory_path]/.*
Replace the
[directory_path]
with the path to your desired directory.