centos7 split 切割文件_Linux split命令拆分文件详解

Linux split命令可以将一个大文件分割成指定大小的很多个小文件,并且拆分速度非常的快,有时需要将文件分割成更小的片段,比如为提高可读性,生成日志。拆分一个1G大小的文件不到1秒的时间就可以完成,如果在Windows系统上进行操作估计会很卡很卡。

选项

[linuxidc@localhost ~]$ split --help

用法:split [选项]... [输入 [前缀]]

Output fixed-size pieces of INPUT to PREFIXaa, PREFIXab, ...; default

size is 1000 lines, and default PREFIX is 'x'.  With no INPUT, or when INPUT

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 lines per output file

-d, --numeric-suffixes[=FROM]  use numeric suffixes instead of alphabetic;

FROM changes the start value (default 0)

-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 per output file

-n, --number=CHUNKS    generate CHUNKS output files; see explanation below

-u, --unbuffered        immediately copy input to output with '-n r/...'

--verbose  在每个输出文件打开前输出文件特征

--help  显示此帮助信息并退出

--version  显示版本信息并退出

SIZE is an integer and optional unit (example: 10M is 10*1024*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

l/K/N  output Kth of N to stdout without splitting lines

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:

请向 报告split 的翻译错误

要获取完整文档,请运行:info coreutils 'split invocation'

版本

[linuxidc@localhost ~]$ split --version

split (GNU coreutils) 8.22

Copyright (C) 2013 Free Software Foundation, Inc.

许可证:GPLv3+:GNU 通用公共许可证第3 版或更新版本。

本软件是自由软件:您可以自由修改和重新发布它。

在法律范围内没有其他保证。

由Torbjörn Granlund 和Richard M. Stallman 编写。

语法

split(选项)(file)PREFIX

实例

[linuxidc@localhost linuxidc.com]$ more linuxidc

w

w

w

l

i

n

u

x

i

d

c

c

o

m

1.根据行拆分

每3行拆分成一个文件,拆分后的文件名以linuxidc开头,以数字作为后缀后缀长度为1

[linuxidc@localhost linuxidc.com]$ split -l 3 linuxidc -d -a 1 linuxmi

[linuxidc@localhost linuxidc.com]$ ll

总用量 24

-rw-rw-r--. 1 linuxidc linuxidc 28 5月  19 19:43 linuxidc

-rw-rw-r--. 1 linuxidc linuxidc  6 5月  19 19:45 linuxmi0

-rw-rw-r--. 1 linuxidc linuxidc  6 5月  19 19:45 linuxmi1

-rw-rw-r--. 1 linuxidc linuxidc  6 5月  19 19:45 linuxmi2

-rw-rw-r--. 1 linuxidc linuxidc  6 5月  19 19:45 linuxmi3

-rw-rw-r--. 1 linuxidc linuxidc  4 5月  19 19:45 linuxmi4

2.按字节进行拆分

每三个字节拆分成一个文件,默认不加单位就是字节,也可以带单位比如KB,MB等

[linuxidc@localhost linuxidc.com]$ split -b 3 linuxidc -d -a 1 CentOS

[linuxidc@localhost linuxidc.com]$ ls -l centos*

-rw-rw-r--. 1 linuxidc linuxidc 3 5月  19 19:48 centos0

-rw-rw-r--. 1 linuxidc linuxidc 3 5月  19 19:48 centos1

-rw-rw-r--. 1 linuxidc linuxidc 3 5月  19 19:48 centos2

-rw-rw-r--. 1 linuxidc linuxidc 3 5月  19 19:48 centos3

-rw-rw-r--. 1 linuxidc linuxidc 3 5月  19 19:48 centos4

-rw-rw-r--. 1 linuxidc linuxidc 3 5月  19 19:48 centos5

-rw-rw-r--. 1 linuxidc linuxidc 3 5月  19 19:48 centos6

-rw-rw-r--. 1 linuxidc linuxidc 3 5月  19 19:48 centos7

-rw-rw-r--. 1 linuxidc linuxidc 3 5月  19 19:48 centos8

-rw-rw-r--. 1 linuxidc linuxidc 1 5月  19 19:48 centos9

3.生成一个大小为100KB的测试文件:

[linuxidc@localhost linuxidc.com]$ dd if=/dev/zero bs=100k count=1 of=linuxidc.file

记录了1+0 的读入

记录了1+0 的写出

102400字节(102 kB)已复制,0.00037479 秒,273 MB/秒

[linuxidc@localhost linuxidc.com]$ ll

总用量 100

-rw-rw-r--. 1 linuxidc linuxidc 102400 5月  19 19:51 linuxidc.file

4.使用split命令将上面创建的linuxidc.file文件分割成大小为10KB的小文件:

[linuxidc@localhost linuxidc.com]$ split -b 10k linuxidc.file

[linuxidc@localhost linuxidc.com]$ ls

linuxidc.file  xaa  xab  xac  xad  xae  xaf  xag  xah  xai  xaj

5.此时文件被分割成多个带有字母的后缀文件,如果你想用数字后缀可使用-d参数,同时可以使用-a length来指定后缀的长度:

[linuxidc@localhost linuxidc.com]$ split -b 10k linuxidc.file  -d -a 3

[linuxidc@localhost linuxidc.com]$ ls

linuxidc.file  x000  x001  x002  x003  x004  x005  x006  x007  x008  x009

6.为分割后的文件指定文件名的前缀:

[linuxidc@localhost linuxidc.com]$ split -b 10k linuxidc.file -d -a 3 linuxmi_file

[linuxidc@localhost linuxidc.com]$ ls

linuxidc.file    linuxmi_file002  linuxmi_file005  linuxmi_file008

linuxmi_file000  linuxmi_file003  linuxmi_file006  linuxmi_file009

linuxmi_file001  linuxmi_file004  linuxmi_file007

7.使用-l选项根据文件的行数来分割文件,例如把文件分割成每个包含10行的小文件:

[linuxidc@localhost linuxidc.com]$ split -l 10 linuxidc.file

8.将split分割的文件合并成一个

[linuxidc@localhost linuxidc.com]$ split -b 10k linuxidc.file

[linuxidc@localhost linuxidc.com]$ ls

linuxidc.file  xaa  xab  xac  xad  xae  xaf  xag  xah  xai  xaj

[linuxidc@localhost linuxidc.com]$ cat x*>>y*

[linuxidc@localhost linuxidc.com]$ ls

linuxidc.file  xaa  xab  xac  xad  xae  xaf  xag  xah  xai  xaj  y*

总结

Linux下的spit命令非常实用,比如导入数据时将文件进行拆分并导入会快很多。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值