FreeKB - Linux Commands lsof command
Linux Commands - lsof command

The lsof (list open files) command can be used, as the name suggests, to list the open files on the system. The lsof command without any options should return something like this. Notice in this example that many of the files have "Permission denied". This is because the lsof command in this example is being ran by john.doe instead of root.

[john.doe@server1 ~]$ lsof
COMMAND      PID     USER   FD      TYPE             DEVICE SIZE/OFF       NODE NAME
init           1     root  cwd   unknown                                        /proc/1/cwd (readlink: Permission denied)
init           1     root  rtd   unknown                                        /proc/1/root (readlink: Permission denied)
init           1     root  txt   unknown                                        /proc/1/exe (readlink: Permission denied)
init           1     root NOFD                                                  /proc/1/fd (opendir: Permission denied)
kthreadd       2     root  cwd   unknown                                        /proc/2/cwd (readlink: Permission denied)
kthreadd       2     root  rtd   unknown                                        /proc/2/root (readlink: Permission denied)
kthreadd       2     root  txt   unknown                                        /proc/2/exe (readlink: Permission denied)
kthreadd       2     root NOFD                                                  /proc/2/fd (opendir: Permission denied)
migration      3     root  cwd   unknown                                        /proc/3/cwd (readlink: Permission denied)

 

The lsof command can be piped through the wc (word count) command to determine how many files are open.

[john.doe@server1 ~]$ lsof | wc -l
1234

 

Often, the lsof command with the -u option is used to display the files opened by a certain user.

[john.doe@server1 ~]$ lsof -u john.doe | head
COMMAND      PID    USER   FD      TYPE             DEVICE  SIZE/OFF       NODE NAME
sshd      116079 john.doe cwd   unknown                                         /proc/116079/cwd (readlink: Permission denied)
sshd      116079 john.doe rtd   unknown                                         /proc/116079/root (readlink: Permission denied)
sshd      116079 john.doe txt   unknown                                         /proc/116079/exe

 

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 1d702 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |