
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.
Last updated: July 6, 2024
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.
tee is a command meant to read from standard input and write to standard output and files.
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:
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.
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:
This command makes multiple copies of the git.txt file’s contents without executing separate copy commands for each file destination.
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.
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.
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:
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.
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:
So, this command creates four copies of git.txt similar to the ones created by the for loop.
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.