【问题标题】:Get list of file names and their md5 hashes into text file获取文件名列表及其 md5 散列到文本文件中
【发布时间】:2023-03-29 14:20:02
【问题描述】:

我编写了这个命令来查找所有 .txt 文件名并对其进行排序/去重,然后保存到一个文本文件中。

find . -type f -name '*.txt' -exec basename {} \; | sort | uniq - txtsort.txt

这样我得到的文件填充了唯一的文件名

...
Empty.txt
...

如何将文件 md5 哈希值连接到它们的文件名?

喜欢:

...
d41d8cd98f00b204e9800998ecf8427e_Empty.txt
...

【问题讨论】:

  • 注意:这个命令行不能正确处理包含换行符的文件名。

标签: bash macos shell terminal zsh


【解决方案1】:
find . -type f -name '*.txt' -exec bash -c "a=\$(basename {} | md5) ; mv {} \$(dirname {})/\$a\_\$(basename {}) " \;

这应该可以解决问题。

find . -type f -name '*.txt' :遍历所有 .txt 文件。

a=\$(basename {} | md5) :存储当前文件基本名称的 md5 哈希值。

mv {} \$(dirname {})/\$a\_\$(basename {}) :通过添加 $a 重命名当前文件,即用 _ 分隔。

【讨论】:

  • 也许我应该更好地解释一下自己,我不需要重命名文件,我只需要将找到的列表 md5_filename.txt 存储在一个新的文本文件中(然后必须对其行进行重复数据删除和排序)
【解决方案2】:

我发现使用 md5 -r 回答了我的问题(我真的不需要名称中的下划线,它只是为了美观),所以:

find . -type f -name '*.txt' -exec md5 -r {} \; | sort | uniq - txtsort.txt

会成功的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-03
    • 2020-07-06
    • 1970-01-01
    • 2021-10-08
    • 2010-11-06
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    相关资源
    最近更新 更多