How to Force Remove Directory in Linux? [Step-by-Step]

A Linux system contains thousands of files and directories. Some files or directory is necessary and some are not necessary in long run. Keeping those unnecessary files or directories is not a good manner for maintaining an efficient system. If those files or directories are write-protected then you need to force remove the directory in Linux. In this article, I will demonstrate some approaches to remove a directory in Linux forcefully.

Key Takeaways

  • Getting familiar with the process of forcefully removing any directory in Linux.
  • Learning about the recursive and force options of the rm command in Linux.

Process Flow Chart

Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTSProcess flowchart of forcefully removing directory in Linux.

Common Unsuccessful Approach to Remove Write-Protected Folder

The most common approach to removing any write-protected directory is using the rm -r command. If you try to remove any write-protected directory using the rm -r command, you will get a confirmation prompt. It can not remove a write-protected directory without confirming from the confirmation prompt.Trying to remove folder1 directory using "rm -r" command.

Watch Step-by-Step Process to Force Remove a Write Protected Directory in Linux

Step-by-Step Process to Force Remove a Write Protected Directory in Linux

You can easily remove a write-protected directory using the rf option with the rm command. In this case, I will remove a write-protected directory named sample without asking for any confirmation. To do so, follow the below procedures.

Steps to Follow >

➊ At first, open the Ubuntu terminal.

➋ Now, for printing the permission mode of the sample directory execute the following command.

ls -l
EXPLANATION
  • ls: Lists all the contents of the current directory.
  • -l: Lists all the contents of the current directory with size, permission information, owner information, group information, last modification time, etc.

➌ Then, run the following command in the terminal to remove the sample directory forcefully.

rm -rf sample
EXPLANATION
  • rm: Deletes folder.
  • -rf: Deletes the following directory forcefully without asking for any confirmation.
  • sample: Directory name.

➍ Now, to see the existence of the sample directory execute the following command

ls -l
EXPLANATION
  • ls: Lists all the contents of the current directory.
  • -l: Lists all the contents of the current directory with size, permission information, owner information, group information, last modification time, etc.
The sample directory has been removed forcefully in Linux.The above image shows that I have successfully removed the write-protected sample directory forcefully.

Read More: 3 Ways to Remove Write Protected Regular File Using the rm command

Complementary Information

Besides knowing the process of forcibly removing the directory in Linux, you will find the below information helpful.

How to Remove Directory Recursively in Linux Using the rm Command

You can easily remove a directory recursively in Linux using the rm command in Linux. Here I will remove a folder named folder1 using the rm command in Linux. To do so, follow the below procedures.

Steps to Follow >

➊ At first, open the Ubuntu terminal.

➋ Now, for printing the permission mode of the folder1 directory execute the following command.

ls -l
EXPLANATION
  • ls: Lists all the contents of the current directory.
  • -l: Lists all the contents of the current directory with size, permission information, owner information, group information, last modification time, etc.

➌ Then, run the following command in the terminal to remove the folder1 directory recursively.

rm -r folder1
EXPLANATION
  • rm: Deletes folder.
  • -r: Deletes the following directory recursively.
  • folder1: Directory name.

➍ Now, to see the existence of the folder1 directory execute the following command.

ls -l
EXPLANATION
  • ls: Lists all the contents of the current directory.
  • -l: Lists all the contents of the current directory with size, permission information, owner information, group information, last modification time, etc.
Removing the folder1 recursively using the rm commandThe above image shows that I have successfully removed the directory named “folder1recursively.

Read More: 2 Cases of Recursive Remove of Directory in Linux

Conclusion

In this article, I have discussed the process of forcefully removing directories in Linux. I hope that after going through this article, you will find the necessary information and be productive enough to remove directories forcefully in Linux.

People Also Ask

How do you force delete a directory?

You can delete a directory forcefully by executing the “rm -rf DIRECTORY_NAME” command in the command prompt.
How do you force delete a file in Linux?
If you want to delete a file forcefully in Linux, execute the “rm -rf FILE_NAME” command in the Ubuntu terminal.
How do I delete a directory in Linux without permission?
You can easily delete a directory in Linux without permission forcefully by running the “rm -rf DIRECTORY_NAME” command into the Ubuntu terminal.
What is ‘rm -rf FILE/DIRECTORY_NAME’ in Linux?
The “rm -rf FILE/DIRECTORY_NAME” is the command syntax for removing a file or directory forcefully in Linux.

Related Articles

Rate this post
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

Leave a Comment