Linux / UNIX List Open Files for Process

See all UNIX related articles/faq
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.
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements none|Linux terminal
Category Processes Management
OS compatibility AlmaLinux Alpine Amazon Linux Arch CentOS Debian Fedora FreeBSD Linux macOS Mint Mint openSUSE Pop!_OS RHEL Rocky Slackware Solaris Stream SUSE Ubuntu Unix
Est. reading time 3 minutes

UNIX List Open Files For Process

First use the ps command command to get the PID of process, enter:
$ ps -aef | grep {process-name}
$ ps -aef | grep httpd

Next pass this PID to the pfiles command under Solaris Unix:
$ pfiles {PID}
$ pfiles 3533

See the pfiles command documentation> for more information or type the following man command:
% man pfiles

FreeBSD list open files per process

On FreeBSD use the fstat command along with the ps command. For example:
# ps aux | grep -i openvpn # filter outputs using the grep command #
# fstat -p {PID}
# fstat -p 1219

We can count open files 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 show open files per process command

FreeBSD pstat command in action

Linux List Open Files For Process

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 the PID of firefox web-browser, enter:
$ ps -C firefox -o pid=
Output:

 7857

To list opens files for the Firefox process with PID # 7857, enter:
$ ls -l /proc/7857/fd
Sample outputs:

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]

You can filter out required info using the grep command or egrep command:
$ ls -l /proc/7857/fd | grep firefox
$ ls -l /proc/7857/fd | grep -E 'regex-as-per-your-need'

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)"
done
Ubuntu Linux List Open Files for Process Bash Command

Listing Open Files on Linux

Using the lsof command to display the processes using the most file handles

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
Again, you can filter out required information from the lsof outputs using the grep command or egrep command:
$ lsof -p 351 | grep foo
$ lsof -p 351 | grep -E 'regex-as-per-your-need'

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 awk
  • awk '{print $1}' – Display first field i.e. process name only
  • uniq -c – Omit duplicate lines while prefix lines by the number of occurrences
  • sort -r – Reverse sort
  • head – Display top 10 process along with open files count

Conclusion

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

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

26 comments… add one
  • zersh Jun 7, 2021 @ 8:55

    There for multiple processes with the same name

    lsof | awk '{print $2, $1}' | sort | uniq -c | sort -r -n | head -n 20

    Outputs:

    Count, PID,Name
      20201 2748 thunderbid
      19224 2819 chrome
      13143 2868 chrome
      11397 1541 skypeforlinux
      10374 9893 vk
       8132 1854 skypeforlinux
       7308 9932 vk
       5038 9979 vk
       4522 10430 sublime_text
       4233 9978 Telegram
       3798 2545 konsole
    
  • Conualfy May 16, 2024 @ 12:26

    It would be nice to also add some filtering. In my case, I needed to see the status on my Nautilus huge collection copy-paste process. Normally, the output of both ls and lsof show a lot of other files, not only the copied ones. Change the PID (in my case, 12092) and part of the path (for me media/D) to your liking:

    ls -l /proc/12092/fd | grep -i 'media\/D'

    Or, with lsof:

    lsof -p 12092 | grep -i 'media\/D'
    • 🛡️ Vivek Gite (Author and Admin) Vivek Gite May 16, 2024 @ 19:36

      Added. Thank you for the feedback.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.