How can I concatenate two files in Unix?

47

10

How can I create a new file "new.txt" that is a concatenation of "file1.txt" and "file2.txt" in Unix?

shanmuga sundhari

Posted 2011-01-04T09:13:34.387

Reputation:

zcat file1.txt.gz > new.txt for gzip files – frops – 2015-12-18T14:47:25.897

Answers

75

cat file1.txt file2.txt > new.txt

Nathan Fellman

Posted 2011-01-04T09:13:34.387

Reputation: 8 152

7cat actually means concatenate. – user1686 – 2011-01-04T13:44:54.033

3I don't even know how to use Linux and I knew this. Sounds like a homework question to me :) – Shinrai – 2011-01-04T21:11:19.957

Helpful addition: With a ">" the target-file is overwritten with the source-files and with ">>" the source-files are appended to the target-file – None – 2011-01-04T23:14:43.687

11Bash, ksh, zsh: cat file{1,2}.txt > new.txt – Paused until further notice. – 2011-01-04T23:30:53.530

15

if the file new.txt is an empty file, you can simply use the cat command :

cat file1.txt file2.txt > new.txt

if new.txt is not empty, and you want to keep its content as it is, and just want to append the concatenated output of two files into it then use this:

cat file1.txt file2.txt >> new.txt

dig_123

Posted 2011-01-04T09:13:34.387

Reputation: 466

6

If you want to append two or more files to an existing file without overwriting the file's (file4.txt) content, then below is an example:

cat file1.txt file2.txt file3.txt >> file4.txt

Even if the file file4.txt is not present, it would get created. If it is present, the other files' contents would get appended to it.

Shazmeen Pathan

Posted 2011-01-04T09:13:34.387

Reputation: 69