Removing all files from the current directory is a task that many Linux users may need to execute at some time, whether it’s to clear out old files, clean up a directory before starting a new project, or simply free up space on the system. In this article, I’ll discuss a couple of different ways to remove all files from the current directory in Linux.
Key Takeaways
- Learning to remove all the files from the current directory using the rm command in Linux.
- Learning to use the find command & the exec command together to remove all the files from the current directory in Linux.
- Knowing about frequently asked questions and their answers regarding file permission.
Requirements
- You must have root/sudo access to Ubuntu.
- You need to backup important files before the task.
- You need to avoid removing system directories.
Process Flow Chart
Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS
Watch 2 Methods to Remove All Files From the Current Directory in Linux
2 Methods to Remove All Files From the Current Directory in Linux
Here, I am going to show 2 methods of removing all the files from the current directory in Linux. The first method is using the rm command and the second method is using the find command & the exec command together.
Method 01: Using the “rm” Command to Remove All Files From Current Directory in Linux
The rm command is used to remove files from directories in Linux. To remove all the files from the current directory, you can use the asterisk (*) symbol along with the rm command. Here I will remove all the files from the Summer directory which has a few files and subdirectories. Follow my lead for a better understanding:
Steps to Follow >
➊ At first, open the Ubuntu terminal.
➋ Now, navigate to the Summer directory by executing the command below:
cd Summer
- cd: Changes directory.
- Summer: The folder that contains all the files.
➌ To view all the files and subdirectories of the Summer directory, run the following command in the command prompt:
ls -lR
- ls: Shows all the files in a specific folder.
- option -l: Long listing format.
- option -R: Enables Recursive mode.
❹ Type the following command to remove all the files from the Summer directory:
rm *
- rm: Removes files.
- asterisk(*): Used to select all the files.
❺ Then, strike the ENTER button.❻ To check if all the files are deleted or not, copy the command below in the command prompt:
ls -lR
- ls: Shows all the files in a specific folder.
- option -l: Long listing format.
- option -R: Enables Recursive mode.
❼ And press the ENTER key.As you can see in the image above, all the files are removed from the current directory which is Summer.
In this process, you cannot remove the subdirectories and the files in the subdirectories. So if you want to remove all the files and subdirectories use the following command:
rm -r *
This will remove all existing things in the current directory.
Read More: 3 Ways to Remove All Files in a Linux Directory
- Remove All Files in a Directory with the Prompt in Linux
- How to Remove a User and Home Directory in Linux [2 Methods]
Method 02: Using the “find” & the “exec” Command With the “rm” Command
To remove all the files from the current directory, you can also use the find command & the exec command along with the rm command. Here I will remove all the files from the Winter directory. Follow the steps below to do the same:
Steps to Follow >
➊ Firstly open the terminal in Ubuntu.
➋ Now, navigate to the Winter directory by executing the command below:
cd Winter
- cd: Changes directory.
- Winter: The folder that contains all the files.
➌ To view all the files of the Winter directory, run the following command in the command prompt:
ls -l
- ls: Shows all the files in a specific folder.
- option -l: Long listing format.
❹Type the following command to remove all the files from the Winter directory:
find . -type f -exec rm {} \;
- find: Used to perform search operations.
- (.): Means the current folder.
- –type f: Specifies only regular files.
- -exec: Used to execute the rm command on each file found by the find command.
- rm: Removes files.
- {}: Placeholder for the current pathname.
- \: Used before (;) to treat it like an argument rather than a separator.
- ;: Indicates the end of the command.
❺ Then, strike the ENTER button.
ls -l
- ls: Shows all the files in a specific folder.
- option -l: Long listing format.
❼ And hit the ENTER key.As you can see in the snapshot above, all the files are removed from the current directory which is Winter.
Read More: How to Find and Delete Directory in Linux [3 Methods]
Comparative Analysis of Methods
As this article presents multiple methods for completing a single task, it is natural to feel uncertain about which one to select. For this reason, I have included a comparative analysis of two different approaches, outlining their pros and cons, to assist you in making a well-informed decision.
Methods | Pros | Cons |
---|---|---|
Method 1 |
|
|
Method 2 |
|
|
To wrap things up, it is worth noting that both approaches have their own strengths and weaknesses, and the right method for you will depend on your personal preferences. For those who do not want to remove the files inside subdirectories, Method 1 may be the better option, whilst you can choose Method 2 to remove even the files inside subdirectories.
Conclusion
Linux has multiple ways to remove all files from the current directory, including the rm command and the find command. Before running any program, it’s essential to take into account the consequences of each option. You should also always make a backup of any important files before making any system modifications.
People Also Ask
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
- 3 Ways to Remove Write Protected Regular File Using the rm command
FUNDAMENTALS A Complete Guide for Beginners