Linux 大文件如何切割与合并?

简介: 往往是因为网络传输的限制,导致很多时候,我们需要在 Linux 系统下进行大文件的切割。这样将一个大文件切割成为多个小文件,进行传输,传输完毕之后进行合并即可。

往往是因为网络传输的限制,导致很多时候,我们需要在 Linux 系统下进行大文件的切割。这样将一个大文件切割成为多个小文件,进行传输,传输完毕之后进行合并即可。


Linux 大文件的分割与合并

Linux 学习–文件分割与合并

Linux下大文件切割与合并

1. 文件切割 - split

在 Linux 系统下使用 split 命令进行大文件切割很方便

[1] 命令语法

# -a: 指定输出文件名的后缀长度(默认为2个:aa,ab...)
# -d: 指定输出文件名的后缀用数字代替
# -l: 行数分割模式(指定每多少行切成一个小文件;默认行数是1000行)
# -b: 二进制分割模式(支持单位:k/m)
# -C: 文件大小分割模式(切割时尽量维持每行的完整性)
split [-a] [-d] [-l <行数>] [-b <字节>] [-C <字节>] [要切割的文件] [输出文件名]

[2] 使用实例

# 行切割文件
$ split -l 300000 users.sql /data/users_
# 使用数字后缀
$ split -d -l 300000 users.sql /data/users_
# 按字节大小分割
$ split -d -b 100m users.sql /data/users_

[3] 帮助信息

# 帮助信息
$ split --help
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             设定每个输出文件的行数
  -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'

2. 文件合并 - cat

在 Linux 系统下使用 cat 命令进行多个小文件的合并也很方便

[1] 命令语法

# -n: 显示行号
# -e: 以$字符作为每行的结尾
# -t: 显示TAB字符(^I)
cat [-n] [-e] [-t] [输出文件名]

[2] 使用实例

# 合并文件
$ cat /data/users_* > users.sql

[3] 帮助信息

# 帮助信息
$ 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'


相关文章
|
10天前
|
Linux
使用Linux命令`comm`进行文件比较
`comm`命令在Linux中用于比较两个已排序文件的差异和共同点。基本语法是`comm [OPTION]... FILE1 FILE2`。它输出三列:公共行、仅在第一个文件中的行和仅在第二个文件中的行。选项如`-1`, `-2`, `-3`用于隐藏相应列。注意,输入文件必须排序,且`comm`不考虑行内空白差异。此命令在文本处理和数据分析中非常有用。
|
5天前
|
Linux API C语言
C语言读写BMP文件-EasyBmp【 linux 平台】
**EasyBmp** 是一个49KB的轻量级C++图像处理库,专注于BMP格式,提供简单易用的API。它的特点是小巧、开源、易于理解和高度定制。通过示例代码展示了如何轻松读取、缩放和保存BMP图像。适合需要高效处理BMP图像的开发者。
|
6天前
|
Linux Shell
蓝易云 - Linux下查看文件和文件夹大小
这两个命令是查看Linux系统中文件和文件夹大小的基本工具,可以根据需要进行组合使用。
8 0
|
6天前
|
应用服务中间件 Linux nginx
蓝易云 - Linux使用pid文件结束nginx
以上就是使用pid文件结束nginx进程的方法。
7 0
|
7天前
|
缓存 Linux Windows
初识Linux操作系统(根目录下的重要文件)(命令提示符的含义)
Linux系统基于&quot;一切皆文件&quot;的理念,重要文件分布在如/root(root用户目录)、/home(普通用户目录)、/etc(应用配置)、/dev(设备文件)、/boot(内核及启动文件)、/proc(动态系统信息)、/lib64(库文件)、/opt(软件存放)、/tmp(临时文件)。&quot;[root@localhost ~]#&quot;代表管理员在root目录,&quot;$&quot;代表普通用户。创建新用户用`useradd`命令。调节终端字体大小:Ctrl+Shift++增大,Ctrl+减号缩小。绝对路径从根目录开始,相对路径从当前目录开始。
|
9天前
|
存储 缓存 Unix
【嵌入式软件工程师面经】Linux文件IO
【嵌入式软件工程师面经】Linux文件IO
13 1
|
9天前
|
Linux Python
在 Linux 中查看目录文件数的方法
【6月更文挑战第7天】在 Linux 中查看目录文件数的方法包括:使用 `ls` 命令结合 `wc -l`,`find` 命令,`tree` 命令,以及编程方式(如 Python)。`ls` 和 `find` 命令通过管道传递给 `wc -l` 统计行数,而 `tree` 命令提供树状视图并显示文件数。编程方式则允许自定义实现,例如 Python 中的 `os.walk()`。注意权限、效率和场景适用性,选择合适的方法以提高 Linux 操作效率。
28 3
|
9天前
|
监控 Linux Perl
在 Linux 中显示文件指定行内容的方法和技巧
【6月更文挑战第7天】在 Linux 中查看文本文件指定行,可使用 head/tail 显示文件头/尾部行,sed 的行号指定功能或 awk 进行处理。在代码审查、日志分析和文本处理等场景下,这些命令能提升效率。注意文件编码、行号准确性,并组合使用命令以实现更多功能。通过练习,能更好地掌握这些工具。
34 0
|
9天前
|
存储 数据挖掘 Linux
在 Linux 中找目录中最大文件
【6月更文挑战第7天】在 Linux 中找目录中最大文件:使用 `du` 加 `sort` 命令 (`du -a /path | sort -nr | head -n 1`),`find` 加 `xargs` 和 `sort` (`find /path -type f -print0 | xargs -0 du -h | sort -nr | head -n 1`) 或编写 Python 脚本。适用于服务器管理、数据分析和文件清理,注意文件权限、目录深度和文件系统类型。结合其他命令可提升效率。
23 0
|
9天前
|
Linux
蓝易云 - 修改linux文件权限命令:chmod
例如,`chmod 755 文件名`将给文件设置读/写/执行权限给所有者,读/执行权限给组用户和其他用户。
11 0