Bash scripting files with find and xargs
When I run shasum from a several direc开发者_如何学JAVAtories 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
How about using the -exec
option of find
?
find 2010/Whitmore/* -exec shasum {} \;
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
Does
find -type f | sed 's/./\\&/g' | xargs shasum
work?
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.
instead of:
find 2010/Whitmore/* | xargs -0 | shasum
you should use
find 2010/Whitmore/* | xargs shasum
精彩评论