100 Useful Command-Line Utilities

by Oliver; 2014

62. split

split splits up a file. For example, suppose that:
$ cat test.txt
1
2
3
4
5
6
7
8
9
10
If we want to split this file into sub-files with 3 lines each, we can use:
$ split -l 3 -d test.txt test_split_
where -l 3 specifies that we want 3 lines per file; -d specifies we want numeric suffixes; and test_split_ is the prefix of each of our sub-files. Let's examine the result of this command:
$ head test_split_*
==> test_split_00 <==
1
2
3

==> test_split_01 <==
4
5
6

==> test_split_02 <==
7
8
9

==> test_split_03 <==
10
Note that the last file doesn't have 3 lines because 10 is not divisible by 3—its line count equals the remainder.

Splitting files is an important technique for parallelizing computations.

<PREV   NEXT>