Wednesday, 18 July 2018

Split Command Examples in Unix / Linux

The Split command in unix or linux operating system splits a file into many pieces (multiple files). We can split a file based on the number of lines or bytes. We will see how to use the split command with an example.

Split Command, Unix Command, Linux Command, LPI Study Materials

As an example, let’s take the below text file as the source file which we want to split:

> cat textfile
unix linux os
windows mac os
linux environment

There are three lines in that file and the size of the file is 47 bytes.

Split Command Examples:


1. Splitting file on number of lines.


The Split command has an option -l to split the file based on the number of lines. Let say i want to split the text file with number of lines in each file as 2. The split command for this is

split -l2 textfile

The new files created are xaa and xab. Always the newly created (partitioned) file names start with x. We will see the contents of these files by doing a cat operation.

> cat xaa
unix linux os
windows mac os

> cat xab
linux environment

As there only three lines in the source file we got only one line in the last created file.

2. Splitting file on the number of bytes


We can use the -b option to specify the number of bytes that each partitioned file should contains. As an example we will split the source files on 10 bytes as

split -b10 textfile

The files created are xaa, xab, xac, xad, xae. The first four files contain 10 bytes and the last file contains 7 bytes as the source file size is 47 bytes.

3. Changing the newly created file names from character sequences to numeric sequences.


So far we have seen that the newly created file names are created in character sequences like xaa, Xab and so on. We can change this to numeric sequence by using the -d option as

split -l2 -d textfile

The names of the new files created are x00 and x01.

4. Changing the number of digits in the sequence of filenames.


In the above example, you can observe that the sequences have two digits (00 and 01) in the file names. You can change the number of digits in the sequence by using the -a option as

split -l2 -d -a3 textfile

Now the files created are x000 and x001

Related Posts

0 comments:

Post a Comment