linux - 获取文件名及其 md5sums 的组合列表

标签 linux bash md5sum

我有一个文件列表,在某些 filein.txt 上每行列出 1 个,如下所示:

mikesfile.php
ericsfile.php
subdir1/johnsfile.php
subdir1/davidsfile.php
subdir1/subdir2/ashleysfile.php
subdir1/subdir2/zoesfile.php

我需要我的 bash 脚本逐行读取,md5sum 相应的文件,然后将文件及其 md5 写入名为 fileout.txt 的新文件

例如:

e14086108b4d5d191c22b0a085694e4a - mikesfile.php
ebadb70de710217a7d4d4c9d114b8145 - ericsfile.php
b40bb5dfb23bf89b3011ff82d9cb0b0b - subdir1/johnsfile.php
d03e9b7306cb1f6c019b574437f54db0 - subdir1/davidsfile.php
f840a8d2ea7342303c807b6cb6339fd1 - subdir1/subdir2/ashleysfile.php
3560e05d5ccdad6900a5dfed1a4a8154 - subdir1/subdir2/zoesfile.php

我一直在搞这个:

while read line; do echo -n "$line" | md5sum; done; < filein > fileout

但它只是转储了 md5 哈希并完全省略了相应的文件名。到处搜索试图解决这个问题,但无济于事。

我非常感谢您帮助将两者结合起来并将它们正确写入输出文件,如图所示。非常感谢。

最佳答案

bash + awk 解决方案:

while read -r fn; do
    echo "$fn" | md5sum | awk -v fn="$fn" '{ print $0,fn }'
done < filein > fileout

$ cat fileout
1db757c4f098cebf93639f00e55bc88d  - mikesfile.php
f063a35599d453721ada5d0c8fcc0185  - ericsfile.php
a8c3a721d12b432c94c23d463fb5a93f  - subdir1/johnsfile.php
a4aa114d977c75153aac382574229d3a  - subdir1/davidsfile.php
abd77236c393266115acda48ddb4f9a0  - subdir1/subdir2/ashleysfile.php
18ba7e37f42a837d33a7fb3e56a618b5  - subdir1/subdir2/zoesfile.php

关于linux - 获取文件名及其 md5sums 的组合列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48679777/

相关文章:

c - 当我尝试运行 mpi 示例时权限被拒绝

bash - 通过 bash curl 发送邮件

linux - 如何使用 bash 脚本编写密码?

java - 一个简单的重复 block 查找算法在使用 BloomFilter 进行查找时性能更差

Python比较md5哈希

c - 逐 block 反转文件内容

c++ - CMake find_* 不搜索子目录

bash - 并行检查 md5 文件

linux - ELF Header Magic - 为什么要放入 0x7F?

linux - 在嵌套文件夹中创建文件夹不起作用