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. There are some ways you can use to remove all files in a Linux directory.
To delete all files in a directory from the terminal application, use the following commands: rm /path/to/dir/ to delete everything in the directory, or rm -r /path/to/dir/ to remove all subdirectories and files within the specified directory.

Process flow chart to remove all files in a Linux directory:Process flow chart to remove all files in a Linux directory.

[Distro Used Throughout the Tutorial: Ubuntu 22.04.02 LTS]

3 Methods to Remove All Files in a Linux Directory

You can remove all the files in a Linux directory in multiple ways. Those are listed below:

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

1. Remove Files in a Directory in Linux Using the “rm” Command

To remove any file or folder easily in Linux, you can employ the rm command. 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.

To remove files in a directory using the rm command, follow the steps:

  1. Type the ls command to list the contents of your desired directory after opening your terminal, in this case, it was directory Ayesha.
    ls /home/ayesha/Ayesha/
    EXPLANATION
    • ls: Lists files and directories.
    • /home/ayesha/Ayesha/: Specifies the absolute path of the directory.
  2. Listing contents of directory in Linux This directory contains some text files, an image file, and two sub-directories.
  3. To delete only the files, execute the following command.
    rm /home/ayesha/Ayesha/*
    EXPLANATION
    • 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.
  4. 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

2. Remove Files in a Directory in Linux Using the “find” Command

To remove files in a directory in Linux, you can use the find command. 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.

To remove files in a Linux directory using the find command, follow the steps:

  1. First, navigate to your desired folder. In my case, I went directly to the directory named  Ayesha.
  2. Type the ls command and press ENTER button. This will show you the list of contents.
  3. After that type the following command to specify the type of content which you want to delete.
    find . -type f -delete
    EXPLANATION
    • find: Searches for files and directories.
    • single dot (.): Indicates current directory.
    • -type f: Finds all files.
    • -delete: Removes the specified content.
  4. Then again type the ls command to check the contents and 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

3. Remove Files Including Hidden Files in a Directory in Linux Using “dotglob” Option

The dotglob option can be enabled to include hidden files and directories which otherwise would not have been included. Now, to remove those files including hidden files in a directory in Linux using the dotglob option, execute the following steps:

  1. Navigate to your desired directory.
  2. Type the following command to print out all the files including hidden files:
    ls -a
    EXPLANATION
    • ls: Lists all files and directories.
    • -a: Display all files including hidden files whose names start with a dot (.).
  3. Type the command below to turn on dotglob option:
    shopt -s dotglob
    EXPLANATION
    • 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.
  4. Type the given command and hit ENTER button.
    rm -f  *
    EXPLANATION
    • 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.
  5. Enter the following command to check the list of contents along with hidden files again:
    ls -a
  6.  Type the command below to turn off dotglob option:
    shopt -u dotglob
    EXPLANATION
    • 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]

Comparison of Methods to Remove All Files in a Linux Directory

Here is a comparison table to get a better understanding of which method you will use to remove all files in a Linux directory:

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.

Conclusion

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 is 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?

Yes, you can recover files that were accidentally deleted using the rm command. To do so, you can 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 a prompt?

To remove files without prompt or confirmation in a directory, use rm -f command in your command line followed by the file name that you want to remove. The option -f forcibly deletes files.

How to remove files in a Linux directory except one or a few files?

To remove files in a Linux directory except for one or a few, use !(pattern_name) after rm command. For instance, If you want to exclude one file from the removal use: rm !(filename). To exclude more than one file use the following command: rm !(filename1 | filename2). Again to remove all files except zip files use: rm !(*.zip).

How to remove files in a Linux subdirectory?

To remove files in the Linux subdirectory, use the command: rm -r /path/to/directory/*. The option -r tells the rm command to delete recursively.

How to remove a directory in Linux?

To remove a directory in Linux, use: rmdir <directory_name>. It will permanently remove the directory.

How to remove hidden files in Linux?

To remove all the hidden files from the current directory, you can type rm .*. Hidden files generally start with a dot (.) sign. To recursively remove the hidden files, you can use the rm -r .* command.


Related Articles

4.8/5 - (5 votes)
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
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