获取文件名及其md5sums

时间:2018-02-08 07:22:23

标签: 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哈希并完全省略相应的文件名。搜索到所有试图纠正这个无济于事。

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

只需直接申请md5sumprintf即可,例如。

$ while read -r name; do 
    printf "%s %s %s\n" $(md5sum <<<"$name") "$name";
done <filein.txt

<强>输出

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