Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Introduction

Copying files in Linux serves various purposes. It allows data manipulation or transformation without altering the original. Additionally, it supports system configuration by replicating settings across environments. It’s also crucial for data migration, sharing files between users or systems, and automating tasks through scripts. Overall, copying files ensures data safety, facilitates efficient management, and supports seamless operations in system administration and automation.

In this tutorial, we’ll use Linux commands to copy data from one file to another in the shell script.

First, we’ll start by exploring the cp command. Next, we’ll examine the sed, awk, head, and dd commands to achieve similar results. Lastly, we’ll look at how a while loop with the read command can be used to replicate data from one file to another.

2. Using the cp Command

To begin with, we started by looking at the cp command in Linux to duplicate files or directories. This command is widely used to create backups or to copy files to different locations. The script copies a file named source.txt and creates a duplicate named destination.txt.

The cp command in Linux is a powerful utility that allows users to copy files or directories. By default, it overwrites existing files without prompting and maintains the original content of the source file in the destination.

Now, let’s view the code to copy the file:

$ cp source.txt destination.txt

After executing this script, the destination.txt file becomes an exact duplicate of source.txt. The cp command transfers all contents from source.txt to destination.txt without any modifications, ensuring an exact copy.

3. Using sed Command

Another method to duplicate files is using the sed command. The sed command, short for stream editor, is a versatile tool in Linux for text manipulation. It’s commonly used to perform text transformations such as search and replace, deletion, insertion, and reformatting directly within a file or input stream.

Although sed is primarily used for editing text, it can also be utilized to process and output file contents. In this case, we leverage sed to duplicate the content of a file.

Next, let’s look into the code and view its working:

$ sed '' source.txt > destination.txt

The sed command reads the contents of source.txt without making any changes because no editing commands are specified. Furthermore, the > operator redirects the output from the terminal to a new file named destination.txt. Consequently, if destination.txt already exists, it overwrites the file with the contents of source.txt; otherwise, it creates a new file to store the output.

This script uses sed to process the content of source.txt and redirect it to a new file named destination.txt. The command essentially copies the entire content of source.txt into destination.txt without making any modifications.

4. Using the awk Command

Alternatively, we can achieve the same outcome of duplicating file content by using the awk command.

The awk command is a versatile text-processing tool in Linux, designed to extract, transform, and manipulate data from structured text files. Furthermore, it allows users to process fields and rows efficiently, making it a popular choice for tasks involving data parsing and transformation.

Now, let’s dive into the code and see how it operates:

$ awk '{ print }' source.txt > destination.txt

The awk command reads the source.txt file line by line, processing each line sequentially. The action block { print } ensures that each line is output exactly as it appears in the source file without any modifications. After that, the > operator then redirects this output to destination.txt. Hence, overwriting the file if it already exists or creating a new one if it does not.

This script utilizes the awk command to read the contents of source.txt and output them unchanged to a new file named destination.txt. Thus, the command acts as a simple text copy operation, leveraging awk to process the input file.

5. Using the head Command

A simple way to duplicate the contents of a file is by utilizing the head command in Linux.

The head command is commonly used to display the initial lines of a file. However, with specific options, it can process the entire file and redirect its contents to another file without modifications.

Let’s explore the code and understand how it works:

$ head -n -0 source.txt > destination.txt

Initially, this script uses the head command to read all lines from source.txt and save them into destination.txt. The head command processes the entire content of source.txt by using the -n -0 option. Next the > operator then redirects this output to destination.txt, overwriting the file if it already exists. In addition, create a new file if it does not.

6. Using the dd Command

An alternative method to copy file contents in Linux is by using the dd command.

The dd command provides versatile functionality, primarily copying and converting data at a low level. Users often employ it to create backups, transfer data between files, and handle raw disk images.

Let’s examine the code and see how it works:

$ dd if=source.txt of=destination.txt status=none

This script utilizes the dd command to copy data from source.txt to destination.txt without displaying any progress information. The dd command reads the content of source.txt as specified by the if=source.txt option and then writes this content into destination.txt using the of=destination.txt option. The status=none option ensures that no progress or status information is displayed during the process, allowing the operation to run silently.

7. Using a while Loop for File Copying

Similarly, another approach to copy the contents of one file to another in Linux is by utilizing a while loop. Hence, this method allows for reading each line from the source file and writing it to the destination file, offering more flexibility during the process.

Let’s examine the code and its functionality:

$ while read -r line; do
    echo "$line" >> destination.txt
done < source.txt

This script uses a while loop to process each line in source.txt and append it to destination.txt. The command utilizes a while loop to read the file source.txt line by line.

The -r flag in the read command ensures that the system interprets backslashes instead of escape characters. Additionally, the command echo “$line” >> destination.txt appends each line directly to destination.txt within the loop. The >> operator adds new content seamlessly without overwriting the existing data.

Furthermore, the loop processes all lines in source.txt as specified by the done < source.txt part, which indicates that the input comes from source.txt. This method enables line-by-line processing and ensures efficient copying of the contents from source.txt to destination.txt.

8. Conclusion

In this tutorial, we demonstrated how Linux commands were used to copy data from one file to another in a shell script.

We began by exploring the cp command and then examined the sed, awk, head, and dd commands to achieve similar results. Finally, we reviewed how a while loop with the read command was used to replicate data from one file to another.