1. Splitting files

You can use the split command to split files, which supports both text and binary file splitting, and the cat command to merge files.

1.1 Text file splitting

When splitting a text file, you can split it by file size or by number of lines of text.

Splitting by file size

When splitting a file by file size, you need to specify the split file size with the -C parameter.

$ split -C 100M large_file.txt stxt

As shown above, we split the large file large_file.txt by 100M size and specify the split file prefix stxt; when no prefix is specified, split will automatically name the split file, usually starting with x.

Split by line

Text files can also be split by lines. When splitting by lines, the file size is ignored and the number of lines in the split file is specified with the -l argument.

$ split -l 1000 large_file.txt stxt

1.2 Binary file splitting

Splitting a binary file is similar to splitting a text file by size, except that the -b argument specifies the size of the split file.

$ split -b 100M data.bak sdata

2. File merging

You can use the cat command to merge files that are split in the above ways.

The cat command merges split files.

$ cat stxt* > new_file.txt

3. Command Format

3.1 split command description

The split command has the following format.

split [option]... [file to cut [output file prefix]]

Command parameters

-a, --suffix-length=N Use a suffix of length N (default 2)
-b, --bytes=SIZE Set the size of the output file. Supported units: m,k
-C, --line-bytes=SIZE Set the maximum number of lines in the output file. Similar to -b, but will try to maintain the integrity of each line
-d, --numeric-suffixes Use numeric suffixes instead of letters
-l, --lines=NUMBER Number of lines in the device output file
    --help show version information
    --version Output version information

3.2 cat command description

Common usage scenarios for the cat command are

Displaying the contents of a file.

$ cat filename

Create an empty file.

$ cat > filename

File merge.

$ cat file1 file2 > file