Linux中split大文件分割和cat合並文件


當需要將較大的數據上傳到服務器,或從服務器下載較大的日志文件時,往往會因為網絡或其它原因而導致傳輸中斷而不得不重新傳輸。這種情況下,可以先將大文件分割成小文件后分批傳輸,傳完后再合並文件。

1.分割 -- split命令

可以指定按行數分割和按字節大小分割兩種模式。

(1) 按行數分割

$ split -l 300 large_file.txt new_file_prefix

加上-d,使用數字后綴;加上--verbose,顯示分割進度:

$ split -l50000 -d large_file.txt part_ --verbose

(2) 按字節大小分割

$ split -b 10m large_file.log new_file_prefix

 

2.合並 -- cat命令

$ cat part_* > merge_file.txt

 

[注] split命令語法:

 split --h
Usage: split [OPTION]... [FILE [PREFIX]]
Output pieces of FILE to PREFIXaa, PREFIXab, ...;
default size is 1000 lines, and default PREFIX is 'x'.

With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -a, --suffix-length=N   generate suffixes of length N (default 2)            后綴名稱的長度 (默認為2) 
      --additional-suffix=SUFFIX  append an additional SUFFIX to file names
  -b, --bytes=SIZE        put SIZE bytes per output file                       每個輸出文件的字節大小
  -C, --line-bytes=SIZE   put at most SIZE bytes of records per output file    每個輸出文件每行的最大字節大小
  -d                      use numeric suffixes starting at 0, not alphabetic   使用數字后綴代替字母后綴
      --numeric-suffixes[=FROM]  same as -d, but allow setting the start value
  -e, --elide-empty-files  do not generate empty output files with '-n'        不產生空的輸出文件
      --filter=COMMAND    write to shell COMMAND; file name is $FILE           寫入到shell命令行
  -l, --lines=NUMBER      put NUMBER lines/records per output file             設定每個輸出文件的行數,默認行數是1000行
  -n, --number=CHUNKS     generate CHUNKS output files; see explanation below  產生chunks文件
  -t, --separator=SEP     use SEP instead of newline as the record separator;  使用新字符分割
                            '\0' (zero) specifies the NUL character
  -u, --unbuffered        immediately copy input to output with '-n r/...'     無需緩存
      --verbose           print a diagnostic just before each                  顯示分割進度
                            output file is opened
      --help     display this help and exit                                    顯示幫助信息
      --version  output version information and exit                           顯示版本信息

The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).

CHUNKS may be:
  N       split into N files based on size of input
  K/N     output Kth of N to stdout
  l/N     split into N files without splitting lines/records
  l/K/N   output Kth of N to stdout without splitting lines/records
  r/N     like 'l' but use round robin distribution
  r/K/N   likewise but only output Kth of N to stdout

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/split>
or available locally via: info '(coreutils) split invocation'

cat命令語法:

$ cat --h
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s) to standard output.

With no FILE, or when FILE is -, read standard input.

  -A, --show-all           equivalent to -vET
  -b, --number-nonblank    number nonempty output lines, overrides -n
  -e                       equivalent to -vE
  -E, --show-ends          display $ at end of each line
  -n, --number             number all output lines
  -s, --squeeze-blank      suppress repeated empty output lines
  -t                       equivalent to -vT
  -T, --show-tabs          display TAB characters as ^I
  -u                       (ignored)
  -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB
      --help     display this help and exit
      --version  output version information and exit

Examples:
  cat f - g  Output f's contents, then standard input, then g's contents.
  cat        Copy standard input to standard output.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/cat>
or available locally via: info '(coreutils) cat invocation'

功能說明:切割文件。

  語  法:split [--help][--version][-<行數>][-b <字節>][-C <字節>][-l <行數>][要切割的文件][輸出文件名]

  補充說明:split可將文件切成較小的文件,預設每1000行會切成一個小文件。

  參  數:

  -<行數>或-l<行數>  指定每多少行就要切成一個小文件。

  -b<字節>  指定每多少字就要切成一個小文件。支持單位:m,k

  -C<字節>  與-b參數類似,但切割時盡量維持每行的完整性。

  --help  顯示幫助。

  --version  顯示版本信息。

  [輸出文件名]  設置切割后文件的前置文件名,split會自動在前置文件名后再加上編號。

  使用例子:

    split -b 100m 1111.log (按照字節分隔) split -l 1000000 1111.log(按照行數分隔)

1. 分割文件

文件分割可以使用split命令,該即支持文本文件分割,又支持二進制文件分割;而合並文件可以使用cat命令。

1.1 文本文件分割

分割文本文件時,可以按文件大小分割,也可以按文本行數分割。

按文件大小分割

按文件大小分割文件時,需要以-C參數指定分割后的文件大小:

1
$ split -C 100M large_file.txt stxt

如上所示,我們將大文件large_file.txt按100M大小進行分割,並指定了分割后文件前綴stxt;當不指定前綴時,split會自動對分割文件進行命名,一般會以x開頭。

按行分割

文本文件還可以以行為單位進行分割,以行數進行分割時會忽略文件大小,並以-l參數指定分割后文件的行數:

1
$ split -l 1000 large_file.txt stxt

1.2 二進制文件分割

二進制文件分割類似於按大小分割文本文件,不同的是以-b參數來指定分割后的文件大小:

1
$ split -b 100M data.bak sdata

2. 文件合並

文件合並使用cat命令,上面幾種方式分割的文件都可以使用cat命令合並。

cat命令合並分割文件:

1
$ cat stxt* > new_file.txt

3. 命令格式

3.1 split命令說明

split命令格式如下:

split [選項]... [要切割的文件 [輸出文件前綴]]

命令參數

-a, --suffix-length=N   使用長度為 N 的后綴 (默認 2)

-b, --bytes=SIZE        設置輸出文件的大小。支持單位:m,k

-C, --line-bytes=SIZE   設置輸出文件的最大行數。與 -b 類似,但會盡量維持每行的完整性

-d, --numeric-suffixes  使用數字后綴代替字母

-l, --lines=NUMBER      設備輸出文件的行數

    --help     顯示版本信息

    --version  輸出版本信息

3.2 cat命令說明

cat是Linux下使用頻率較高的命令之一,該令詳細介紹:

cat連接文件並打印到標准輸出設備上

cat命令的常見使用場景有:

顯示文件內容:

1
$ cat filename

創建一個空文件:

1
$ cat > filename

文件合並:

1
$ cat file1 file2 > file


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM