使用 find 和 xargs 编写 Bash 脚本文件
当我从多个目录对单个文件运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
find
的-exec
选项怎么样?查找 2010/Whitmore/* -exec shasum {} \;
How about using the
-exec
option offind
?find 2010/Whitmore/* -exec shasum {} \;
如果您的磁盘速度很快,您可能需要并行运行多个:
要了解有关 GNU Parallel 的更多信息,请观看介绍视频(如果您是天体物理学家,您会很高兴这样做):http://www.youtube.com/watch?v=OpaiGYxkSuQ
If your disks are fast you may want to run several in parallel:
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
有效吗
?
Does
work?
要解决文件名中空格字符的问题,请使用“\0”作为文件分隔符:
xargs
的-P
参数指定应运行多少个并发进程。To get around the trouble with space chars in filenames, use '\0' as file separator:
The
-P
argument toxargs
specifies how many concurrent processes it should run.而不是:
你应该使用
instead of:
you should use