I have 1000 csv files in a directory. I would like to concatenate them all together in order. They are named img_size_1.csv to approx img_size_1000.csv This answer is close but assumes a list file. Can this be done in a one-liner?
Yes it can, with the unimaginatively named
$ cat *csv > all.csv
cat does what it says on the bottle, it conCATenates its input and prints to standard output. The command above will give an error if a file called
all.csv already exists in the target directory:
$ cat *csv > all.csv cat: all.csv: input file is output file
You can safely ignore that error, the contents of Apparently, on some systems (e.g. OSX according to the comments below this answer), you cannot ignore the error and this will enter a loop, catting
all.csv will be overwritten.
all.csv back into itself until you run out of disk space. If so, just delete
all.csv, if it exists, before running the command.
ls -1 *.csv | while read fn ; do cat "$fn" >> output.csv.file; done
If you want to concatenate them by alphabetic order :
ls -1 *.csv | sort | while read fn ; do cat "$fn" >> output.csv.file; done
If you want to concatenate them by time creation order :
ls -1t *.csv | while read fn ; do cat "$fn" >> output.csv.file; done