使用 find 和 xargs 编写 Bash 脚本文件

发布于 2024-10-23 19:58:56 字数 969 浏览 4 评论 0原文

当我从多个目录对单个文件运行 shasum 时,我得到了我期望和最终想要的结果:

shasum 2010/Whitmore/The\ Astrophysical\ Journal\ 2010\ Whitmore.pdf
5da84767c2301f29ed3b7db7542167150fe63b8b  2010/Whitmore/The Astrophysical Journal 2010 Whitmore.pdf

当我从同一目录运行 find 时,它列出了文件,没有问题:

find 2010/Whitmore/* | xargs -0
2010/Whitmore/The Astrophysical Journal 2010 Whitmore.pdf

我一直在尝试编写一个 bash 脚本(或者只是一行),它将返回 find (有很多)找到的每个文件的单独 shasum 及其文件名。在我尝试过的任何组合中,将文件名通过管道传输到 shasum 都不起作用。

我预计问题出在文件名中的空格上,但是在 Google 搜索后,尝试了一些 find/xargs/ls 组合(以及似乎对其他人有效的各种组合,包括 -print0 标志),我仍然无法弄清楚找到一种对这些目录下的每个文件运行 shasum 的方法。

我想要的输出示例(假设 Whitmore 下有 2 个文件),以防有完全不同的方式获得此结果:

find 2010/Whitmore/* | xargs -0 | shasum
5da84767c2301f29ed3b7db7542167150fe63b8b  2010/Whitmore/The Astrophysical Journal 2010 Whitmore.pdf
4d2b0a6da00473133b4617d67d9249da3d05cc19  2010/Whitmore/arXiv 2010 Whitmore.pdf

我使用的是 Mac OSX 10.6

When I run shasum from a several directories up on a single file, I get what I expect and ultimately want:

shasum 2010/Whitmore/The\ Astrophysical\ Journal\ 2010\ Whitmore.pdf
5da84767c2301f29ed3b7db7542167150fe63b8b  2010/Whitmore/The Astrophysical Journal 2010 Whitmore.pdf

When I run find from the same directory, it lists the files no problem:

find 2010/Whitmore/* | xargs -0
2010/Whitmore/The Astrophysical Journal 2010 Whitmore.pdf

I've been trying to write a bash script (or just a single line) that will return the individual shasum on each file found by find (there are lots) along with its filename. Piping the filenames to shasum does not work in any of the combinations I've tried.

I expect that the problem is with the spaces in the filename, but after Google searching, trying a number of find/xargs/ls combinations (and various combinations of what seemed to work for other people including -print0 flags), I still cannot figure out a way to run shasum on each file under these directories.

An example of the output I want (assuming there are 2 files under Whitmore) in case there's a completely different way of getting this result:

find 2010/Whitmore/* | xargs -0 | shasum
5da84767c2301f29ed3b7db7542167150fe63b8b  2010/Whitmore/The Astrophysical Journal 2010 Whitmore.pdf
4d2b0a6da00473133b4617d67d9249da3d05cc19  2010/Whitmore/arXiv 2010 Whitmore.pdf

I am on a Mac OSX 10.6

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

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

发布评论

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

评论(5

旧情别恋 2024-10-30 19:58:56

使用 find-exec 选项怎么样?

查找 2010/Whitmore/* -exec shasum {} \;

How about using the -exec option of find?

find 2010/Whitmore/* -exec shasum {} \;

〆凄凉。 2024-10-30 19:58:56

如果您的磁盘速度很快,您可能需要并行运行多个:

find 2010/Whitmore/* -type f | parallel shasum

要了解有关 GNU Parallel 的更多信息,请观看介绍视频(如果您是天体物理学家,您会很高兴这样做):http://www.youtube.com/watch?v=OpaiGYxkSuQ

If your disks are fast you may want to run several in parallel:

find 2010/Whitmore/* -type f | parallel shasum

To learn more about GNU Parallel watch the intro video (If you are an astrophysicist you will be glad you did): http://www.youtube.com/watch?v=OpaiGYxkSuQ

捂风挽笑 2024-10-30 19:58:56

有效吗

find -type f | sed 's/./\\&/g' | xargs shasum

Does

find -type f | sed 's/./\\&/g' | xargs shasum

work?

佞臣 2024-10-30 19:58:56

要解决文件名中空格字符的问题,请使用“\0”作为文件分隔符:

find 2010/Whitmore/ -type f -print0 | xargs -0 -l -P 2 shasum

xargs-P 参数指定应运行多少个并发进程。

To get around the trouble with space chars in filenames, use '\0' as file separator:

find 2010/Whitmore/ -type f -print0 | xargs -0 -l -P 2 shasum

The -P argument to xargs specifies how many concurrent processes it should run.

骷髅 2024-10-30 19:58:56

而不是:

查找 2010/Whitmore/* | xargs -0 | xargs -0沙苏姆

你应该使用

查找 2010/Whitmore/* | xargs 沙苏姆

instead of:

find 2010/Whitmore/* | xargs -0 | shasum

you should use

find 2010/Whitmore/* | xargs shasum

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