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 LTS
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. 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/
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.
ls
➍ 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.
ls
➏ Press ENTER key.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.
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 |
|
|
Method 2 |
|
|
Method 3 |
|
|
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 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?
How to remove files in a directory Linux without prompt?
How to remove files in a Linux directory except one or a few files?
How to remove files in a Linux subdirectory?
How to remove a directory in Linux?
Related Articles
- How to Force Remove Directory in Linux? [Step-by-Step]
- How to Undelete Folder in Ubuntu? [With Solutions]
- How to Remove a Non-Empty Directory in Linux [2 Methods]
- 2 Cases of Recursive Remove of Directory in Linux
- How to Remove a User and Home Directory in Linux [2 Methods]
- How to Find and Delete Directory in Linux [3 Methods]