开发者

Bash go through list of dirs and generate md5

What would be the bash script that:

  1. Goes through a directory, and puts all the sub-directories in an array
  2. For each dir, generate an md5 sum of a file inside that dir

Also, the file who's md5sum has to be generated doesn't always have the same name and 开发者_开发百科path. However, the pattern is always the same:

/var/mobile/Applications/{ the dir name here is taken from the array }/{some name}.app/{ binary, who's name is the same as it's parent dir, but without the .app extension }

I've never worked with bash before (and have never needed to) so this may be something really simple and nooby. Anybody got an idea? As can be seen by the path, this is designed to be run on an iDevice.


for dir in /var/mobile/Applications/*; do
  for app in "$dir"/*.app; do
    appdirname=${app##*/}
    appname=${appdirname%.app}
    binary="$app/$appname"
    if [ -f "$binary" ]; then
      echo "I: dir=$dir appbase=$appbase binary=$binary"
    fi
  done
done

Try this, I hope the code is straight-forward. The two things worth explaining are:

  • ${app##*/}, which uses the ## operator to strip off the longest prefix matching the expression */.
  • ${appdirname%.app}, which uses the % operator to strip off the shortest suffix matching the expression .app. (You could have also used %% (strip longest suffix) instead of %, since the pattern .app is always four characters long.)


Try something like:

ls -1 /Applications/*/Contents/Info.plist | while read name; do md5 -r "$name"; done

the above will show md5 checksum for all Info.plist files for all applications, like:

d3bde2b76489e1ac081b68bbf18a7c29 /Applications/Address Book.app/Contents/Info.plist
6a093349355d20d4af85460340bc72b2 /Applications/Automator.app/Contents/Info.plist
f1c120d6ccc0426a1d3be16c81639ecb /Applications/Calculator.app/Contents/Info.plist


Bash is very easy but you need to know the cli-tools of your system.

For to print the md5 hash of all files of the a directory recursively:

find /yourdirectory/ -type f | xargs md5sum

If you only want to list the tree of directories:

find /tmp/ -type d

You can generate a list with:

MYLIST=$( find /tmp/ -type d )

Use "for" for iterate the list:

for i in $MYLIST; do
    echo $i;
done

If you are a newbie in bash:

  • http://tldp.org/LDP/Bash-Beginners-Guide/html/
  • http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