Linux – How to remove all the files in a directory

directorylinuxrecursiverm

I am trying to remove all files and subdirectories in a directory. I used rm -r to remove all files, but I want to remove all files and subdirectories, excluding the top directory itself.

For example, I have a top directory like images. It contains the files header.png, footer.png and a subdirectory.

Now I want to delete header.png, footer.png and the subdirectory, but not images.

How can I do this in linux?

Best Answer

If your top-level directory is called images, then run rm -r images/*. This uses the shell glob operator * to run rm -r on every file or directory within images.

Related Question