Remove All Files From Current Directory in Linux [2 Methods]

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

Flowchart to remove all files in current directory linux

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.

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

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
EXPLANATION
  • cd: Changes directory.
  • Summer: The folder that contains all the files.
navigates to summer directory(remove all files in current directory linux)In the snapshot above, you can see the directory is changed to Summer from home.

➌ To view all the files and subdirectories of the Summer directory, run the following command in the command prompt:

ls -lR
EXPLANATION
  • ls: Shows all the files in a specific folder.
  • option -l: Long listing format.
  • option -R: Enables Recursive mode.
view all files and subdirectories(remove all files in current directory linux)In the image above, you can see all the files, and subdirectories of the Summer directory.

❹ Type the following command to remove all the files from the Summer directory:

rm *
EXPLANATION
  • rm: Removes files.
  • asterisk(*): Used to select all the files.

❺ Then, strike the ENTER button.remove all files in current directory linux❻ To check if all the files are deleted or not, copy the command below in the command prompt:

ls -lR
EXPLANATION
  • ls: Shows all the files in a specific folder.
  • option -l: Long listing format.
  • option -R: Enables Recursive mode.

And press the ENTER key.check if all files are deletedAs 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



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
EXPLANATION
  • cd: Changes directory.
  • Winter: The folder that contains all the files.
navigates to Winter directory(remove all files in current directory linux)In the snapshot above, you can see the directory is changed from home to Winter.

➌ To view all the files of the Winter directory, run the following command in the command prompt:

ls -l
EXPLANATION
  • ls: Shows all the files in a specific folder.
  • option -l: Long listing format.
view all filesIn the image above, you can see all the files, and subdirectories of the Winter directory.

❹Type the following command to remove all the files from the Winter directory:

find . -type f -exec rm {} \;
EXPLANATION
  • 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.

Warning: If you use this method, it may remove all the files inside the subdirectories as well.
remove all files in current directory linux❻ To check if all the files are deleted or not, write the following command in the command prompt:

ls -l
EXPLANATION
  • ls: Shows all the files in a specific folder.
  • option -l: Long listing format.

 And hit the ENTER key.list all filesAs you can see in the snapshot above, all the files are removed from the current directory which is Winter.

Note: It’s important to note that the find command should only be used under extreme caution. Before running this command, make sure to review it twice and be mindful that if used incorrectly, it may remove important.

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
  • The syntax is very simple.
  • Can do simple tasks very fast.
  • Possibilities of making errors are low.
  • No options to filter by file type.
  • Can not delete files in sub-directories.
  • Limited range of capabilities.
Method 2
  • Can filter by file type & other attributes.
  • Can delete files in sub-directories.
  • More versatile & can handle complex tasks.
  • The syntax is complex.
  • Slower for simple tasks.
  • Possibilities are higher for making errors.

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

How can I remove all files in the current directory except for a specific file?
You can use the rm command with the option -f to force remove all files in the current directory, and then use the “-exclude” option to specify which files you want to keep.
Can I remove files in multiple directories at once?
Yes, you can recursively remove files from multiple directories using the rm command with the -r option. As an alternative, you can search for and delete files in numerous directories using the find command.
How do I force delete a directory in Linux?
In Linux, to force delete a directory use the rm command with the options –recursively or -r & –force or -f. Hence, use the command syntax “rm -rf /location/path/to/directory”.
How can I remove all hidden files in the current directory?
You can use the rm command with the -f and -r options to force delete all files in the current directory, including hidden files and directories.
Can I recover files that were deleted using the 'rm' command?
Most of the time, it is impossible to recover files removed with the rm command. It is usually advised to backup vital files before making any changes to the system.
What is the difference between the 'rm' and 'rmdir' commands?
The rm command is used to remove files and directories, while the rmdir command is used to remove empty directories only.

Related Articles

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Jannatul Ferdousi Sylvie

Hi there! This is Jannatul Ferdousi Sylvie. I am one of the Linux Content Developer Executives at Softeko. I live in Dhaka, Bangladesh. I have completed my BSc. in Software Engineering from American International University-Bangladesh. You can see my projects in the GitHub account. I love traveling, shopping, cooking, and gardening. Read Full Bio

Leave a Comment