Skip to main content
Quick Tip

Delete All Files of a Directory in Linux

Here is a quick Linux command tips on deleting the contents of a directory, not the directory itself.

Abhishek Prakash

Warp Terminal

At times you'll need to delete all the files of a directory. Not the directory itself but the contents of the directory. You could do it to clean up a project, free up space or for any other purpose.

To empty a directory, you use the following command:

rm -r path_to_dir/*

The wild card means everything here. So, you are instructing the rm command to remove everything in the given directory.

Please note that this is different than using rm on the directory name directly without /*. If you use rm -r dir, it will delete the directory along with its contents. That's not always desirable. Is it?

Let's see about deleting all the contents of a directory in detail.

Properly removing all files in a directory

Linux command line does not have a recycle bin. You have to be careful while deleting files. And if you have to remove multiple files using wildcard, you must be extra cautious.

This is why I advise switching to the directory you want to empty and then using the rm command. This reduces the risk of accidentally deleting the contents of a wrong directory.

Step 1: Go to the desired directory

Use the cd command to switch to the directory you want to empty.

For example, I am going to delete all the contents of the /home/abhishek/sample directory. So, I switch to it first.

cd /home/abhishek/sample

It's good to ensure that you are in the current directory:

pwd
Switch to the directory to delete its contents

Step 2: List the directory contents

You should check the directory contents to ensure that you are not deleting any important files. I advise showing hidden files as well.

ls -al

If there are sub-directories, please ensure nothing important is in there.

List directory contents

Step 3: Delete all files and folders in the directory

Once you are sure that nothing important is in the directory, it is time to delete the contents.

You can use the rm command like this:

rm -r *

Which is a way of instructing the rm command to delete everything it sees in the current directory recursively. The recursive option -r is essential for removing sub-directories.

Sometimes, there are write protected files and then you'll be asked to confirm the removal. To avoid that, you can include the forced delete option -f.

rm -rf *
Delete all files of a directory in Linux
🚧
As you can see above, the hidden files are not deleted this with rm -rf *.

To delete only the hidden files, you can additionally run this command:

rm -rf .*
Deleting hidden files

Conclusion

While deleting all the contents of a directory without deleting the directory itself seems like an easy job, things get a bit complicated if there are hidden files and folders.

In those cases, you have to run rm -rf .* after rm -rf *.

I hope you liked this quick little Linux command line tip. Let me know if you have questions or suggestions.

Abhishek Prakash