2 Cases of Recursive Remove of Directory in Linux

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 article

Watch 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.

Warning: Once you remove any directory with the rm command you can not restore that directory.

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.Directory and subdirectories tree of the mother.Here you can check the mother directory and subdirectories inside the mother directory structure by executing the “ls -lR mother” command in the terminal.Details of the mother directory and subdirectories inside it.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
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.
  • mother: Folder name.

Removing the mother directory and subdirectories inside it recursively in Linux.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]



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.

Directory and subdirectories tree of the project.Here you can check the project directory and subdirectories inside the project directory details by executing the “ls -lR project” command in the terminal.Details of the project directory and subdirectories inside it. 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
Explanation
  • rm: Deletes the folder.
  • -r: Deletes recursively.
  • -i: Ask permission before deleting or descending.
  • project: Folder name.

Removing the task_1 subdirectory inside the project directory recursively from the interactive prompt in Linux..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
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.
  • project: Folder name.

I have successfully removed the task_1 subdirectory inside the project directory.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.The sample_file.txt exists on the desktop.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
Explanation
  • 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
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.
  • sample_file.txt: File name.

The sample_file.txt has been removed.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?

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

Rate this post
Susmit Das Gupta

Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

We will be happy to hear your thoughts

Leave a reply

LinuxSimply
Logo