I'm trying, using bash, to merge the content of a list of files (more than 1K) into a big file.
I've tried the following cat command:
cat * >> bigfile.txt
however what this command does is merge everything, included also the things already merged.
e.g. file1.txt
content1
file2.txt
content2
file3.txt
content3
file4.txt
content4
bigfile.txt
content1
content2
content3
content2
content3
content4
content2
but I would like just
content1
content2
content3
content4
inside the .txt file
The other way would be cat file1.txt file2.txt ...
and so on... but I cannot do it for more than 1k files!
Thank you for your support!
Two quick options for combining text files. Open the two files you want to merge. Select all text (Command+A/Ctrl+A) from one document, then paste it into the new document (Command+V/Ctrl+V). Repeat steps for the second document. This will finish combining the text of both documents into one.
Type the cat command followed by the file or files you want to add to the end of an existing file. Then, type two output redirection symbols ( >> ) followed by the name of the existing file you want to add to.
To merge lines of files, we use the paste command in the Linux system. The paste command is used to combine files horizontally by outputting lines consisting of the sequentially corresponding lines from each FILE, separated by TABs to the standard output.
If you want to combine multiple large files, then instead of specifying each file's name to be concatenated, use the wildcards to identify these files, followed by an output redirection symbol. Hence, it is possible to concatenate all the files in the current directory using an asterisk (*) symbol wildcard as: cat *.
The problem is that you put bigfile
in the same directory, hence making it part of *
. So something like
cat dir/* > bigfile
should just work as you want it, with your fileN.txt
files located in dir/
You can keep the output file in the same directory, you just have to be a bit more sophisticated than *
:
shopt -s extglob
cat !(bigfile.txt) > bigfile.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With