There are two alternative ways to delete a directory, file, or folder in Bash. We can delete the folders or directories using two different commands. The “rmdir” command is used to delete the empty folders or directories. The “rm” command is used to delete the folders or directories that are not empty. However, we will use the “rm” command to delete the folders in this article. Additionally, we delete some files from the Linux desktop.
Deleting a Folder or Directory in LINUX Using Bash
In this part, we demonstrate how to delete a file or directory using the Linux command line. But before we do this, we must first open the terminal. Before we can remove a folder or director, we must first know its name. Therefore, we must first execute the “ls” command in the terminal to find a folder or directory, or to view all of the folders. In Linux and other operating systems based on Unix, the “ls” command is used to display the files or folders. The ls function enables you to list all the files or folders in the current working directory by default and further engage with them through the command line, similar to how you explore in folder or terminal with a Graphical.
When we press the enter key on the keyboard after typing “ls” in the terminal, all of the folders or directories are displayed on the terminal window, as seen in the following figure. There are “6” folders in total with the names “Desktop”, “Downloads”, “Music”, “Pictures”, “Templates”, and “Videos” displayed.
Now that we are aware that the directories and folders do not include the extensions in their names, we attempt to delete the “Music” directory in this section using the “rm” command. To accomplish this, write “rm” then press the space bar before typing “-rf”. The “rm” or “-rf” command stands for recursive and prevents the server from asking you to confirm your deletion. In simple words, the “rm” command eliminates the entries for a selected file, set of documents, or collection of files from a directory’s list of entries. The “-rf” command permanently deletes a directory. It indicates that even if a record or folder has interpreted access, it will still be removed. Now that the “rm -rf” command is entered, we must put a space before typing the name of the folder that we want to delete. Therefore, enter the “music” folder here.
Now that we executed this command, the “Music” folder is removed. To verify the deletion, type the “ls” command in the terminal to see if the folder is deleted or not.
After the execution of this command, the names of the folders appear on the screen as shown in the following. As you can see, all the folders are visible except the “Music” folder which means that the folder is removed.
The following section is identical to the previous one. But in this section, we eliminate the “Templates” folder rather than the “Music” folder. To do this, we use the same “rm” command followed by a space, “-rf,”, and then another space which is preceded by the name of the “Templates” folder.
Therefore, when we execute the aforementioned command, the “Templates” folder is deleted. By entering the “ls” command in the terminal window, we now determine whether the folder is deleted or not.
When we press enter after typing this command, the name of the remaining folder is then displayed. In the following image, you can see that the “Music” and “Templates” folders are not included in the list of folders. Instead, only four folders – “Desktop”, “Downloads,” “Pictures,” and “Videos” – are displayed.
Deleting Multiple Folders or Directories
In this section, we use the same command to delete several folders at once. In this section, we eliminate two folders at once. To accomplish this, we use the command in which we first type “rm”. Then, we use space. After that, we type “-rf” then space. Lastly, the name of the folders that we want to delete. In this case, we want to remove the “Pictures” and “Videos” folders, so we type “Pictures” and “Videos” in the command.
Following the execution of this command, we type the “ls” command on the terminal to determine whether the folders are deleted or not.
Therefore, when we click enter, the names of the remaining folders are displayed, indicating that the folders that we deleted were successfully erased. We are now only left with the two directories – “Desktop” and “Downloads”.
Deleting Files from the Desktop
As we deleted the folders in the previous section, we now delete the files of the desktop in this section. Now, to delete a file from the desktop, we must first retrieve the directory location using the command where “cd” is used first, followed by space. Then, type “Desktop” and a slash at the end.
After running this command, we get the directory of the desktop. Now, we use the type of the “ls” command to display all the files on the screen.
As you can see in the following image, when we ran this command, the entire desktop’s number of files appears on the screen. It has three files within. The first of which is called “file.sh” because it is a shell script and the other two are text files called “textfile.txt” and “textfile.txt.save”.
Currently, we remove the “file.sh” file from the desktop. To do this, we use the command in which we first type “rm”, then a space, followed by the name of the file that we want to remove. In this case, it is “file.sh”.
When we execute this command, we use the “ls” command once again to verify whether the file has been eliminated.
When this command is executed, the name of the remaining file that’s been left on the desktop is displayed along with two files that demonstrate the removal of the “file.sh” file from the desktop.
Using the “*” Sign to Delete All Files from the Desktop
In this part, we use the “*” sign to delete all of the files from the desktop. In this case, we create new files on the desktop. Then, we use the “ls” command to display all the files of the desktop in the terminal.
As you can see in the following screenshot, when we execute this command, it displays all of the desktop’s files. These four files are “data.txt”, “empty.txt”, “Mh.txt”, and “sh.txt”.
We use the command in which we write “rm”. Then, we put space. After that, we write “-rf” and the “*” symbol. In the end, when we run this command, all of the files are deleted from the desktop, simultaneously.
Now, nothing is displayed on the screen when we use the “ls” command. Run to check the display of a desktop file because the file is deleted. You can see this in the following image:
In this article, we discussed how to use the “rm -rf” command in Bash to delete a folder or directory in Linux. The first section covered how to simultaneously delete multiple folders as well as how to use the “ls” command to display every folder or directory on the terminal window. In the second part, we discussed how to delete a single file from the desktop in Linux and how to delete all of the files from the desktop at once using the “*” sign.