bash 管道找找
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5773844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Piping find to find
提问by Harold Smith
I want to pipe a find result to a new find. What I have is:
我想将查找结果通过管道传输到新查找。我所拥有的是:
find . -iname "2010-06*" -maxdepth 1 -type d | xargs -0 find '{}' -iname "*.jpg"
Expected result: Second find receives a list of folders starting with 2010-06, second find returns a list of jpg's contained within those folders.
预期结果:第二次查找接收从 2010-06 开始的文件夹列表,第二次查找返回包含在这些文件夹中的 jpg 列表。
Actual result: "find: ./2010-06 New York\n: unknown option"
实际结果:“查找:./2010-06 纽约\n:未知选项”
Oh darn. I have a feeling it concerns the format of the output that the second find receives as input, but my only idea was to suffix -print0 to first find, with no change whatsoever.
哦该死。我有一种感觉,它与第二个查找作为输入接收的输出格式有关,但我唯一的想法是将 -print0 后缀为第一个查找,没有任何更改。
Any ideas?
有任何想法吗?
采纳答案by Chris Eberle
You need 2 things. -print0, and more importantly -I{}on xargs, otherwise the {}doesn't do anything.
你需要两件事。-print0,更重要的是-I{}在 xargs 上,否则{}什么都不做。
find . -iname "2010-06*" -maxdepth 1 -type d -print0 | xargs -0 -I{} find '{}' -iname '*.jpg'
回答by user unknown
Useless use of xargs.
xargs 的无用使用。
find 2010-06* -iname "*.jpg"
At least Gnu-find accepts multiple paths to search in. -maxdepth and type -d is implicitly assumed.
至少 Gnu-find 接受多个路径进行搜索。 -maxdepth 和类型 -d 是隐式假设的。
回答by drysdam
How about
怎么样
find . -iwholename "./2010-06*/*.jpg
etc?
等等?
回答by ghostdog74
Although you did say that you specifically want this find + pipe problem to work, its inefficient to fork an extra findcommand. Since you are specifying -maxdepth as 1, you are not traversing subdirectories. So just use a forloop with shell expansion.
尽管您确实说过您特别希望此 find + pipe 问题起作用,但是 fork 额外的find命令效率低下。由于您将 -maxdepth 指定为 1,因此您不会遍历子目录。所以只需使用for带有外壳扩展的循环即可。
for file in *2010-06*/*.jpg
do
echo "$file"
done
If you want to find all jpg files inside each 2010-06* folders recursively, there is also no need to use multiple findsor xargs
如果要递归查找每个 2010-06* 文件夹内的所有 jpg 文件,也无需使用多个finds或xargs
for directory in 2010-06*/
do
find $directory -iname "*.jpg" -type f
done
Or just
要不就
find 2006-06* -type f -iname "*.jpg"
find 2006-06* -type f -iname "*.jpg"
Or even better, if you have bash 4 and above
或者更好,如果你有 bash 4 及更高版本
shopt -s globstar
shopt -s nullglob
for file in 2010-06*/**/*.jpg
do
echo "$file"
done

