文件切割(split),合并(cat),比对(diff,md5sum)

1.文件切割split

Linux split命令用于将一个文件分割成数个。

该指令将大文件分割成较小的文件,在默认情况下将按照每1000行切割成一个小文件。

1.1 语法

split [--help][--version][-<行数>][-b <字节>][-C <字节>][-l <行数>][要切割的文件][输出文件名]

wc命令用于计算文件中的行数、字数和字节数。wc -l 文件名 查看文件的行数。

1.2 参数说明

  • -<行数> : 指定每多少行切成一个小文件
  • -b<字节> : 指定每多少字节切成一个小文件
  • --help : 在线帮助
  • --version : 显示版本信息
  • -C<字节> : 与参数"-b"相似,但是在切 割时将尽量维持每行的完整性
  • [输出文件名] : 设置切割后文件的前置文件名, split会自动在前置文件名后再加上编号

1.3 split 实例

1.3.1 按行来分割

[root@localhost mytext]# ll
总用量 4104
-rw-r--r--. 1 root root 2097154 9月  20 18:38 2022-09-19.log
-rw-r--r--. 1 root root 2097154 9月  20 18:50 c.log
[root@localhost mytext]# split -200 2022-09-19.log 
[root@localhost mytext]# ll
总用量 6160
-rw-r--r--. 1 root root 2097154 9月  20 18:38 2022-09-19.log
-rw-r--r--. 1 root root 2097154 9月  20 18:50 c.log
-rw-r--r--. 1 root root  745777 9月  20 19:41 xaa
-rw-r--r--. 1 root root  630099 9月  20 19:41 xab
-rw-r--r--. 1 root root  721278 9月  20 19:41 xac
[root@localhost mytext]# 

以上命令执行后,指令"split"会将原来的大文件"2022-09-19.log"切割成多个以"x"开头的小文件。

修改默认以x开头命名的方式:

[root@localhost mytext]# rm -rf x*
[root@localhost mytext]# split -200 -d 2022-09-19.log  2022-09-19.log_
[root@localhost mytext]# ll
总用量 6160
-rw-r--r--. 1 root root 2097154 9月  20 18:38 2022-09-19.log
-rw-r--r--. 1 root root  745777 9月  20 19:44 2022-09-19.log_00
-rw-r--r--. 1 root root  630099 9月  20 19:44 2022-09-19.log_01
-rw-r--r--. 1 root root  721278 9月  20 19:44 2022-09-19.log_02
-rw-r--r--. 1 root root 2097154 9月  20 18:50 c.log
[root@localhost mytext]# 

1.3.2 按指定字节来分割文件

按b

[root@localhost mytext]# ll
总用量 4104
-rw-r--r--. 1 root root 2097154 9月  20 18:38 2022-09-19.log
-rw-r--r--. 1 root root 2097154 9月  20 18:50 c.log
[root@localhost mytext]# split -b 1M -d 2022-09-19.log 2022-09-19.log_
[root@localhost mytext]# ll
总用量 6156
-rw-r--r--. 1 root root 2097154 9月  20 18:38 2022-09-19.log
-rw-r--r--. 1 root root 1048576 9月  20 19:47 2022-09-19.log_00
-rw-r--r--. 1 root root 1048576 9月  20 19:47 2022-09-19.log_01
-rw-r--r--. 1 root root       2 9月  20 19:47 2022-09-19.log_02
-rw-r--r--. 1 root root 2097154 9月  20 18:50 c.log
[root@localhost mytext]# 

按C

[root@localhost mytext]# split -C 1M -d 2022-09-19.log 2022-09-19.log_
[root@localhost mytext]# ll
总用量 6156
-rw-r--r--. 1 root root 2097154 9月  20 18:38 2022-09-19.log
-rw-r--r--. 1 root root 1011263 9月  20 19:49 2022-09-19.log_00
-rw-r--r--. 1 root root 1048353 9月  20 19:49 2022-09-19.log_01
-rw-r--r--. 1 root root   37538 9月  20 19:49 2022-09-19.log_02
-rw-r--r--. 1 root root 2097154 9月  20 18:50 c.log
[root@localhost mytext]# 

