Linux allows you to protect files & directories from being deleted by settings its sticky bit, which allows on its owners or the root user to be able to delete them. This method is used by system administrators to protect files & directories from unauthorized users. The sticky bit not only prevents unauthorized users from deleting file & directories but also disallows users from making any modifications to them. Sometimes you may need to delete write protected file in Linux. Here are the steps to do so. They work in most Linux distributions.
How to Delete Write Protected File in Linux
Here are the steps to delete write protected file.
1. Check Permissions
Only owners, superusers and root users are allows you to delete or modify write protected files & directories. So first view its permissions using ls -l command.
$ ls -l filename -rwxrwxrwt ...
In the above output, the last bit ‘t’ is the sticky bit and if its value is t only then it is write protected. Otherwise, anyone can delete it.
2. Check Directory Permission
Next, you need to have write access to the directory (its parent directory) that allows you to modify or delete this file or directory.
$ ls -ld dirname
If you don’t have enough privileges, you can switch to superuser and then remove the file.
$ su - $ rm -f filename
3. Check i attribute
Finally, you need to check if the extended file system ‘i’ attribute is set or not, for this file. If your file or directory has this attribute set, then it cannot be modified, deleted, renamed or linked to. Only root users can add/remove this permission. You can use the lsattr command to check the value of this attribute.
$ lsattr filename
You will see the following type of output.
----i------------ filename
For example, the above output indicates that the ‘i’ attribute is set for it. In such cases, you can use chattr -i command to remove the file’s ‘i’ attribute. But please note, only the root user will be able to run this command. Once you have removed the ‘i’ attribute for a file or directory, it can be deleted by other users also.
$ chattr -i filename $ rm -f filename
In this article, we have learnt how to delete write protected file in Linux.
Also read:
How to Suspend Process in Linux
How to Run Linux Commands Without Logging in History
How to Get Column Names in Pandas DataFrame
How to Change NGINX AutoIndex
How to Manage User Password Expiry & Aging in Linux
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.