0

How to print all libraries in directories which use shared library (mylibrary)? I tried:

ldd /usr/mt-60/apl/651/*.so |grep libmylibrary

But output is without library names:

    libmylibrary.so => /usr/aaa/libmylibrary.so (0x40017000)
    libmylibrary.so => /usr/aaa/libmylibrary.so (0x40016000)
2

1 Answer 1

1
find /usr/mt-60/apl/651/*.so -exec sh -c 'ldd "$1" | grep -q libmylibrary' sh {} \; -print

The trick is -exec is also a test in find. In this case it tests the exit status of sh which is the exit status of grep; so -print will only work if grep finds anything.

Notes:

  • We need inner sh to make the inner pipe work.
  • Here all paths start with /usr/mt-60/apl/651/, so there will be no path that looks like an option (e.g. -L). In general you may want to use find -- … and ldd -- … in case there is such a path. It would be relevant if your pattern started with * or ? or literal - (another way to deal with such unhandy pattern is to prepend ./ to it).
  • The * wildcard is expanded by the (outer) shell, in general find will get multiple arguments before -exec. This may lead to argument list too long (or no such file or directory if there is no match). For this reason you may want find to do the matching:

    find /usr/mt-60/apl/651/ -type f -name "*.so" …
    

    where * is not expanded by the shell because it's quoted. Note this approach is recursive (investigate -maxdepth or see this question if it's a problem).

  • Omit -q to see the output from grep before the path to the respective file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .