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:
You can alter this permission if you are the file owner by adding the following command:
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 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.
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:
To give yourself all the permissions related to the file, you can use edit sudoers through the following command:
The following image illustrates that you have the admin privileges as a sudoer, root, and admin access:
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.
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.