5538

28 分钟

#Bash 的 split 命令

split [OPTION]... [FILE [PREFIX]]

功能

将文件拆分为多个文件。

类型

可执行文件(/usr/bin/split),属于 coreutils

参数

  • OPTION 选项:
    • -a, --suffix-length=N - 输出文件名后缀长度设为 N;默认为 2
    • --additional-suffix=SUFFIX - 输出文件名额外添加后缀 SUFFIX
    • -b, --bytes=SIZE - 每个输出文件的大小为 SIZE 字节
    • -C, --line-bytes=SIZE - 每个输出文件的大小不超过 SIZE 字节,保持行的完整性
    • -d - 输出文件名后缀使用从 0 开始的数值;默认为 aa ab 格式
    • --numeric-suffixes[=FROM] - 输出文件名后缀使用从 FROM 开始的数值
    • -x - 输出文件名后缀使用从 0 开始的十六进行数值
    • --hex-suffixes[=FROM] - 输出文件名后缀使用从 FROM 开始的十六进行数值
    • -e, --elide-empty-files - 使用 -n 选项时,不生成空文件
    • --filter=COMMAND - 将输出写入 COMMAND 命令处理,并设置 $FILE 为文件名
    • -l, --lines=NUMBER - 每个输出文件有 NUMBER 行;默认为 1000 行
    • -n, --number=CHUNKS - 基于 CHUNK 生成文件:
      • N - 平均拆分成 N 个文件
      • K/N - 平均拆分成 N 份,将第 K 份打印到标准输出
      • l/N - 平均拆分成 N 个文件,保持行的完整性
      • l/K/N - 平均拆分成 N 份,将第 K 份打印到标准输出,保持行的完整性
      • r/N - 类似 l/N 但是使用循环分配
      • r/K/N - 类似 l/K/N 但是使用循环分配
    • -t, --separator=SEP - 使用 SEP 而不是换行符(\n)作为行的分隔符
    • -u, --unbuffered - 输出时不使用缓冲
    • --verbose - 在打开每个输出文件前打印诊断信息
    • --help - 显示帮助
    • --version - 显示版本
  • FILE - 文件列表;如果没有这个参数或指定为 -,则读取标准输入
  • PREFIX - 输出文件名前缀;默认为 x

#示例

字母后缀

$ split 1.txt # 将文件 1.txt 拆分,每个文件 1000 行 $ ls 1.txt xaa $ split -n 3 1.txt # 将文件 1.txt 拆分为 3 个文件 $ ls 1.txt xaa xab xac $ split -n 3 1.txt my_file_ # 输出文件名前缀为 my_file_ $ ls 1.txt xaa xab xac my_file_aa my_file_ab my_file_ac

数值后缀

$ split -n 3 -d 1.txt my_file_ # 前缀为 my_file_,使用从 0 开始的数值后缀 $ ls 1.txt my_file_00 my_file_01 my_file_02 $ split -n 3 --numeric-suffixes=10 1.txt # 前缀为 my_file_,使用从 10 开始的数值后缀 $ ls 1.txt my_file_00 my_file_01 my_file_02 my_file_10 my_file_11 my_file_12

#推荐阅读

#手册

SPLIT(1) User Commands SPLIT(1) NAME split - split a file into pieces SYNOPSIS split [OPTION]... [FILE [PREFIX]] DESCRIPTION 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) --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 -x use hex suffixes starting at 0, not alphabetic --hex-suffixes[=FROM] same as -x, 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 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,R,Q (powers of 1024) or KB,MB,... (powers of 1000). Binary prefixes can be used, too: KiB=K, MiB=M, and so on. 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 AUTHOR Written by Torbjorn Granlund and Richard M. Stallman. REPORTING BUGS GNU coreutils online help: <https://www.gnu.org/software/coreutils/> Report any translation bugs to <https://translationproject.org/team/> COPYRIGHT Copyright © 2023 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. SEE ALSO Full documentation <https://www.gnu.org/software/coreutils/split> or available locally via: info '(coreutils) split invocation' GNU coreutils 9.4 April 2024 SPLIT(1)

创建于 2025/12/4

更新于 2025/12/4