Combine multiple txts into one
Solution 1:
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.
Solution 2:
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.
Solution 3:
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
.
Solution 4:
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
etc.
You would have to rename files as 00001, 00002 and so on.