split 命令用于将文件平均分割为多个小文件,多用于日志查看。
先下载一个测试文件 [split_test][1]
[root@dosbat ~]# wget https://raw.githubusercontent.com/wxnacy/file/master/common/split_test
[root@dosbat ~]# split split_test
[root@dosbat ~]# ll
-rw-r--r-- 1 root root 216320 8月 15 05:55 split_test
-rw-r--r-- 1 root root 20000 8月 15 05:56 xaa
-rw-r--r-- 1 root root 20000 8月 15 05:56 xab
-rw-r--r-- 1 root root 20000 8月 15 05:56 xac
-rw-r--r-- 1 root root 20000 8月 15 05:56 xad
...
默认 `split` 会将文件均匀的分割成多个小文件,文件名以 x 开头,剩余字符按字符表排序。
语法
$ split [OPTION]... [FILE [PREFIX]]
split
命令有多个参数可供调用
- -b 指定输出文件的大小,单位为 byte。
- -d 使用数字作为后缀。
- -l 指定输出文件的行数
- -a 指定后缀的长度
示例
分割出 100k 的文件
[root@dosbat ~]# split -b 100k split_test
[root@dosbat ~]# ll
total 856
-rw-r--r-- 1 root root 216320 8月 15 05:55 split_test
-rw-r--r-- 1 root root 102400 8月 15 05:58 xaa
-rw-r--r-- 1 root root 102400 8月 15 05:58 xab
-rw-r--r-- 1 root root 11520 8月 15 05:58 xac
-rw-r--r-- 1 root root 20000 8月 15 05:56 xad
-rw-r--r-- 1 root root 20000 8月 15 05:56 xae
-rw-r--r-- 1 root root 20000 8月 15 05:56 xaf
-rw-r--r-- 1 root root 20000 8月 15 05:56 xag
-rw-r--r-- 1 root root 20000 8月 15 05:56 xah
...
使用数字为后缀,并指定长度
[root@dosbat ~]# split -b 100k -d -a 4 split_test
[root@dosbat ~]# ll
-rw-r--r-- 1 root root 216320 8月 15 05:55 split_test
-rw-r--r-- 1 root root 102400 8月 15 06:01 x0000
-rw-r--r-- 1 root root 102400 8月 15 06:01 x0001
-rw-r--r-- 1 root root 11520 8月 15 06:01 x0002
指定文件前缀
[root@dosbat ~]# split -b 100k -d -a 4 split_test split.
-rw-r--r-- 1 root root 102400 8月 15 06:02 split.0000
-rw-r--r-- 1 root root 102400 8月 15 06:02 split.0001
-rw-r--r-- 1 root root 11520 8月 15 06:02 split.0002
...
指定行数分割文件
[root@dosbat ~]# split -l 1000 split_test
[root@dosbat ~]# ll
-rw-r--r-- 1 root root 20000 8月 15 06:03 xaa
-rw-r--r-- 1 root root 20000 8月 15 06:03 xab
-rw-r--r-- 1 root root 20000 8月 15 06:03 xac
-rw-r--r-- 1 root root 20000 8月 15 06:03 xad
-rw-r--r-- 1 root root 20000 8月 15 06:03 xae
-rw-r--r-- 1 root root 20000 8月 15 06:03 xaf
-rw-r--r-- 1 root root 20000 8月 15 06:03 xag
-rw-r--r-- 1 root root 20000 8月 15 06:03 xah
-rw-r--r-- 1 root root 20000 8月 15 06:03 xai
-rw-r--r-- 1 root root 20000 8月 15 06:03 xaj
-rw-r--r-- 1 root root 16320 8月 15 06:03 xak
...
[root@prometheus ~]# wc -l xaa
1000 xaa