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

coding

当需要将较大的数据上传到服务器,或从服务器下载较大的日志文件时,往往会因为网络或其它原因而导致传输中断而不得不重新传输。这种情况下,可以先将大文件分割成小文件后分批传输,传完后再合并文件。

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

以上是 Linux中split大文件分割和cat合并文件 的全部内容, 来源链接: utcz.com/z/509776.html

回到顶部