Split a file into two or more files

The split command splits a file into two or more files. You need to specify two parameters:

  • the prefix of the output file, to which an alphabetic code (aa, ab…) is added.
  • the size of the individual files.

You can specify the size of the output files in two ways:

  • by bytes – the -b option breaks the file into pieces of the size defined in bytes.
  • by lines – the -l option breaks the file into pieces with that many lines.

Consider this example. We have created a file called file.txt:

linux example file for the split command

We can split that file by bytes:

linux split command by bytes

In the example above you can see that we have splitted the file file.txt by bytes (55 in this case). The prefix specified was new_file_. You can see that the split command created two files, new_file_aa and new_file_ab.

To split by lines, use the -l option:

linux split command by lines

In the example above we have first numbered the lines using the nl command. We’ve then splitted the file into pieces with one line each.

Subscribe to our Newsletter

Like us on Facebook

top
Geek University 2019