Linux Bash Command Line – How to Display Open File Descriptors Without Using lsof Command

bashcommand linelinux

Hi I have read Here that lsof is not an accurate way of getting the number of File Descriptors that are currently open. He recommended to use this command instead

 cat /proc/sys/fs/file-nr

While this command displays the number of FD's, how do you display the list of open file descriptors that the command above just counted?

Best Answer

There are two reasons lsof | wc -l doesn't count file descriptors. One is that it lists things that aren't open files, such as loaded dynamically linked libraries and current working directories; you need to filter them out. Another is that lsof takes some time to run, so can miss files that are opened or closed while it's running; therefore the number of listed open files is approximate. Looking at /proc/sys/fs/file-nr gives you an exact value at a particular point in time.

cat /proc/sys/fs/file-nr is only useful when you need the exact figure, mainly to check for resource exhaustion. If you want to list the open files, you need to call lsof, or use some equivalent method such as trawling /proc/*/fd manually.

Related Question