create account

每天进步一点点:Linux下校验文件 by oflyhigh

View this thread on: hive.blogpeakd.comecency.com
· @oflyhigh ·
$18.29
每天进步一点点:Linux下校验文件
在之前的帖子中,我介绍了[axel这个多线程下载工具](https://steemit.com/cn/@oflyhigh/axel-wget),用来替换wget进行多线程下载。

每天进步一点点
![](https://steemitimages.com/DQmRkLq6rRew3mHfx4vYGWyqpC8wSebLPeC2iZCXAdpuGkR/image.png)
(图源 :[pixabay](https://pixabay.com/))

不过在用axel下载了一个数百G的文件后,我突然发觉心里没底,万一这个文件里边丢失哪怕一个字节的内容,这个文件都算白下了,那么如何校验我下载的文件和原始文件一致呢?


# md5sum

其实最简单的方法就是校验文件的hash值了,而校验文件hash值最常用的就是md5sum啦。

Ubuntu 18.04发行版中已经包含了md5sum(包含在[GNU Core Utilities](http://www.gnu.org/software/coreutils/)中),所以我们直接使用即可,无需安装。

调用方式如下:
>`md5sum [option]… [file]…`

比如我要检查文件aaa.tgz的md5,那么只需调用:
>`md5sum aaa.tgz`

输出结果如下:
>![](https://cdn.steemitimages.com/DQmbqwA6KiEmLjyfCfFKfJnH95mag76SmLy9WAKyKu4HHdn/image.png)

之后再去服务器上执行同样的命令,获取服务器对应文件的hash,并进行对比就可以啦。

# 使用校验文件

在对单个文件进行处理时,只要不是头昏眼花,上述人工肉眼判断是没有问题的,但是如果多个文件需要校验,那么估计就该头昏眼花了。

所以使用-c参数就很有必要了:
>`-c, --check          read MD5 sums from the FILEs and check them`

简单的来讲,就是把原始文件生成的md5sum输出写入到文件中,下载或复制等传播后,md5sum会直接处理文件并对比校验码。

举例如下:
我们在服务器上生成aaa.tgz的校验码并保存到本地文件md5.txt,内容如下:
>`d41d8cd98f00b204e9800998ecf8427e aaa.tgz`

在本地执行如下命令:
>`md5sum -c md5.txt`

返回内容如下,说明文件校验成功。
>![](https://cdn.steemitimages.com/DQmPRDaVR6pwStx7fQZZBkJdsz6TJZQxQfBDigZphxz5jhR/image.png)

下面我们手工破坏一下文件,再测试一下:
>`echo "hello" >> aaa.tgz`
>`md5sum -c md5.txt`

再来看测试结果,会发现提示校验检查出错:
>![](https://cdn.steemitimages.com/DQmY35hAWfHZjBm91W2H5wY4PAiWnLevmHVKYjFSQsANrvn/image.png)

# 其它补充

除了md5sum,还有好多内置的校验工具可用:
>![](https://cdn.steemitimages.com/DQmT1EMQYAT7dksyvNJJpdYiPxXyFdKatnLCG9XH64CS5KE/image.png)

对我而言,校验自己的文件,用md5sum足够啦。


# 相关链接

* http://www.gnu.org/software/coreutils/md5sum
* http://www.gnu.org/software/coreutils/sha256sum
* [每天进步一点点:使用axel替代wget进行高速多线程下载](https://steemit.com/cn/@oflyhigh/axel-wget)

----
<center><strong>Vote For Me As Witness</strong>
https://steemit.com/~witnesses type in **`oflyhigh`** and click ***`VOTE`***
[![](https://cdn.steemitimages.com/DQmX5NysqT44FBa3bhuWqQ69nAbseu8Nt5YQPn2pYejPVxA/image.png)](https://steemit.com/~witnesses)
[Vote @oflyhigh via Steemconnect](https://steemconnect.com/sign/account-witness-vote?witness=oflyhigh&approve=1)
<strong>Thank you!</strong></center>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 171 others
👎  ,
properties (23)
authoroflyhigh
permlink2ndygz-linux
categorycn
json_metadata{"tags":["cn","hash","md5sum","linux","study"],"image":["https://steemitimages.com/DQmRkLq6rRew3mHfx4vYGWyqpC8wSebLPeC2iZCXAdpuGkR/image.png","https://cdn.steemitimages.com/DQmbqwA6KiEmLjyfCfFKfJnH95mag76SmLy9WAKyKu4HHdn/image.png","https://cdn.steemitimages.com/DQmPRDaVR6pwStx7fQZZBkJdsz6TJZQxQfBDigZphxz5jhR/image.png","https://cdn.steemitimages.com/DQmY35hAWfHZjBm91W2H5wY4PAiWnLevmHVKYjFSQsANrvn/image.png","https://cdn.steemitimages.com/DQmT1EMQYAT7dksyvNJJpdYiPxXyFdKatnLCG9XH64CS5KE/image.png","https://cdn.steemitimages.com/DQmX5NysqT44FBa3bhuWqQ69nAbseu8Nt5YQPn2pYejPVxA/image.png"],"links":["https://steemit.com/cn/@oflyhigh/axel-wget","https://pixabay.com/","http://www.gnu.org/software/coreutils/","http://www.gnu.org/software/coreutils/md5sum","http://www.gnu.org/software/coreutils/sha256sum","https://steemit.com/~witnesses","https://steemconnect.com/sign/account-witness-vote?witness=oflyhigh&approve=1"],"app":"steemit/0.1","format":"markdown"}
created2019-11-01 02:10:36
last_update2019-11-01 02:10:36
depth0
children6
last_payout2019-11-08 02:10:36
cashout_time1969-12-31 23:59:59
total_payout_value9.578 HBD
curator_payout_value8.716 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,099
author_reputation6,354,924,622,318,836
root_title每天进步一点点:Linux下校验文件
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id92,089,520
net_rshares61,161,807,949,620
author_curate_reward""
vote details (237)
@partiko ·
Thank you so much for participating in the Partiko Delegation Plan Round 1! We really appreciate your support! As part of the delegation benefits, we just gave you a 3.23% upvote! Together, let’s change the world!
properties (22)
authorpartiko
permlinkre-2ndygz-linux-20191101t023014
categorycn
json_metadata"{"app": "partiko"}"
created2019-11-01 02:30:18
last_update2019-11-01 02:30:18
depth1
children0
last_payout2019-11-08 02:30:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length213
author_reputation39,207,160,334,751
root_title每天进步一点点:Linux下校验文件
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id92,089,846
net_rshares0
@softmetal ·
$0.15
@tipu curate
👍  
properties (23)
authorsoftmetal
permlinkq09q7m
categorycn
json_metadata{"users":["tipu"],"app":"steemit/0.1"}
created2019-11-01 02:14:15
last_update2019-11-01 02:14:15
depth1
children2
last_payout2019-11-08 02:14:15
cashout_time1969-12-31 23:59:59
total_payout_value0.076 HBD
curator_payout_value0.075 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length12
author_reputation146,291,910,015,030
root_title每天进步一点点:Linux下校验文件
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id92,089,591
net_rshares836,835,330,433
author_curate_reward""
vote details (1)
@oflyhigh ·
谢谢
properties (22)
authoroflyhigh
permlinkq0aqwc
categorycn
json_metadata{"app":"steemit/0.1"}
created2019-11-01 15:26:39
last_update2019-11-01 15:26:39
depth2
children0
last_payout2019-11-08 15:26:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2
author_reputation6,354,924,622,318,836
root_title每天进步一点点:Linux下校验文件
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id92,106,517
net_rshares0
@tipu ·
<a href="https://tipu.online/curator?softmetal" target="_blank">Upvoted &#128076;</a> (Mana: 10/15)
properties (22)
authortipu
permlinkre-q09q7m-20191101t021424
categorycn
json_metadata""
created2019-11-01 02:14:24
last_update2019-11-01 02:14:24
depth2
children0
last_payout2019-11-08 02:14:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length99
author_reputation55,935,285,319,710
root_title每天进步一点点:Linux下校验文件
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id92,089,598
net_rshares0
@transisto ·
Downvoting due to suspected vote trading.
👍  
👎  ,
properties (23)
authortransisto
permlinkq0d2s6
categorycn
json_metadata{"app":"steemit/0.1"}
created2019-11-02 21:38:30
last_update2019-11-02 21:38:30
depth1
children1
last_payout2019-11-09 21:38:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length41
author_reputation330,357,940,720,833
root_title每天进步一点点:Linux下校验文件
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id92,142,361
net_rshares-7,352,634,008,728
author_curate_reward""
vote details (3)
@oflyhigh ·
Hi @transisto
I hope you can read this post carefully and the cancel the downvote.  
The about same reason [here](https://steemit.com/cn/@oflyhigh/q0dcrg).
properties (22)
authoroflyhigh
permlinkq0dcw1
categorycn
json_metadata{"users":["transisto"],"links":["https://steemit.com/cn/@oflyhigh/q0dcrg"],"app":"steemit/0.1"}
created2019-11-03 01:16:51
last_update2019-11-03 01:16:51
depth2
children0
last_payout2019-11-10 01:16:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length155
author_reputation6,354,924,622,318,836
root_title每天进步一点点:Linux下校验文件
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id92,146,207
net_rshares0