How do I get a list of shared library filenames under Linux?
If you want to catch them all, there is no other choice but to do full filesystem traversals. ldconfig
knows only about the libraries in the standard paths and the extra paths it is configured to look in (usually defined in /etc/ld.so.conf*
). The usual suspect places where other libraries can be found are $HOME
and /opt
, though there is no limit, especially when people build applications themselves.
If you don't trust the output of ldconfig -p
, you can parse its cache directly. It's binary, but using strings
removes all the garbage (5 so /lib/ matches):
strings -n5 /etc/ld.so.cache
On my system, they both give the same results, which I verified with this quicky:
a=$(ldconfig -p | awk -F'> ' '{print $2}' | sort); # get them to the same format
b=$(strings -n5 /etc/ld.so.cache | sort -u);
echo -e "$a\n$b\n$b" | sort | uniq -u
Which checked if any library on the ldconfig -p
list was not mentioned in the cache. Nothing was returned.
Related videos on Youtube
ephsmith
Updated on September 18, 2022Comments
-
ephsmith almost 2 years
Is there a GNU Linux built-in for getting a list of the filenames of shared libraries? I have considered parsing the output of
ldconfig -p
. However, I'm not sure about consistency in output from system to system.I am already aware that I could use
find
but given that the linker / loader has a cache of libraries and locations, it seems more efficient to use it in some way. Additionally, the paths that the linker / loader searches are the only ones I'm concerned with--i.e libraries that have been processed byldconfig
. -
ephsmith almost 12 yearslynxlynxlynx, I appreciate your thoughts and I understand that perspective. I should limit the scope of my question--I'll edit for clarity. In this case, we are concerned primarily with what
ldconfig
knows about. -
lynxlynxlynx almost 12 yearsI hope it is not a security measure, since you have
LD_LIBRARY_PATH
,LD_ADD
and probably others that can practically circumvent the original list. -
ephsmith almost 12 yearsYou bring up a good point. This is not a security measure. It's one approach in an effort to support some legacy scripts that depend libs that could be in one of several different locations.
-
ephsmith almost 12 yearsGreat update! Parsing the cache directly this way really helps.