Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've got 14 files all being parts of one text. I'd like to merge them into one. How to do that?

share|improve this question
add comment

4 Answers

up vote 21 down vote accepted

This is technically what cat ("concatenate") is supposed to do, even though most people just use it for outputting files to stdout. If you give it multiple filenames it will output them all sequentially, and then you can redirect that into a new file; in the case of all files just use * (or /path/to/directory/* if you're not in the directory already) and your shell will expand it to all the filenames

$ cat * > merged-file
share|improve this answer
Beware that your quoted command will probably only do what the poster wants if they're numbered in such a way that the shell expands * in "natural" order. If you have "file1.txt...file9.txt...file14.txt" it won't work because file1?.txt will sort between file1.txt and file2.txt. You'd have to rename them to "file01.txt...file09.txt...file14.txt". Say echo * if you're not sure. –  Warren Young Nov 4 '10 at 21:43
@Warren: good point (or you can use zsh and set its numeric_glob_sort option). –  Gilles Nov 4 '10 at 23:04
@warren-young a correct, useful warning comment. But in my actual case the order makes no difference (because files contain just simple SQL statements inserting data records which have no dependencies). –  Ivan Nov 4 '10 at 23:16
add comment

The command

$ cat * > merged-file

actually has the undesired side-effect of including 'merged-file' in the concatenation, creating a run-away file. To get round this, either write the merged file to a different directory;

$ cat * > ../merged-file

or use a pattern match that will ignore the merged file;

$ cat *.txt > merged-file
share|improve this answer
cat * > merged-file works fine. Globs are processed before the file is created. If merged-file already exists, cat (mine at least) will detect that it's the output file and refuse to read it. IF the file already exists AND you have the redirect later in the pipeline, then it obviously can't do that, so then and only then do you get the runaway file. –  Kevin Feb 21 '12 at 22:48
add comment

If your files aren't in the same directory, you can use the find command before the concatenation:

find /path/to/directory/ -name *.csv -print0 | xargs -0 -I file cat file > merged.file

Very useful when your files are already ordered and you want to merge them to analyze them.

share|improve this answer
add comment

If this is just a one off merge, you can use an online tool such as:


EDIT: Why was this downvoted? It is a perfectly valid and useful answer to the question

share|improve this answer
This is a Unix/Linux site; using a website to merge files is slightly ridiculous –  Michael Mrozek May 17 '13 at 6:02
Apart taking several orders of magnitude longer to merge a few files that way, people inexperienced enough to use such a website instead of the tools provided by their OS, would probably also not realise they would publish the content of their files to the website's owner. Now let me merge those 5 files with credit card info into one big one, that would be easier to manage.... So the answer is not deemed useful (and that is what downvoting means, just hover your mouse over the downarrow), that has nothing to do with being valid or not. –  Anthon May 17 '13 at 6:17
The answer is both VALID and USEFUL. "Ridiculous" to you could be "thinking outside the box" to others. (processing time + learning time) > (time taken to use online tool). Surely you would agree >99% of people who are interested in learning "cat *.txt" commands, know that they have to be careful with anything they upload to the internet. While the answer won't be useful to everyone, I believe it is valid and useful to the vast majority of readers. –  StackMan May 17 '13 at 7:47
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.