2 文件合并。cat 连接文件命令。>重定向符,覆盖。>>重定向符,内容后面追加。

[root@localhost mytext]# ll
总用量 4104
-rw-r--r--. 1 root root 2097154 9月  20 18:38 2022-09-19.log
-rw-r--r--. 1 root root 1011263 9月  20 19:49 2022-09-19.log_00
-rw-r--r--. 1 root root 1048353 9月  20 19:49 2022-09-19.log_01
-rw-r--r--. 1 root root   37538 9月  20 19:49 2022-09-19.log_02
[root@localhost mytext]# cat 2022-09-19.log_00 2022-09-19.log_01 2022-09-19.log_02 >3.log
[root@localhost mytext]# ll
总用量 6156
-rw-r--r--. 1 root root 2097154 9月  20 18:38 2022-09-19.log
-rw-r--r--. 1 root root 1011263 9月  20 19:49 2022-09-19.log_00
-rw-r--r--. 1 root root 1048353 9月  20 19:49 2022-09-19.log_01
-rw-r--r--. 1 root root   37538 9月  20 19:49 2022-09-19.log_02
-rw-r--r--. 1 root root 2097154 9月  20 19:51 3.log
[root@localhost mytext]# 

3.比对文件是否相同

3.1diff 

Linux diff 命令用于比较文件的差异。

diff 以逐行的方式,比较文本文件的异同处。如果指定要比较目录,则 diff 会比较目录中相同文件名的文件,但不会比较其中子目录。

3.1.1 常用参数

  • -c  显示全部内文,并标出不同之处。
  • -s或--report-identical-files  若没有发现任何差异,仍然显示信息。

3.1.2 示例

[root@localhost mytext]# ll
总用量 6156
-rw-r--r--. 1 root root 2097154 9月  20 18:38 2022-09-19.log
-rw-r--r--. 1 root root 1011263 9月  20 19:49 2022-09-19.log_00
-rw-r--r--. 1 root root 1048353 9月  20 19:49 2022-09-19.log_01
-rw-r--r--. 1 root root   37538 9月  20 19:49 2022-09-19.log_02
-rw-r--r--. 1 root root 2097154 9月  20 19:51 3.log
[root@localhost mytext]# diff -s 2022-09-19.log 3.log 
檔案 2022-09-19.log 和 3.log 相同

3.2 使用md5sum 来计算和比对前后两个大文件的 md5 值

[root@localhost mytext]# ll
总用量 6156
-rw-r--r--. 1 root root 2097154 9月  20 18:38 2022-09-19.log
-rw-r--r--. 1 root root 1011263 9月  20 19:49 2022-09-19.log_00
-rw-r--r--. 1 root root 1048353 9月  20 19:49 2022-09-19.log_01
-rw-r--r--. 1 root root   37538 9月  20 19:49 2022-09-19.log_02
-rw-r--r--. 1 root root 2097154 9月  20 19:51 3.log
[root@localhost mytext]# md5sum 2022-09-19.log
fe393f0fe9398f48fbf2b16692862b02  2022-09-19.log
[root@localhost mytext]# md5sum 3.log 
fe393f0fe9398f48fbf2b16692862b02  3.log
[root@localhost mytext]# md5sum 2022-09-19.log_00
39976bb3b2a3fd348521af7eb37f78a3  2022-09-19.log_00
[root@localhost mytext]# md5sum 2022-09-19.log_01
0ddaba1ec6d25a0389d00891fc6c3529  2022-09-19.log_01
[root@localhost mytext]# md5sum 2022-09-19.log_02
c5dff381a5fb44d36b0159fbe509b2dc  2022-09-19.log_02
[root@localhost mytext]# 

 注意:md5sum读取的是文件内容,与文件名无关。

posted @ 2022-09-21 11:09  家乐福的搬砖日常  阅读(334)  评论(0编辑  收藏  举报