How do I list all open files for a Linux or UNIX process using command line options? How can I show open files per process under Linux? Both Linux and Unix-like operating systems come with various utilities to find out open files associated with the process.
First use the ps command command to get PID of process, enter:$ ps -aef | grep {process-name} $ ps -aef | grep httpd Next pass this PID to pfiles command,$ pfiles {PID} $ pfiles 3533 See pfiles command documentation for more information
On FreeBSD use the fstat command along with the ps command:# ps aux | grep -i openvpn # filter outputs using the grep command # # fstat -p {PID} # fstat -p 1219 We can count open files count for openvpn process as follows using the wc command:# fstat -p 1219 | grep -v ^USER | wc -l The -p option passed to the fstat to report all files open by the specified process.
FreeBSD pstat command in action
First you need to find out PID of process. Simply use any one of the following command to obtain process id:# ps aux | grep {program-name} OR$ ps -C {program-name} -o pid= For example, find out PID of firefox web-browser, enter:$ ps -C firefox -o pid= Output:
7857To list opne files for firefox process, enter:$ ls -l /proc/7857/fd Sample output:
total 0 lr-x------ 1 vivek vivek 64 2008-03-05 22:31 0 -> /dev/null l-wx------ 1 vivek vivek 64 2008-03-05 22:31 1 -> pipe:[18371] lr-x------ 1 vivek vivek 64 2008-03-05 22:31 10 -> /usr/lib/firefox/firefox l-wx------ 1 vivek vivek 64 2008-03-05 22:31 2 -> pipe:[18371]For privileged process use the sudo command and to count open files use the wc command on Linux as follows:# Get process pid sudo ps -C Xorg -o pid sudo ls -l /proc/${pid-here}/fd # Say pid is 9497 for Xorg, then sudo ls -l /proc/9497/fd | wc -l We can use bash for loop as follows too:
# Linux Count and List Open Files for Nginx Process # SEARCH="nginx" for i in $(ps -C "${SEARCH}" -o pid | grep -v PID) do echo "PID # ${i} open files count : $(sudo ls -l /proc/${i}/fd | wc -l)" doneListing Open Files on Linux
The lsof command list open files under all Linux distributions or UNIX-like operating system. Type the following command to list open file for process ID 351:$ lsof -p 351 In this example display and count all open files for top 10 processes on Linux operating systems or server:# lsof | awk '{print $1}' | sort | uniq -c | sort -r | head ## force numeric sort by passing the '-n' option to the sort ## # lsof | awk '{print $1}' | sort | uniq -c | sort -r -n | head
3884 nginx 643 php-fpm7. 370 memcached 90 rsyslogd 81 systemd 63 systemd-j 58 systemd-r 55 systemd-n 50 systemd-l 48 networkd-Where,
lsof – Run the lsof to display all open files and send output to the awkawk '{print $1}' – Display first field i.e. process name onlyuniq -c – Omit duplicate lines while prefix lines by the number of occurrencessort -r – Reverse sorthead – Display top 10 process along with open files countNow you know how to find open files per process on Linux, FreeBSD, and Unix-like systems using various command-line options. See how to increase the system-wide/user-wide number of available (open) file handles on Linux for more information.
