3 Ways to Remove All Files in a Linux Directory

A directory or folder in Linux contains various types of files such as text, image, audio or any executable program and even sub-folders. Sometimes it becomes important to free up space in a directory and you can do so by removing all its existing files. In this article, I will explore different ways to remove all the files in a Linux directory regardless of the file types or extensions but not deleting any sub-folders. However, caution must be adopted while deleting files as this is a permanent operation and can be difficult to restore them.

Key Takeaways

  • Learning how to remove only the files and not the directory itself.
  • Understanding how to specify the files and delete them using find command.
  • Knowing how to remove hidden files.

Process Flow Chart

Distro Used Throughout the Tutorial: Ubuntu 22.04.02 LTSProcess flow chart to remove all files in a Linux directory.

Watch 3 Methods to Remove All Files in a Linux Directory

3 Methods to Remove All Files in a Linux Directory

The goal of this article is to show you how to remove the files of a directory while still keeping the directory or its subfolders intact. Here I will show you three different methods to accomplish this objective using the Command Line Interface (CLI).

The first method employs the most widely used command, rm. The second method specifies the files and deletes them using a single command find. The third method focuses on deleting hidden files which require more attention when removing from a system as they contain important configurations.

You can read our Comparative Analysis of Methods to distinguish between these three methods and pick the best one for your needs.

Method 01: Using rm Command to Remove Files in a Directory in Linux

You can employ the rm command to remove any file or folder easily. However, to specify the deletion of files in a directory, in this case, directory Ayesha, I will use rm followed by path-to-directory and an asterisk (*) at the end.

Steps to Follow >

➊ At first, open your Ubuntu terminal.

➋ Type the ls command to list the contents of your desired directory, in this case, it was directory Ayesha.

ls /home/ayesha/Ayesha/
  • ls: Lists files and directories.
  • /home/ayesha/Ayesha/: Specifies the absolute path of the directory.

➌ Press ENTER key.Listing contents of directory in Linux This directory contains some text files, an image file, and two sub-directories.

➍ To delete only the files, execute the following command.

