计算 Android 中目录层次结构中文件的哈希值

Calculating hash of files in directory hierarchy in Android

我需要计算目录层次结构中文件的 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 /* | 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

你应该使用 find:

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