I'll show you two ways #
You can use the split
command to split by number of lines or number of bytes.
Let me show you how.
First example: auto split a single CSV file into multiple files of 200 lines each #
Let's say I have one .csv-file that I want to split into multiple files that contain a maximum of 200 lines. I can use the following command:
split -l 200 split_me
Where split_me.csv
is the filename of the .CSV file you wish to split into chunks of 200 lines. The split function itself is smart enough to give the new files a name, so no need to worry about that.
For many cases this alone is enough. But if you want some additional options, read on.
Example 2: split into files of 1MB each, and add a '.csv' extension to all of them #
Beware!
Make sure you execute this command in the RIGHT folder, or risk changing way more files than you’ve intended!
Let's make it more interesting:
-
-
I want to split a file of 6.4MB into files of 1MB each,
-
-
and I want each resulting file to also have the
.csv
extension.
For that, we need to expand the split command a little bit.
First we used the -l
flag, which separated the files by a number of lines. Now we're going to use the -b
flag, which separates by size (bytes). Splitting it in files of 1MB is easy, just use 1m
.
And additionally, we'll use a little loop and the mv
-command to change the extensions.
First: split into chunks of 1mb each:
split -b 1m split_me.csv
And next, add the .csv
extension to all of them:
for i in *; do mv "$i" "$i.csv"; done