rm /home/ayesha/Ayesha/*
  • rm: Removes file or folder.
  • /home/ayesha/Ayesha/: Specifies the absolute path of the directory Ayesha.
  • *: Deletes all the files but will not delete any sub-directory.

➎ Press ENTER key again.

➏ Finally, type the ls command to check if all the files are deleted or not.

ls /home/ayesha/Ayesha/

removing only files wit rm In the above image, it is clear that all the files within the directory are deleted while the sub-directories still exist.

Read More: 3 Ways to Remove Write Protected Regular File Using the rm command

Method 02: Using find Command to Remove Files in a Directory in Linux

You can easily look for files and folders in a directory with the find command based on name, size, type and time. The option -type f indicates the files which need to be searched. To delete all the identified files you can implement find command followed by -delete option.

Steps to Follow >

➊ Open your Ubuntu terminal.

➋ Then, navigate to your desired folder. In my case, I went directly to the directory named  Ayesha.

➌ Type the following command and press ENTER button. This will show you the list of contents.


➍ After that type the following command to specify the type of content which you want to delete.

find . -type f -delete
  • find: Searches for files and directories.
  • single dot (.): Indicates current directory.
  • -type f: Finds all files.
  • -delete: Removes the specified content.

➎ Then type the below command to check the contents.


➏ Press ENTER key.Searching files and deleting them inside a directory in Linux.The above image depicts that all the files are deleted except the subdirectories of the directory Ayesha.

Read More: Remove All Files in a Directory with the Prompt in Linux

Method 03: Using dotglob Option to Remove Files in a Directory in Linux

While removing files, you can enable the dotglob option to include the hidden files which otherwise would not have been included. To do so follow the procedures below.

Steps to Follow >

➊ Open Ubuntu terminal.

➋ Navigate to your desired directory.

➌ Type the following command to print out all the files including hidden files.

ls -a
  • ls: Lists all files and directories.
  • -a: Display all files including hidden files whose names start with a dot (.).

➍ Press the ENTER button.

➎ Type the command below to turn on dotglob option.

shopt -s dotglob
  • shopt: A built-in shell command to manipulate shell option.
  • -s: Enables or sets shell option.
  • dotglob: Enables shell to include hidden files and directories.

➏ Type the given command and hit ENTER button.

rm -f  *
  • rm: Remove file or folder.
  • -f: Force removal of files without prompting the user for confirmation.
  • *: Indicates any sequence of characters in the current directory. * used with rm deletes the files but not the sub-directories.

➐ Enter the following command to check the list of contents along with hidden files again.

ls -a

➑ Type the command below to turn off dotglob option.

shopt -u dotglob
  • shopt: A built-in shell command to manipulate shell option.
  • -u: Disables or unsets shell option.
  • dotglob: Enables shell to include hidden files and directories.

Removing files including hidden files in Linux.As you can see that after executing the above-mentioned steps, no files including the hidden files exist in the directory anymore. Moreover, the single dot (.) and double dot (..) in the list of contents indicate the current directory and parent directory respectively.

Read More: Remove All Files from Current Directory in Linux [2 Methods]

Comparative Analysis of Methods

I have demonstrated three methods for the removal of files in a Linux directory. A comparison is done to show you the similarities and differences among these methods. It will be easier for you to understand when to use a certain command.

Methods Pros Cons
Method 1
  • Quick and easy way.
  • Delete multiple files at once using a wildcard (*) character.
  • Removes files permanently.
  • Needs utmost caution while using this command.
Method 2
  • Precise control over the files by specifying the criteria (file name, type, size, time) for removal.
  • Requires knowledge about the options. to specify criteria.
  • Deletes files permanently and irreversibly.
Method 3
  • Removes all files including hidden files.
  • No need for complex commands.
  • Can be risky as hidden files contain important data for systems.
  • Removes files in the current directory and subdirectory. So important to stay in the correct directory.

Although all three methods have pros and cons, it depends on the user which method to use to get the best outcome. When there is a need to delete multiple files instantly, method 1 can be implemented. Method 2 can be put into action when you want to delete files by looking for them. Lastly, method 3 is advantageous in cases when you want to delete all files including the hidden ones.


The removal of all files in a Linux directory can be accomplished by the three methods shown in this article. You should use the method best suited for your purpose depending on important factors such as file type, need for confirmation, hidden files and their configuration. However, you should be careful when handling these commands. Furthermore, make sure to specify the options carefully for each command. The deletion of files are permanent and hence I recommend you to have a regular backup of your important files.

People Also Ask

Can I recover files that were accidentally deleted using the rm command?
To recover the files that were accidentally deleted, use data recovery tools such as GUI tools, CLI tools and ext3prep successor. However, recovery is a complex process and there is no guarantee of success. It is recommended to have a backup for important files.

How to remove files in a directory Linux without prompt?
The command rm -f removes files without prompt or confirmation. The option -f forcibly deletes files.

How to remove files in a Linux directory except one or a few files?
The pattern !(pattern_name) after rm command will delete all files except for one or a few desired files. If you want to exclude one file from the removal, you can implement the command “rm !(filename)“. For excluding more than one file use the following command: “rm !(filename1 | filename2)“. Again to remove all files except zip files the command should be “rm !(*.zip)“.

How to remove files in a Linux subdirectory?
The command rm -r /path/to/directory/* will delete the files in the directory and subdirectory. The option -r tells the rm command to delete recursively.

How to remove a directory in Linux?
You can use the command rmdir <directory_name> to delete a folder. It will permanently remove the directory.

Related Articles

Rate this post
Ayesha Tun Nashrah

Hello! I am Ayesha Tun Nashrah, currently working as a Linux Content Developer Executive for LinuxSimply, SOFTEKO. Completing my graduation in engineering, I aim to collaborate my technical knowledge and creative writing skills to create engaging content for Linux. My goal is to contribute to the Linux community by writing tutorials, articles, and guides for novice and Linux experts. Besides, writing about Linux articles, you will find me immersed in books and enjoying music. Read Full Bio

Leave a Comment