当前位置:网站首页>Split and merge in the command line

Split and merge in the command line

2020-12-07 08:57:32 linuxprobe2020

Backup files often involve the problem of large file transfer , Poor network quality or other problems often lead to transmission interruption , And had to retransmit . If you split the large file into several small files for transmission 、 The way to merge all files after all transfers are completed , It can often improve the transmission experience . This section will introduce  Linux  command File split and merge operations under the line .

 Split and merge on the command line split and merge on the command line

As an engineer , We often communicate with command Do business with , But except for the orders for work , Have you ever tried to replace every aspect of your life with orders ?

In order to let more fans can use the command line as their own effective tool ,Linux China specially invited three authors , Write the 《 Command line Survival Guide 》, I hope you can use this series of articles , Let's change your perspective , Look at the command line . If you want to experience a different command line , Let's take a look at today's 《 Command line Survival Guide 》.

Operation Overview

Backup files often involve the problem of large file transfer , Poor network quality or other problems often lead to transmission interruption , And had to retransmit . If you split the large file into several small files for transmission 、 The way to merge all files after all transfers are completed , It can often improve the transmission experience .

This section will introduce Linux Command line file splitting and merging operations .

Concepts and terms

Split , seeing the name of a thing one thinks of its function , Is to split a large file into several smaller files , After these small files are spliced in order, large files can be restored . Usually , Splitting according to file size is a common way ; But because the text file has the characteristics of line number , So in addition to splitting by size , It can also be split by the number of rows .

Merge , It is to splice several smaller files into a larger file . Usually you just need to connect in order , Relatively simple .

Usually , To ensure that the two files before splitting and after merging are the same , It needs to be verified .

Practical operation
Scene one : Use split Split files

plit yes Linux The file splitting utility provided by the environment , Supports both binary and text files . Usually it has been followed by coreutils Built into the system , No additional installation is required .

Split by size , Binary files and text files are split by size with different options :

  1. Binary files use -b Option to specify the size of the split file .
    Text files use -C Option to specify the size of the split file .

The usage of the two is similar , It only needs :
split [ Options ] [ size ] [ File to be split ] [ After the split prefix ( Optional )]
In order to debian-live-10.5.0-amd64-lxqt.iso( about 2.4 GB) Press 100MB The size is split as an example :

> split -b 100M debian-live-10.5.0-amd64-lxqt.iso debian-live-

You can see that the original file is split into 25 Smaller files , from aa To ay:

debian-live-aa  debian-live-ad  debian-live-ag  debian-live-aj  debian-live-am  debian-live-ap  debian-live-as  debian-live-av  debian-live-ay
debian-live-ab  debian-live-ae  debian-live-ah  debian-live-ak  debian-live-an  debian-live-aq  debian-live-at  debian-live-aw
debian-live-ac  debian-live-af  debian-live-ai  debian-live-al  debian-live-ao  debian-live-ar  debian-live-au  debian-live-ax
Scene two : Use cat Merge files

cat yes linux The file connection utility provided by the environment , Ability to connect files and output them to standard output . Usually it has been followed by coreutils Built into the system , No additional installation is required .

It is worth noting that , In order to ensure that the merged file is consistent with the original file , When conditions permit , It can be used diff Command to verify .

Use cat The merge file can be made of cat [ file ...] > [ The merged file name ] The format of , This method works for both binary and text files .

As before debian-live-aa To debian-live-ay For example , You can use this command when merging :

> cat debian-live-* > debian-live.iso

You can see that the merged debian-live.iso file .

debian-live-aa  debian-live-ad  debian-live-ag  debian-live-aj  debian-live-am  debian-live-ap  debian-live-as  debian-live-av  debian-live-ay
debian-live-ab  debian-live-ae  debian-live-ah  debian-live-ak  debian-live-an  debian-live-aq  debian-live-at  debian-live-aw  debian-live.iso
debian-live-ac  debian-live-af  debian-live-ai  debian-live-al  debian-live-ao  debian-live-ar  debian-live-au  debian-live-ax
Use diff Verification file

Just press diff [ The original document ] [ Present document ] It can be executed in the format of . If no output , It proves that there is no difference between the two documents .

In contrast to debian-live-10.5.0-amd64-lxqt.iso and debian-live.iso For example :

> diff debian-live-10.5.0-amd64-lxqt.iso debian-live.iso
> #  There is no output , It means that they are the same .
Small exercise

Try to split the log files in the system in different ways .

版权声明
本文为[linuxprobe2020]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/202012070855070412.html