force delete directory in linux

How to Force Delete Directory in Linux

Sometimes when you try to delete a folder in Linux, you may get ‘Permission Denied’ error message. But you may really need to delete the directory anyway. So you will need to forcefully delete the folder. In this article, we will learn how to force delete directory in Linux.

How to Force Delete Directory in Linux

Generally, you get this error when the Linux user you have logged in as, does not have write ‘w’ permission to modify its contents. Although write permission is required for writing contents to a directory, it is also required in order for you to delete its contents. If you are in a directory where you have write(w) permission, you can easily delete its files irrespective of their permissions.

Sometimes you may also get this error, if you are not the owner of this directory. We will look at how to fix these two problems.

Let us say you have folder /home/data which you are unable to delete.

$ rm -r /home/data
rm: cannot remove 'data': Permission denied

$ sudo rm -r /home/data
rm: cannot remove 'data': Permission denied

First, run ls command on this folder to check if you have the write permission or not.

$ ls -l /home/data
drwxr-xr-x 2 root root 4096 May 1 13:45

As you can see above, only root user has ‘w’ permission while other users don’t. So you will not be able to delete the folder unless you have logged in as root user.

So you need to modify the folder permission for your user so that you get the permission to delete it. You can do this with chmod command. The number 777 indicates read-write-execute (rwx) permission for all users. You can modify it as per your requirement.

$ chmod 777 /home/data

Now run ls command to check its permissions.

$ ls -l /home/data
drwxrwxrwx 2 root root 4096 May 1 13:45

You will see that the directory permissions drwxrwxrwx include ‘w’ permission for all users. Now you should be able to delete the directory.

$ rm -r /home/data

If you are still unable to delete the required directory, use chown command to change the ownership of the directory. Let us say you have logged in as user ubuntu. As you can see above, the owner of /home/data is root. Here is the command to change its owner to user ubuntu.

$ chown ubuntu:ubuntu /home/ubuntu

Now check the directory permissions again to see if its owner has changed. It will have changed to ubuntu.

$ ls -l /home/data
drwxrwxrwx 2 ubuntu ubuntu 4096 May 1 13:45

Now you should be able to remove directory using rm -r command.

In this article, we have learnt how to force delete directory. You can use these steps to delete directories where you get ‘permission denied’ error messages, when you try to delete them. Of course, you should be careful while changing directory ownership and permissions as it can affect other applications that access it. So follow the above steps only if you really need to.

Also read:

How to Upload File Asynchronously in JavaScript
How to Store Data in Local Storage in JavaScript
How to Display Local Storage Data in JavaScript
How to Enable, Disable & Install Yum Plugins
How to Delete Root Mails in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *