Linux Commands

How to Overwrite a Read-Only File in Linux

A read-only file implies that the user doesn’t have permission to edit or write. Specifically, the “w” permission would have been disabled for the file. When the file’s property has been set as read-only, the user would neither have permission to write(w) the file nor execute(x) the file and be represented as read-only(r).

Sometimes, it becomes tough for the user to overwrite a read-only file. That’s why we gathered and explained the different methods of overwriting a read-only file in Linux.

How to Overwrite a Read-Only File in Linux?

There are two conditions for overwriting any file – when you are the file owner and when you do not have any ownership access to the file’s properties. Let’s look at the following methods to resolve the issue:

Using the chmod Command

The chmod command is used to modify the properties of a file in Linux, and it has a simple command:

$ chmod [reference] [operator] [mode] file

You can alter this permission if you are the file owner by adding the following command:

$ chmod u+wx filename

Let’s take an example where we want to change and overwrite a read-only file located in the Documents directory. Either we can use the directory path in the command or change the current directory of the file:

$ chmod u+wx /home/user/Documents/Information.txt

Or

$ cd ~/Downloads

$ chmod u+wx Information.txt

Note: As you can see in the previous image, we used the -v option or the verbose option to show the resultant changes in the output.

While using this command, it is essential to recollect if you are the only current file user. This command only works if you are a part of the directory in the /etc/sudoers. This step indicates the configuration of the sudo properties of the file using the visudo command.

$ chmod u+wx filename

This command requires a password to gain superuser access to the file. If you want to get the root privileges, please add the “sudo” with the above command:

$ sudo chmod u+wx filename

To give yourself all the permissions related to the file, you can use edit sudoers through the following command:

$ sudo vi /etc/sudoers

The following image illustrates that you have the admin privileges as a sudoer, root, and admin access:

root ALL=(ALL : ALL) ALL

admin ALL=(ALL : ALL) ALL

sudo ALL=(ALL : ALL) ALL

If you do not have “ALL” access, please change it accordingly. Now, you have complete access to all the properties related to the file. And you can either read, write or execute the file without encountering any issues.

Conclusion

You can edit and overwrite a read-only file in Linux using multiple commands and sudo privileges. Even if you don’t have the sudo privileges, you can easily access the superuser benefits using sudo by accessing the root directories. We hope this article improved your knowledge of the comprehensiveness of overwriting a read-only file in Linux.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.