In this bash article, we will learn how to overwrite a file in Linux. To do that, we will learn different methods and Linux commands to overwrite a file in bash using Linux operating system.
Before we start, we must understand what overwriting a file means in Linux.
Different Ways to Overwrite a File in Linux
Overwriting refers to the act of totally replacing one implementation with another. To overwrite anything is to substitute it with something else, obliterating the original.
Using the Linux system, we often need to overwrite and delete file contents. So, let’s learn various approaches to that.
> Symbol to Overwrite a File
>> are used for two different operations. The single greater than the
> operator empties and overwrites the specified file, whereas the
>> operator adds lines to the end of the provided file.
So, we will be using
> for overwriting our file. In the example below, we have used
echo with the
> operator to overwrite the existing file with
echo "abid" > 'Users/Name/Desktop/Namefile.txt'
cp Command to Overwrite a File Without Confirmation
One of the often used commands on Linux and other Linux operating systems for copying files and directories is the
cp command, which stands for a copy.
Here, we will see how to use Linux’s
cp command to force an unconfirmed overwrite of a copy operation. When we use the
cp command, it usually overwrites the target file(s) or directory, as illustrated.
Below is the example of the
cp command typically overwriting the targeted directories and files.
$ cp file.c bak
-i Flag to Overwrite a File to Add Interactive Prompt
We can use the
-i option and click
y if we want to overwrite and add an interactive prompt. Examine the example below:
$ cp -i file.c bak
This line of code brings an interactive prompt while overwriting the file:
cp: overwrite 'bak/file.c'? y
We can also overwrite the file without an interactive prompt. See the example below:
$ \cp file.c bak
chmod Command to Overwrite a Read-Only File
We can overwrite any file in two situations: when you have administrative access to the document’s properties or when you don’t. Take into account the following fixes for the issue.
In Linux, we can use the
chmod command to change a file’s properties, and it has the following short command:
$ chmod [refrence] [operator] [mode] file.txt
shred Command to Overwrite a File
shred command is used to erase data and devices securely.
This command overwrites a file to hide its contents and, optionally, deletes it, making it impossible for any program in the Linux/Unix system to retrieve the file.
We use the
rm command in the terminal to delete files from the system. Files removed with the
rm command may be recovered using the software.
However, files removed using the
shred command are unrecoverable since the
shred command overwrites the files three times with various patterns.
In the Linux/Unix system, by using the terminal, as demonstrated below, we can use the
shred command to overwrite the file’s entries and declare them unrecoverable.
$ shred file.txt