Bash: How to Delete All Files in Directory


Often you may want to delete all files in a directory using Bash.

Here are the most common ways to do so:

Method 1: Delete All Contents in Directory (including the directory itself)

rm -rf /path/to/directory

Method 2: Delete All Contents in Directory (not including the directory itself)

rm -rf /path/to/directory/*

Method 3: Delete Files Only in Directory

rm -f /path/to/directory/{*,.*}

Here is what the various commands do:

  • rm: This stands for remove.
  • -f: This stands for force, which means you won’t be asked if you’re sure you want to remove contents.
  • -r: This stands for recursive, which allows you to recursively move down each folder and remove all contents.

The following examples show how to use each of these methods in practice with a directory located at the following path on our computer:

  • c:/users/bobbi/data3

We can use the ls command to view all of the files and folders in this directory:

We can see that this directory contains the following contents:

  • Two folders: BasketballData and FootballData
  • Three files: out_1.txt, out_2.txt and out_3.txt

Example 1: Delete All Contents in Directory (including the directory itself)

We can use the following syntax to delete all contents in the data3 directory, including the directory itself:

rm -rf c:/users/bobbi/data3

The following screenshot shows how to use this syntax in practice:

Bash delete all files in directory including directory

Notice that when we use the ls command again we are unable to retrieve the directory because the entire directory has been deleted.

Example 2: Delete All Contents in Directory (not including the directory itself)

We can use the following syntax to delete all contents in the data3 directory, not including the directory itself:

rm -rf c:/users/bobbi/data3/*

The following screenshot shows how to use this syntax in practice:

Bash delete all files in directory but not directory itself

Notice that when we use the ls command again we are able to retrieve the directory but it displays no contents since we deleted all contents in the directory.

Note that if you wanted to also delete the hidden files in the directory, you could use the following syntax instead:

rm -rf c:/users/bobbi/data3{*,.*}

Example 3: Delete Files Only in Directory

We can use the following syntax to delete all files in the data3 directory but keep all folders:

rm -f c:/users/bobbi/data3/{*,.*}

The following screenshot shows how to use this syntax in practice:

Bash delete all files in directory but keep folders

Notice that when we use the ls command again we can see that all files have been deleted from the directory but all folders remain.

Also note that we receive two messages that warn us about the two directories that cannot be deleted within the data3 directory.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

Bash: How to Replace Text in String
Bash: How to Replace Special Characters in String
Bash: How to Replace Multiple Characters in String

Leave a Reply