I'm trying to combine multiple txts into a big file. I want them to be put in sequence but this seems not to work properly.

At this time I tried from the terminal:cat *.txt >merged.txt but this seems to concatenate the files randomly.

In my folder I have all the files named with sequential numbers (from 1.txt to 10000.txt). Am I missing anything?


Globbing isn't random, it's guaranteed to be alphabetical (a.k.a. lexicographic order according to your locale), which is different from numeric sorting order.

You can use brace expansion for this. Replace '10' with the number of the last file.

cat {1..10}.txt > merged.txt

This uses bash brace expansion, which you can read about at LESS='+/Brace Expansion' man bash.

Note that unlike file globs, the brace expansion will generate arbitrary strings which need not be existing files; in this case that means you will get errors if there are files missing from the sequence (e.g. if 7.txt does not exist). However, this won't affect the contents of merged.txt which will be produced as expected.

  • 1
    This is unnecessarily quadratic; cat $(for i in {1..10}; do echo "${i}.txt"; done) >> merged.txt is perfectly sufficient. – wchargin Oct 11 '17 at 0:02
  • 1
    You don't need a for loop; you need better file globbing (like zsh has). – Wildcard Oct 11 '17 at 0:10
  • @wchargin Thanks, that’s neater, I thought {1..10} was a zsh thing but I should have thought about how cat’s multiple arguments are how it’s designed to be used. – grg Oct 11 '17 at 5:31
  • 1
    Thanks; I've simplified the command in this answer, too, to be more idiomatic—hope you don't mind. :) This is how I would do it with bash; upvoted. – Wildcard Oct 11 '17 at 6:43
  • 1
    @Wildcard You're of course right that better globbing is superior to a for-loop; I just never thought of using brace expansion as a component to a larger word. Thanks for the improvement. – wchargin Oct 11 '17 at 11:36

Use zsh with extended globs—and enable numeric sorting of the glob expansions with the (n) glob qualifier:

zsh -c 'cat *.txt(n) > merged.txt'

You can get more specific with your globs, also:

zsh -c 'cat <1-10000>.txt(n) > merged.txt'

See man zshexpn for full details.

  • 3
    If you have the multios option set you can skip the cat and just use <*.txt(n) >merged.txt (or <*.txt~merged.txt(n) >merged.txt in case it already exists) – Kevin Oct 11 '17 at 3:09
  • It's highly unlikely, but what happens if the extension is a literal .txt(n)? – JAB Oct 11 '17 at 15:42
  • @JAB won't match. Try it. – Wildcard Oct 11 '17 at 16:04

Heads up. If the files are indeed numbered 1 - 10000 then the operating system is sorting your files in the order of the first number: 1.txt 11.txt 12.txt ... 19.txt 100.txt ... 2.txt 20.txt 21.txt


You would have to rename files as 00001, 00002 and so on.


Normally, you don't parse the output of ls, but as your filenames contain no whitespace or "unusual" characters, we can do this:

shopt -s extglob
ls +([0-9]).txt | sort -n | xargs cat > merged.txt

The +([0-9]) is a bash extended glob pattern to match only one or more numbers.

With GNU ls (available via homebrew in the coreutils package)

ls -v +([0-9]).txt | xargs cat > merged.txt

Another possible answer could be:

ls | sort -n | xargs cat > merged.txt

On a GNU/Linux system you can do ls -v | xargs cat > merged.txt, but this is specific to the GNU version of ls and does not work for BSD ls.

  • 1
    -1, ls -v on a Mac does something completely different from ls -v on Linux. Mac uses BSD tools and -v for numeric sorting of files is a feature of GNU ls only. For BSD ls, -v means Force unedited printing of non-graphic characters; this is the default when output is not to a terminal. – Wildcard Oct 11 '17 at 6:46
  • My bad, I'll update my answer to reflect that. – musicman523 Oct 12 '17 at 3:35

In command promt:
cd "path to directory" (Enter)
copy *.txt result.txt (Enter)

  • 1
    Not sure if that's a windows thing, but copy isn't even a command on macOS, and cp doesn't work like that. See the other answers. – Kevin Oct 11 '17 at 3:14
  • What @Kevin said...I don't recall using that method to concatenate files in Windows so I don't know how you came about that answer. – Allan Oct 11 '17 at 9:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .