Bash 遍历目录列表并生成 md5

发布于 2024-11-25 23:32:54 字数 432 浏览 0 评论 0原文

bash 脚本会是这样的:

  1. 遍历一个目录,并将所有子目录放入一个数组中
  2. 对于每个目录,生成该目录内文件的 md5

和 另外,必须生成 md5sum 的文件不需要生成始终具有相同的名称和路径。然而,模式总是相同的:

/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 }

我以前从未使用过 bash(也从来不需要),所以这可能是非常简单和新手的事情。有人有主意吗?从路径可以看出,它被设计为在 iDevice 上运行。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄人书 2024-12-02 23:32:54
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

试试这个,我希望代码是简单的。值得解释的两件事是:

  • ${app##*/},它使用##运算符去除与表达式*/匹配的最长前缀
  • ${appdirname%.app},它使用 % 运算符去除与表达式 .app 匹配的最短后缀。 (您也可以使用 %% (去掉最长的后缀)而不是 %,因为模式 .app 始终是四个字符长。)
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.)
蓝戈者 2024-12-02 23:32:54

尝试类似的操作:

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

上面将显示所有应用程序的所有 Info.plist 文件的 md5 校验和,例如:

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

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
薄荷港 2024-12-02 23:32:54

Bash 非常简单,但您需要了解系统的 cli 工具。

用于递归打印目录中所有文件的 md5 哈希值:

find /yourdirectory/ -type f | xargs md5sum

如果您只想列出目录树:

find /tmp/ -type d

您可以使用以下命令生成列表:

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

使用“for”迭代列表:

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

如果您是 bash 新手:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文