Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shell - cat - merge files content into one big file

Tags:

shell

cat

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!

like image 321
fabioln79 Avatar asked May 24 '12 12:05

fabioln79


People also ask

How do I combine the contents of a file?

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.

How do you combine files in cat command?

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.

How do I combine the contents of two files in Linux?

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.

How do I combine multiple text files into one in Linux?

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 *.


2 Answers

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/

like image 145
mvds Avatar answered Sep 19 '22 09:09

mvds


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
like image 37
glenn jackman Avatar answered Sep 21 '22 09:09

glenn jackman