Linux split文件切分工具的使用
目录
0x00、背景介绍
日常生活中,我们会遇到大文件,像操作系统镜像、高清电影、超大日志文件等等,即便是压缩效果也不大。如下问题博主会经常遇到,简单罗列一下几种常见的情况。
1.通常会遇到大文件,有时候对于超大文件的拷贝到U盘是有限制的,大于多少G就不允许拷贝。
2.另外我们通常会记录程序的操作日志,有时候为了方便定位,即便是做成每天按日期划分,单个文件还是会超级大。
3.在Linux下vim打开超大文件,受到内存硬件原因,往往会遇到打不开或者内存使用过高导致卡机问题。
4.有时候我们并不一定要打开查看整个文件,而是获取其中的一部分,拆分就会显得十分省力。
0x01、split介绍
Linux下有个强大的文件切分的命令工具split,查看帮助文件。
root@Lemon:~/Desktop/split$ split --help
Usage: split [OPTION]... [FILE [PREFIX]]
Output pieces of FILE to PREFIXaa, PREFIXab, ...;
default size is 1000 lines, and default PREFIX is 'x'.
如果没有指定文件,或者文件为"-",则从标准输入读取。
必选参数对长短选项同时适用。
-a, --suffix-length=N generate suffixes of length N (default 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
-l, --lines=NUMBER put NUMBER lines/records per output file
-n, --number=CHUNKS generate CHUNKS output files; see explanation below
-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 在每个输出文件打开前输出文件特征
--help 显示此帮助信息并退出
--version 显示版本信息并退出
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/>
请向<http://translationproject.org/team/zh_CN.html> 报告split 的翻译错误
Full documentation at: <http://www.gnu.org/software/coreutils/split>
or available locally via: info '(coreutils) split invocation'
我们看到split的主要参数
split [--help][--version][-<行数>][-b <字节>][-C <字节>][-l <行数>][要切割的文件][输出文件名]
-<行数> : 指定每多少行切成一个小文件
-b<字节> : 指定每多少字节切成一个小文件
--help : 查看在线帮助
--version : 显示版本信息
-C<字节> : 与参数"-b"相似,但是在切 割时将尽量维持每行的完整性
[输出文件名] : 设置切割后文件的前置文件名, split会自动在前置文件名后再加上编号
在没有明确指定拆分后文件的命名方式的情况下,split 会默认采用 x 字符作为文件前缀,拼接下划线,然后拼接aa、ab、ac...dc等,类似x_aa、x_ab、x_dc。可以直接接文件名前缀指定。
添加--verbose参数可以看到拆分创建文件的过程。
添加-a参数,可以设置后缀拼接划分的长度,如-a 5,输出文件结果x_aaaaa、x_aaaab。
下面是man split系统命令的使用手册。
SPLIT(1) User Commands &nb

本文介绍了Linux下split命令的使用,包括按行数、字节数、固定数量拆分文件,并展示了如何指定文件名前缀、后缀长度和添加verbose参数。此外,还详细讲解了如何在Linux和Windows环境下合并已拆分的文件,确保文件内容完整无误。
最低0.47元/天 解锁文章
911





