1. Overview

In Linux, duplicating files has a variety of use cases like generating large datasets for testing and creating backups. Although there are multiple ways to complete this task, simplicity and efficiency are important factors to consider in these approaches.

In this tutorial, we’ll explore how to quickly duplicate a file n times in the shell.

2. Using the tee Command

tee is a command meant to read from standard input and write to standard output and files.

2.1. Using tee With Shell Redirection

In this first approach, we utilize tee with shell redirection to create multiple copies of a file while simultaneously saving the output to another file:

$ tee filecopy{1..4}.txt < git.txt >/dev/null

Here’s the breakdown:

  • filecopy{1..4}.txt – this represents brace expansion syntax in Bash. To clarify, it expands to filecopy1.txt, filecopy2.txt, filecopy3.txt, and filecopy4.txt specifying four output files.
  • < git.txt – redirects the contents of the file git.txt to the standard input of the tee command
  • >/dev/null – this redirects the standard output of the tee command to >/dev/null. This file /dev/null file is meant to discard all data written to itHence, the output that the tee command would normally display in the terminal is discarded and not displayed.

This command helps when we want to copy the content of one file to multiple files and prevent the display of output in the terminal. It includes both file creation and content redirection.

2.2. Combining tee With the cat Command

Using the cat command helps display the content of a file. However, by combining cat with the tee command, we can simultaneously display the content of a file and copy it to multiple files:

$ cat git.txt | tee filecopy1.txt filecopy2.txt filecopy3.txt

Let’s discuss the command above:

  • cat git.txt – displays the contents of the git.txt file in the terminal
  • | – takes the output of the cat command and sends it as input to the tee command
  • tee – receives this input and sends it to the standard output and also the specified files, namely filecopy1.txt, filecopy2.txt and filecopy3.txt

This command makes multiple copies of the git.txt file’s contents without executing separate copy commands for each file destination.

3. Using the cp Command

cp is a command useful for creating copies of files and directories. In this scenario, we’ll use it to create multiple copies of a specified file.

3.1. Manual Iteration

This approach involves manually iterating with the cp command until the desired copies are created:

$ cp git.txt filecopy1.txt

The command above copies the contents of the git.txt file into a new file named filecopy1.txt. If filecopy1.txt exists, it will be overwritten, and if it’s absent, a new file is created. So, if we want to duplicate the git.txt file n times, we repeat the command above n times.

However, we’ll change the number after filecopy to indicate the iteration number. To understand further, if n represents the number of times we want to repeat the copying process, we’ll have filecopy1.txt to filecopyn.txt.

3.2. Automatic Iteration

The previous approach is only efficient when dealing with a few files. Let’s modify it by introducing a for loop to remove repetition:

$ for ((i=1; i<=4; i++)); do cp git.txt "filecopy$i.txt"; done

Let’s break down the command in detail:

  • for ((i=1; i<=4; i++)); do – this command initiates a for loop by defining the initial value of the variable i to 1. This loop proceeds if i is less than or equal to 4. Also, after each iteration, i increments by 1.
  • cp git.txt “filecopy$i.txt”; – represents the body of the loop. It utilizes the cp command to copy the contents of the git.txt file to a new file with a name created using the value of the variable i. For instance, in the first iteration, it copies git.txt to a file named filecopy1.txt.
  • done – defines the end of the loop

Here, the loop runs four times, copying the contents of git.txt to four files, namely, filecopy1.txt, filecopy2.txt, filecopy3.txt, and filecopy4.txt. Furthermore, the for loop enables us to automate the task at hand by minimizing the manual effort. As a result, this improves the efficiency.

3.3. Combining xargs With the cp Command

Another quick and efficient approach is using the xargs command in combination with the cp command:

$ seq 4 | xargs -I{} cp git.txt filecopy{}.txt

Let’s see a breakdown of the command:

  • seq 4 – this command generates a sequence of numbers from 1 to 4
  • | – the pipe symbol takes the output of the command to its left, passing it as input to the command on its right
  • xargs -I{} – reads items from the standard input, in this case, the sequence of numbers generated by seq, and executes the specified cp command with those items. The -I{} option is a placeholder for each number received from the standard input.
  • cp git.txt filecopy{}.txt – this is the command that xargs executes for each item read from seq. {} is replaced by an item from the sequence of numbers generated by seq.

So, this command creates four copies of git.txt similar to the ones created by the for loop.

4. Conclusion

In this article, we explored various approaches to duplicate files in Linux quickly using shell commands.

First, we discussed using the tee command. In particular, we demonstrated how to create multiple copies using tee along with brace expansion syntax and shell redirection. Next, we explored the cp command. Here, we described utilizing manual iteration or leveraging a for loop for automatic iteration. Additionally, we introduced a combination of the cp, seq, and xargs commands to complete the same task. One can select each approach depending on personal preference, use case, and the level of automation required.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.