Copy File Contents Multiple Times into One File on Linux
In this article
This guide shows you how to duplicate the contents of a file multiple times and combine them into a single output file on Linux. You'll learn several command-line methods that work for text files, configuration files, and even binary data. No special tools are needed beyond standard Linux utilities.
What You'll Need
- A Linux system with a terminal (any distribution works).
- Basic familiarity with the command line.
- The source file you want to duplicate (e.g.,
data.txt). - Write permissions in the directory where you'll create the output file.
Step-by-Step
- Open a terminal. Use your system's terminal emulator (e.g., GNOME Terminal, Konsole, or a plain TTY).
- Navigate to the directory containing your source file. Use
cd /path/to/directoryto change to the appropriate folder. - Verify the source file exists with
ls -l filename. Replacefilenamewith your actual file name. - Choose your method. The most common approaches are:
- Using
forloop withcat: This is the simplest and most portable method. Run:for i in {1..5}; do cat source.txt; done > output.txtThis copiessource.txt5 times intooutput.txt. - Using
yesandhead: For a large number of repetitions, this is efficient. Run:yes source.txt | head -n 5 | xargs cat > output.txt
This also copies the file 5 times. - Using
awk: If you prefer a single command, run:awk 'BEGIN{for(i=0;i<5;i++) while((getline line < "source.txt")>0) print line}' > output.txtThis duplicates the file 5 times without using external commands.
- Using
- Adjust the repetition count to your needs. Change the number in the loop (e.g.,
{1..10}for 10 copies) or the-nvalue in theheadcommand. - Run the command. Press Enter. The command will execute and create or overwrite the output file.
- Verify the output. Check the line count or file size to confirm the duplication worked. For example, if your source file has 100 lines, after 5 copies the output should have 500 lines. Use
wc -l output.txtto count lines. - Inspect the output with
head output.txtorcat output.txtto ensure the content looks correct.
Common Mistakes
- Overwriting the source file: If you set the output file to the same name as the source, you'll lose the original. Always use a different name for the output.
- Forgetting to redirect: Without
> output.txt, the content will print to the terminal instead of being saved. - Using
>>instead of>:>overwrites the output file each time;>>appends. If you use>>and run the command multiple times, you'll get extra copies. - Spacing errors in the loop: The
forloop syntax must have spaces around brackets and semicolons.for i in {1..5}; dois correct;for i in {1..5};domay fail. - Not quoting filenames with spaces: If your file name contains spaces, wrap it in quotes:
cat "my file.txt".
Tips
- Use a variable for the repetition count to make changes easier:
n=10; for i in $(seq 1 $n); do cat source.txt; done > output.txt
- For very large files or many repetitions, consider using
ddwithconv=appendin a loop, but thecatmethod is usually sufficient and faster. - To add a separator between copies (e.g., a blank line), use:
for i in {1..5}; do cat source.txt; echo; done > output.txt - If you need to duplicate binary files, the same commands work, but avoid adding text separators like
echobecause they will corrupt the binary data. - Check disk space before creating a huge output file with
df -h .to avoid filling your disk.
Troubleshooting
- Command not found error: If you see
cat: command not found, your system may have a minimal environment. Use/bin/cator install coreutils (e.g.,sudo apt install coreutilson Debian/Ubuntu). - Permission denied: If you get
Permission deniedwhen writing the output, you don't have write access to the directory. Usesudoonly if necessary, or change to a writable directory. - Output file is empty: This can happen if the source file is empty or if the command syntax is wrong. Check the source file with
wc -c source.txt. If it's empty, there's nothing to copy. - The output has fewer lines than expected: If you used
yesandhead, ensure-nmatches the number of copies you want. If you used a loop, check the range syntax. - Special characters in the file cause errors: If your file contains backslashes or quotes, the
awkmethod may interpret them. Use thecatloop instead, which treats content literally. - Binary file gets corrupted: When copying binary files, never use
echoor text processing. Usecatin a loop without any additional output.
Frequently Asked Questions
Can I copy a file a variable number of times?
Yes, use a variable in the loop: n=7; for i in $(seq 1 $n); do cat source.txt; done > output.txt.
What if I need to copy the file 1000 times?
Use yes source.txt | head -n 1000 | xargs cat > output.txt. This is efficient for large counts.
Does this work for binary files?
Yes, the cat loop works for binary files, but avoid adding any text separators. The output will be a concatenation of the binary data.
How do I append to an existing file instead of overwriting?
Use >> instead of >. For example: for i in {1..5}; do cat source.txt; done >> existing.txt.
Why does my output have extra blank lines?
If you used echo after cat, it adds a newline. Remove echo if you don't want separators.
Can I do this without creating a new file, just printing to screen?
Yes, omit the > output.txt part. The output will go to the terminal.