How to find which files are opened by a Linux program?
Posted on In QAHow to find which files are opened by a Linux program? For example, when I run cat ~/.bashrc
, how to find out which files are opened by cat
?
You can achieve this by using strace
if the program “open” files using system calls.
For example, I run as this:
strace -o /tmp/st cat ./.bashrc
grep "^open" /tmp/st
It will prints out:
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
open("./.bashrc", O_RDONLY) = 3
These files are opened. You can inspect the trace file (/tmp/st) to check all system called invoked by the command (cat here).