Some Useful LSOF commands

Some Useful LSOF commands

LSOF commands

LSOF is the short form of "LiSt Open Files".
It is a command line utility which is used to list the information about the files that are opened by various processes.
In unix, everything is considered as a file, ( pipes, sockets, directories, devices, etc.).
So by using lsof, you can get the information about any opened files.

1). List all open files:
This command will provide list of open files of all active processes.
------
lsof
------

By default One file per line is displayed. Most of the columns are self explanatory. We will explain the details about couple of cryptic columns (FD and TYPE).

FD – Represents the file descriptor. Some of the values of FDs are,

cwd – Current Working Directory
txt – Text file
mem – Memory mapped file
mmap – Memory mapped device
NUMBER – Represent the actual file descriptor. The character after the number i.e ‘1u’, represents the mode in which the file is opened. r for read, w for write, u for read and write.
TYPE – Specifies the type of the file. Some of the values of TYPEs are,



REG – Regular File
DIR – Directory
FIFO – First In First Out
CHR – Character special file
For a complete list of FD & TYPE, refer man lsof.

2). List user specific opened files:
-----
lsof -u user_name
-----

3). List Process running on a port:
-----
lsof -i :port_number
-----

4). Lists only IPv4 and IPv6 open files :
-----
lsof -i 4
lsof -i 6

-----

5). Lists open files for TCP port ranges 1-1024:
-----
lsof -i :1-1024
-----

6). Lists by pid:
-----
lsof -p PID
-----

7). Kill all activity for a particular user:
-----
killall -9 `lsof -t -u username`
-----

8). Lists Opened files in a directory:
-----
lsof +D /var/log/
-----

9). Lists Opened files based on process names:
-----
lsof -c process_name
-----

10). List all network connections:
-----
lsof -i
-----

11). Lists files Exclude User with ‘^’ Character:
-----
lsof -i -u^root
-----

12). List opened files based on process names starting with:
-----
lsof -c ssh -c init
-----

13). List all TCP or UDP connections:
-----
lsof -i tcp; lsof -i udp
-----

14). List all Network File System ( NFS ) files:
-----
lsof -N -u hoststud -a
-----

15). Combine more list options using OR/AND:
-----
lsof -u hoststud -c init
-----
Author
bhawanisingh
Views
2,733
First release
Last update
Rating
0.00 star(s) 0 ratings
Top