lsof – command

lsof command lists 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.

#lsof

You can see some columns such as command, PID, user, FD, TYPE etc

FD – File descriptor

Some of the values in FD are :-
cwd – Current working directory
txt – Text file
mem – Memory Mapped file
mmap – Memory Mapped device
Number – It represent the actual file descriptor. For example, 0u, 1w and 3r

r = represents read, w = represents write and u = represents both read and write.

Type represents type of the file. Some of the values of TYPE are :-

>REG – Regular file
>DIR – Directory
>CHR – Character special file
>FIFO – First in first out

To list user specific opened files

#lsof -u user_name

Process running on a port

# lsof -i :port_number

Lists only IPv4 and IPv6 open files

#lsof -i 4 – For IPv4
#lsof -i 6 – For IPv6

Lists open files for TCP port ranges 1-1024

#lsof -i :1-1024

Lists by pid

#lsof -p PID

Kill all activity for a particular user

#killall -9 `lsof -t -u username`

Opened files in a directory

#lsof +D path_of_the_directory

Opened files based on process names

#lsof -c process_name

List all network connections

#lsof -i

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s