FUNDAMENTALS A Complete Guide for Beginners
![icon linux](https://linuxsimply.com/wp-content/uploads/2024/05/Linux-Mascot.png)
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.
Process flow chart of recursive remove of directory in Linux:
[Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS]
2 Cases of Recursive Remove of Directory in Linux
I will show you two ways 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
To remove the entire directory including subdirectories inside that parent folder recursively, you can follow this method. 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 remove the entire mother directory including all subdirectories(child_1, child_2, and child_3). To do so, follow the below procedures:
- Open the terminal and 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
EXPLANATION- 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
EXPLANATION- 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.
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
To remove recursively selective directories inside a directory, follow this case. In this 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:
- Open the terminal and type the following command into the command prompt then press the ENTER button to delete the entire mother directory including all subdirectories(task_1, task_2, and task_3) recursively. To do so, follow the below procedures.
rm -r project
EXPLANATION- -r: Deletes the following directory and subfolders inside that directory recursively.
- project: Folder name.
- Then, execute the below command to print the contents of the mother folder.
ls -lR project
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]
How to Remove a File in Linux
To remove a file in Linux, you can use 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:
- Now, type the following command into the terminal to delete the sample_file.txt file.
rm sample_file.txt
EXPLANATION- 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
EXPLANATION- 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.
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
What is recursive in Linux?
In Linux, recursive refers to the ability of a command to include all the subdirectories inside the directories. It works hierarchically to include all the subdirectories and files.
How to remove directories and subdirectories in Linux?
To remove directories and subdirectories in Linux, you can use the rm
command with the -r
option.
How do I delete a directory in the Linux terminal?
To delete a directory from the terminal in Linux execute the rm directory_name
command into the terminal.
What is the fastest way to delete a directory in Linux?
There are multiple ways of deleting a directory in Linux but using the rm command in Linux is the fastest way to delete a directory in Linux.
How do I delete files in Linux?
To delete files from the terminal in Linux type the rm file_name
command into the terminal then press the ENTER button.
What is rm in Linux?
The rm
stands for remove. It is used to remove files, directories, and links. By default, it does not remove directories. To remove an empty directory you have to use the -d
option and to remove a directory having contents you have to use the -r
option.
How to delete part of a file in Linux command?
To delete part of a file in Linux you have to use the cut
command. The cut command prints any part of a file you specify. So, for example, if you execute the cut -b -5 employee.txt
command, it will print the first 5 bytes from each line of the employee.txt file.
Related Articles