Regularly, a Linux user works with multiple folders and several folders inside a folder. Some folders might need to be deleted. If a folder has content in it, it is necessary to delete the directory recursively. In this article, I will discuss two different cases of recursive remove of directory in Linux step by step.
Key Takeaways
- Getting familiar with the process of recursive removing directories both directly and interactively.
Process Flow Chart
Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS
Downloads
Download the folder and subfolders used in the articleWatch 2 Cases of Recursive Remove of Directory in Linux
2 Cases to Remove Directory Recursively in Linux
I will show you two ways how to remove directories recursively in Linux step by step. In the first case, I will show you to remove a directory directly. And in the second case, I will show you how to remove a directory interactively.
Case 01: Remove Entire Directory Recursively in Linux
You can quickly delete the entire directory including subdirectories inside that parent folder recursively. Here, I have a folder named mother. I have several subdirectories in this mother directory: child_1, child_2, and child_3.Here you can check the mother directory and subdirectories inside the mother directory structure by executing the “ls -lR mother” command in the terminal.
Now, I will recursively delete the entire mother directory including all subdirectories(child_1, child_2, and child_3). To do so, follow the below procedures.
Steps to Follow >
➊ At first, open the Ubuntu terminal.
➋ Now, type the following command into the command prompt then press the ENTER button to delete the entire mother directory including all subdirectories(child_1, child_2, and child_3) recursively. To do so, follow the below procedures.
rm -r mother
- rm: Deletes folder.
- -r: Deletes the following directory and subfolders inside that directory recursively.
- mother: Folder name.
➌ Then, execute the below command to print the contents of the mother folder.
ls -lR mother
- ls: Lists all the contents of the current directory.
- -lR: This option lists all the contents of the current directory and subdirectory inside this current directory with size, permission information, owner information, group information, last modification time, etc.
- mother: Folder name.
The above image shows that the mother directory does not exist. Hence, I have successfully deleted this folder and its subfolders(child_1, child_2, and child_3) recursively.
Read More: How to Force Remove Directory in Linux? [Step-by-Step]
- Remove All Files from Current Directory in Linux [2 Methods]
- How to Remove a User and Home Directory in Linux [2 Methods]
Case 02: Interactively Remove Directory Recursively in Linux
You can recursively delete selective directories inside a directory. In such a case, the terminal will ask every time before removing the directory. Hence, you can avoid unwanted removal of the directory. Here I have a directory named project. And there are three subdirectories named task_1, task_2, and task_3.
Here you can check the project directory and subdirectories inside the project directory details by executing the “ls -lR project” command in the terminal.
Now, I will recursively check the project folder and remove the task_1 folder. To do so, follow the below procedures.
Steps to Follow >
➊ At first, open the Ubuntu terminal.
➋ Now, type the following command into the command prompt then press the ENTER button.
rm -ri project
- rm: Deletes the folder.
- -r: Deletes recursively.
- -i: Ask permission before deleting or descending.
- project: Folder name.
The above image shows that I have pressed the Y to remove the project/task_1 folder and N in other cases.
➌ Then, execute the following command into the command prompt to print the contents of the project folder.
ls -lR project
- ls: Lists all the contents of the current directory.
- -lR: This option lists all the contents of the current directory and subdirectory inside this current directory with size, permission information, owner information, group information, last modification time, etc.
- project: Folder name.
The above image shows that I have successfully deleted the task_1 folder recursively from the interactive prompt.
Read More: How to Find and Delete Directory in Linux [3 Methods]
Complementary Information
Besides, knowing about remove directory recursive in Linux, you will find the below information helpful.
How to Remove a File in Linux
You can easily remove a file using the rm command in Ubuntu. Here I have a file named sample_file.txt.Now, I will remove the sample_file.txt file. So to achieve this, follow the steps given below.
Steps to Follow >
➊ At first, open the Ubuntu terminal.
➋ Now, type the following command into the terminal to delete the sample_file.txt file.
rm sample_file.txt
- rm: Deletes file.
- sample_file.txt: Filename.
➌ Then, execute the following command into the command prompt to print the details of the sample_file.txt.
ls -l sample_file.txt
- ls: Lists all the contents of the current directory.
- -l: This option lists all the contents of the current directory with size, permission information, owner information, group information, last modification time, etc.
- sample_file.txt: File name.
The above image shows that I have successfully removed the sample_file.txt file in Linux.
Read More: Remove All Files in a Directory with the Prompt in Linux
Conclusion
In this article, I have discussed the process of recursive removing directories directly and interactively in Linux. I hope that after going through this article, you will find the necessary information and be productive enough to remove directories recursively.
People Also Ask
How do I delete a directory in the Linux terminal?
What is the fastest way to delete a directory in Linux?
How do I delete files in Linux?
What is rm in Linux?
How to delete part of a file in Linux command?
Related Articles