【问题标题】:Calculating hash of files in directory hierarchy in Android在Android中计算目录层次结构中文件的哈希值
【发布时间】:2022-01-09 01:22:43
【问题描述】:

我需要计算目录层次结构中文件的 md5 哈希值。我正在使用以下案例作为测试。我的 Android 设备有一个 md5 二进制文件,但需要文件的绝对路径 (md5 <filename>)。

我在 Android 设备上有以下目录层次结构:

/data/local/tmp/test1
/data/local/tmp/test1/test2
/data/local/tmp/test1/test2/test3

要获取绝对路径列表,我按照here 中提到的答案进行操作。

$ adb shell 'function rcrls() { ls -d $1/* | while read f; do echo "$f"; if [ -d "$f" ]; then rcrls "$f"; fi; done } ; rcrls /data/local/tmp/' > filelist.txt

现在我有了每个文件的绝对路径列表。

接下来我想在脚本中逐行读取这个文件,并为每一行调用md5。如果输入是目录,md5 将打印一条消息。我按照here的例子。

#! /bin/bash
filename='filelist.txt'
cat $filename | while read LINE; do
    adb shell 'md5 $LINE'
done

我得到以下输出:

/data/local/tmp/test1/test2
/data/local/tmp/test1/test2/test3
could not read /data/local/tmp/test1, Is a directory

我希望它为 test1 和 test2 打印一个目录警告,然后为 test3 打印 md5,因为 test3 是一个文件。有人可以建议如何修复此代码吗?我期待的是这样的:

could not read /data/local/tmp/test1, Is a directory
could not read /data/local/tmp/test1/test2, Is a directory
<hash_value>   /data/local/tmp/test1/test2/test3

【问题讨论】:

    标签: android adb md5


    【解决方案1】:

    你应该使用find:

    adb shell find /data/local/tmp -type f -exec md5sum {} '\;'
    

    【讨论】:

    • 我的测试设备在/system/bin 中没有find .. 还有其他建议吗?
    • @Jake 因为您有 adb 访问权限,所以没有什么可以阻止您将自己的 busybox 二进制文件推送到手机上。然后你就有了 find 和所有其他可用的命令,你确切地知道它们支持哪些参数以及输出的格式。
    • 谢谢@DiegoTorresMilano。
    • 谢谢@罗伯特。
    猜你喜欢
    • 1970-01-01
    • 2016-11-23
    • 2011-12-19
    • 2013-02-02
    • 1970-01-01
    • 2021-09-11
    • 2019-10-03
    • 2020-04-28
    • 2010-12-07
    相关资源
    最近更新 更多