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)
wair92
  • 301
  • 1
  • 2
  • 9
  • Also asked at https://askubuntu.com/q/1139415/10127 -- stackoverflow is probably the better forum to ask. – glenn jackman Apr 30 '19 at 16:09
  • If you would use FreeBSD you can use `ldd -f '%A %o\n'` (https://www.freebsd.org/cgi/man.cgi?query=ldd). As I see the GNU `ldd` doesn't have `-f`. – uzsolt Apr 30 '19 at 19:55

1 Answers1

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.
Kamil Maciorowski
  • 69,815
  • 22
  • 136
  • 202