How to Overwrite File in Bash

Abid Ullah Feb 02, 2024
How to Overwrite File in Bash

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.

Use the > Symbol to Overwrite a File

Remember that > and >> 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 abid.

Example Code:

echo "abid" > 'Users/Name/Desktop/Namefile.txt'

Use Force 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.

Example Code:

$ cp file.c bak

Use the -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:

Example Code:

$ 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:

Example Code:

$ \cp file.c bak

Use the 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:

Example Code:

$ chmod [refrence] [operator] [mode] file.txt

Use the shred Command to Overwrite a File

The 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.

Example Code:

$ shred file.txt
Author: Abid Ullah
Abid Ullah avatar Abid Ullah avatar

My name is Abid Ullah, and I am a software engineer. I love writing articles on programming, and my favorite topics are Python, PHP, JavaScript, and Linux. I tend to provide solutions to people in programming problems through my articles. I believe that I can bring a lot to you with my skills, experience, and qualification in technical writing.

LinkedIn

Related Article - Bash File