How to find which files are opened by a Linux program?

How 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?

asked Apr 29, 2014 by Eric Z Ma (44,280 points)

1 Answer

 
Best answer

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).

answered Apr 29, 2014 by Eric Z Ma (44,280 points)

Please log in or register to answer this question.

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...