Removing all files in a directory can help to free up space on your hard drive, especially if the files in the directory are taking up a significant amount of disk space. This will improve the system’s performance and make your file system organized and easier to navigate.
The content of this article is discussed below, where we will discuss the potential methods of removing all files in a Linux directory.
Let’s start explaining the article by first discussing the rm command.
Method 1: Using the rm Command
In this section, we will discuss two different ways that you can apply to remove the files which are mentioned below:
- Remove All Files in a Directory Only
- Remove All Files, and Sub-Directories
Let’s discuss them both in detail in the next section.
Remove All Files in a Directory Only
The below command will show you the content of all the files and directories of a specific path that you accessed:
As you can see that there are total 5 different files available, so to delete all of the existing files you need to type:
$ rm -f *
The “-f” option is used to force the deletion of the files, whereas the * is a wildcard used to select all files in the current directory.
You can see in the above image that all the available files have been deleted, which also prompts you that the directories can’t be deleted.
Also, we have executed the above command in the home directory, so if you want to apply the same thing in any other directory, you need to access that directory first and type the same command as discussed.
$ rm -f *
It can be seen in the above image that we have deleted all the files from the document’s directory.
Remove All Files and Sub-Directories
Sometimes, a directory contains files and sub-directories, and those sub-directories also contain some files you want to delete. You can see all the content of any directory, including the subdirectories it contains, using the following command:
$ ls -R
It can be seen in the above image that there are 5 different files available in the Documents directory and two subdirectories as well with the name ‘dir1’ and ‘dir2’. The dir1 contains two more files with the name ‘demofile1’ and ‘demofile2’, whereas the dir2 contains ‘demofile3’ and ‘demofile4’.
So, to delete every single file and sub-directories from the Documents directory, you can type:
$ rm -rf *
-rf: This option is used to force the deletion of the files and directories.
You can see in the above image that nothing is showing now in the documents directory, meaning that all the files and the sub-directories have been deleted.
Method 2: Using the find Command
The find command is mainly used to find files, but you can also use the ‘-delete’ option to delete files with it. You can see in the below image that a total of 5 different files are available in the home directory with the format of ‘.txt’.
So, to delete all these files, you can run the below command, and then it can be seen that all the files have now been deleted:
$ find . -name "*.txt" -delete
The explanation of the above code is as follows:
find .: This is used to start searching for files in the present working directory (.).
-name “*.txt”: This is used to search for those files that exactly match the mentioned pattern *.txt.
-delete: After fulfilling the first two conditions, this will delete those that are found.
Method 3: Using Graphical User Interface (GUI)
If you do not want to use the terminal to remove all files, then you can also opt for the GUI version. So to do that, you need to access the directory from where you want to remove those files, select all of them and then press the “Delete” button from the keyboard. Another way is to right-click on the selected files and folder and choose “Move to Trash”.
Deleting all the files within a directory can have several benefits, including freeing up space on your hard drive and improving system performance. If the files in the directory are taking up a large amount of storage, removing them can help to make room for new files and improve the system’s speed and efficiency. So, in this article, we have discussed various ways using the CLI and GUI to explain how you can remove all files in a directory in Linux OS.