开发者

Piping find to find

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 th开发者_高级运维ose folders.

Actual result: "find: ./2010-06 New York\n: unknown option"

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.

Any ideas?


You need 2 things. -print0, and more importantly -I{} on xargs, otherwise the {} doesn't do anything.

find . -iname "2010-06*" -maxdepth 1 -type d -print0 | xargs -0 -I{} find '{}' -iname '*.jpg'


Useless use of xargs.

find 2010-06* -iname "*.jpg"

At least Gnu-find accepts multiple paths to search in. -maxdepth and type -d is implicitly assumed.


How about

find . -iwholename "./2010-06*/*.jpg 

etc?


Although you did say that you specifically want this find + pipe problem to work, its inefficient to fork an extra find command. Since you are specifying -maxdepth as 1, you are not traversing subdirectories. So just use a for loop with shell expansion.

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 finds or xargs

for directory in 2010-06*/
do
   find $directory -iname "*.jpg" -type f
done

Or just

find 2006-06* -type f -iname "*.jpg"

Or even better, if you have bash 4 and above

shopt -s globstar
shopt -s nullglob
for file in 2010-06*/**/*.jpg
do
    echo "$file"
done
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