Linux Commands

RM Remove Write-Protected Regular File

The files on any operating system have certain permissions associated with them. These permissions are mainly related to file access. The three most commonly used file permissions are “read”, “write”, and “execute”. However, at times, we have some files that are intentionally kept write-protected so that their contents cannot be modified. It is quite challenging to remove such files from your system if you ever want to do so. Therefore, in this guide, we will walk you through the complete procedure of removing a “write-protected” regular file by using the “rm” command on a Linux Mint 20.3 system.

Removing a File That Is Not Write-Protected

First, we will attempt to remove a file that is not write-protected just to see the difference between its removal and the removal of a write-protected file. We have a file named “test.txt” in our Home directory that is not write-protected. This file is shown in the following image:

We will attempt to remove this file by using the command shown below:

$ rm test.txt

This file will be removed very easily without displaying any output on the terminal as shown in the following image:

For confirming if the specified file has been successfully removed or not, we will use the “ls” command to list the contents of our Home directory. You can confirm from the output of this command shown in the image below, that the “test.txt” file no longer exists in our Home directory.

Making a File Write-Protected

Now, we will create another file named “test.txt” in our Home directory. You can confirm that this file is present in our Home directory by looking at the following image:

To make this file write-protected, we will run the command shown below:

$ sudo chattr +i test.txt

This command will not display any output on the terminal.

To confirm if this file is write-protected, we will try to remove it with the following command:

$ rm test.txt

The output of this command shown in the image below, implies that this file is now write-protected and hence, it cannot be removed this way.

You can even try using the “-f” flag with the “rm” command for removing this file forcefully. However, you will still not be able to remove a write-protected file as shown in the following image:

Removing a Write-Protected Regular File

The only thing that we can do to remove the said file is by changing its permissions with the command shown below:

$ sudo chattr –i test.txt

This command will simply remove the “write-protected” restriction from this file.

Now, we will remove it with the following command:

$ rm test.txt

This time, this command will be executed successfully without displaying any error message as shown in the image below:

For confirming if the specified file has been removed successfully or not, we will make use of the “ls” command. You can confirm from its output shown in the following image that the file “test.txt” no longer exists in our Home directory.

Conclusion

By using this guide, you can easily attempt to remove any of your write-protected files while working on a Linux Mint 20.3 system. All you need to do is to change the permissions of this file by following the prescribed method and then you can simply remove it through the regular way. Check out Linux Hint articles for more guides and tips.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.