Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

In Linux, sometimes large files need to be cut into several smaller files for transmission due to network transmission limitations. After the transmission is complete, the files are combined.

File cutting

Use the split command to split large files.

Syntax format

The split [- < number > lines] [to a] [b - byte < >] [-c < > byte] [-d] [-l < number > lines] [to cut the file] [output filename]Copy the code

Common parameters are described as follows:

  • Line - < number >: Specifies how many lines to cut into a small file
  • -a: Specifies the length of the output filename suffix (default: 2)
  • Byte - < b >: Specifies how many bytes are cut into a small file
  • Byte - C < >With parameters:-bSimilar, but try to maintain the integrity of each line when cutting
  • -d: Specifies that the suffix of the output file name is replaced by a number
  • -l: Specifies how many lines to cut into a small file

For detailed help information, run split –help.

Using the instance

Split the test.sh file into a file every 6 lines using the split command:

$ split -6 test.sh test/test_ 
Copy the code

After executing, a number of small files are generated in the./test directory, each of which contains only six lines:

Test_aa, ab, AC, AD, AE, AF, AG, ahCopy the code

File merging

You can merge multiple small files using the cat command.

Syntax format

cat [-options] filename
Copy the code

Common parameters are described as follows:

  • -n: Numbers all output lines starting from 1
  • -bAnd:-nSimilar, but not numbered for blank lines
  • -s: When there are more than two consecutive blank lines, replace with a blank line

Using the instance

Merge the small files cut above:

cat test_* > test.sh
Copy the code

In Linux, cutting and merging large files is very convenient.

Original is not easy, if small partners feel helpful, please click a “like” and then go ~

Finally, thank my girlfriend for her tolerance, understanding and support in work and life!