Search code examples
androidadbmd5

Calculating hash of files in directory hierarchy in Android


I need to calculate md5 hash of files in a directory hierarchy. I am using the following case as a test. The Android device I have has a md5 binary, but needs absolute path of file (md5 <filename>).

I have the following directory hierarchy on a Android device:

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

To get list of absolute paths, I followed the answer mentioned 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

Now I have list of absolute paths of each file.

Next I want to read this file in a script line by line, and call md5 for each line. md5 will print a message if the input is a directory. I followed the example here.

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

I get the following output:

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

I expected it to print a directory warning for test1 and test2, and then md5 for test3, as test3 is a file. Can someone suggest how to fix this code ? I was expecting something like:

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

Solution

  • You should use find:

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