0

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
Lino
  • 5,084
  • 3
  • 21
  • 39
Jake
  • 16,329
  • 50
  • 126
  • 202

1 Answers1

1

You should use find:

adb shell find /data/local/tmp -type f -exec md5sum {} '\;'
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • The test device I have does not have `find` in `/system/bin` .. any other suggestion ? – Jake Jan 09 '22 at 04:59
  • 1
    Then try something like https://stackoverflow.com/questions/28711063/implementing-a-simple-version-of-find-in-a-bash-script – Diego Torres Milano Jan 09 '22 at 06:29
  • 1
    @Jake As you have adb access nothing prevents you from just pushing your own busybox binary to the phone. Then you have find and all the other commands available and you exactly know what parameters they support and how the output is formatted. – Robert Jan 09 '22 at 11:36
  • Thank you @DiegoTorresMilano. – Jake Jan 09 '22 at 19:39
  • Thank you @Robert. – Jake Jan 09 '22 at 19:39
  • If you are in shell (adb shell), then to get md5 of all the files in current directory: ```find . -type f -exec md5sum {} ";"``` – HackRx May 29 '23 at 14:53