I have a list of sequentially numbered files and I would like to combine them while preserving their sequential ordering.

Example Input:

foo_0.txt
foo_10.txt
foo_11.txt
foo_12.txt
foo_1.txt
foo_2.txt
foo_3.txt
foo_4.txt
foo_5.txt
foo_6.txt
foo_7.txt
foo_8.txt
foo_9.txt

The process can be broken into three steps:

  • List the files which we want to combine.
  • Sort the listed files by their version and not name, this preserves the ordering.
  • Cat sorted files into a single file.
ls foo*.txt |sort --version-sort | xargs cat > foo_all.txt

To combine files from a specific sequence the following can be used

for i in {1..12}; do cat foo_$i.txt>>foo_all.txt; done