I think you may be looking for the strace
program, which can trace and log system calls. For example, I can ask for a list of files opened by the grep
command like this:
strace -e trace=open,openat grep something M* > /dev/null
The -e trace=open,openat
option tells strace
that we're only interested in those two system calls.
I've redirected the output of grep
to /dev/null
here to avoid cluttering the output; you could also send the output of strace
to a file using the -o
option.
Assuming I have the files MANIFEST.in
, MODULE_GUIDELINES.md
, and Makefile
in my local directory, this might produce output like:
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/lib64/libpcre.so.1", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/lib64/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
open("/usr/lib64/gconv/gconv-modules.cache", O_RDONLY) = 3
openat(AT_FDCWD, "Makefile", O_RDONLY|O_NOCTTY) = 3
openat(AT_FDCWD, "MANIFEST.in", O_RDONLY|O_NOCTTY) = 3
openat(AT_FDCWD, "MODULE_GUIDELINES.md", O_RDONLY|O_NOCTTY) = 3
+++ exited with 0 +++
You can see here both the shared libraries opened when running grep
as well as the files that were opened by grep
itself.
You can read more in the strace man page.