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

A Linux system contains thousands of files and directories. Some files or directories are necessary and some are not necessary in the 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.

Process flow chart to forcefully remove a directory in Linux:Process flowchart of forcefully removing directory in Linux.

[Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS]

Common Failed Attempt 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.

Steps 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:

  1. First, open the Ubuntu terminal.
  2. 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.
  3. 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.
  4. 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.

How to Remove Directory Recursively in Linux

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:

  1. First, open the Ubuntu terminal.
  2. 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.
  3. 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.
  4. 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.

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?

To delete a directory forcefully, execute the rm -rf DIRECTORY_NAME command in the command prompt.

How do you force delete a file in Linux?

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?

To delete a directory in Linux without permission, just run the rm -rf DIRECTORY_NAME command from your Ubuntu terminal to forcefully remove it.

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
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
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