Paste side by side multiple files by numerical order

user1687130 picture user1687130 · Jun 12, 2013 · Viewed 10.4k times · Source

I have many files in a directory with similar file names like file1, file2, file3, file4, file5, ..... , file1000. They are of the same dimension, and each one of them has 5 columns and 2000 lines. I want to paste them all together side by side in a numerical order into one large file, so the final large file should have 5000 columns and 2000 lines.

I tried

for x in $(seq 1 1000); do 
paste `echo -n "file$x "` > largefile
done

Instead of writing all file names in the command line, is there a way I can paste those files in a numerical order (file1, file2, file3, file4, file5, ..., file10, file11, ..., file1000)?

for example:

file1

1 1 1 1 1
1 1 1 1 1 
1 1 1 1 1
...

file2

2 2 2 2 2 
2 2 2 2 2
2 2 2 2 2 
....

file 3

3 3 3 3 3 
3 3 3 3 3 
3 3 3 3 3
....

paste file1 file2 file3 .... file 1000 > largefile

largefile

1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
....

Thanks.

Answer

glenn jackman picture glenn jackman · Jun 12, 2013

If your current shell is bash: paste -d " " file{1..1000}