Identify all open files in a directory – lsof command

How to check all opened files in a directory – lsof command?

LSOF is a smart linux command-line utility for analysing the information about the files which are opened by various processes.

LSOF stands for List of Open Files. This command has a vital role in server Administration. In Linux/Unix everything is considered as a file such as pipes, sockets, devices etc are treated as files.

Thus, by using ‘lsof’ command we will get the information of any opened files. For example, if you are trying to unmound a partition and you are getting the error “files are being used“. Here, you can easily identify the files which are in use by uding the LSOF command.

We already covered the lsof command switches in one of the previous article.

lsof command usages with example – Unix/Linux

lsof command usages is a mandatory thing that every sys admin must know. lsof command lists open files. It’s the short form of “LiSt Open Files.” This command has a vital role in server Administration.

This command reveals the files which are opened by a process. In Linux/Unix, everything is considered as a file such as pipes, sockets, devices etc are treated as files. Thus, by using ‘lsof‘ command we will get the information of any opened files.

Here I’m explaining the usage of LSOF command to list all opened file in a directory.

There is a switch “D” associated with LSOF command to do this. See the usage listed below:

# lsof +D path_of_the_directory

Example

[[email protected] ~]# lsof +D /var/log/
COMMAND    PID   USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
rsyslogd   464   root    1w   REG 182,33   139619 131530 /var/log/messages
rsyslogd   464   root    2w   REG 182,33   911817 131531 /var/log/secure
rsyslogd   464   root    4w   REG 182,33   372734 131529 /var/log/maillog
rsyslogd   464   root    5w   REG 182,33     9608 131512 /var/log/cron
httpd      629   root    2w   REG 182,33      743 131526 /var/log/httpd/error_log
httpd      629   root    6w   REG 182,33      170 131524 /var/log/httpd/access_log
httpd      636 apache    2w   REG 182,33      743 131526 /var/log/httpd/error_log
httpd      636 apache    6w   REG 182,33      170 131524 /var/log/httpd/access_log
httpd     1382 apache    2w   REG 182,33      743 131526 /var/log/httpd/error_log
httpd     1382 apache    6w   REG 182,33      170 131524 /var/log/httpd/access_log
lfd      29910   root    5r   REG 182,33   139619 131530 /var/log/messages
lfd      29910   root    6r   REG 182,33   911817 131531 /var/log/secure
lfd      29910   root    7r   REG 182,33      743 131526 /var/log/httpd/error_log

For more information about LSOF, please read this >> 10 lsof command usages with example <<

That’s it!

Also read:

1, 15+ tar command usages with examples – Unix/Linux
2, 10+ commonly using find command switches with example Unix/Linux
3, 20+ Rsync command’s switches and common usages with examples – Unix/Linux

, ,

Post navigation

Arunlal Ashok

Linux Systems Architect at Endurance International Group. Linux lover. Like to play on Linux console. I started this blog to share and discuss Linux thoughts.

Always happy for an open discussion! Write to arun (@) crybit (dot) com. Check about me for more details. About this blog and our strong members, check The team CryBit.com

One thought on “Identify all open files in a directory – lsof command

Leave a Reply

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