Linux Tutorial
Linux File/Directory Management
Linux Packaging And Compression
Vim Text Editor
Linux Text Processing
Linux Software Installation
Linux User/User Group Management
Linux Permission Management
Linux Filesystem Management
Linux Advanced Filesystem Management
Linux System Management
Linux Backup and Recovery
Linux System Service Management
Linux System Log Management
Linux Boot Management
LAMP/LNMP Environment
SELinux Management
The lsof
command (short for "list open files") is a powerful utility in Linux that allows you to view information about open files and the processes that are using them. Files in this context can include regular files, directories, block devices, and network connections. In this tutorial, we'll discuss the basics of the lsof
command and its usage.
1. Basic Usage
To use lsof
without any options, simply type lsof
in the terminal:
lsof
This command will list all open files and display information such as the process ID (PID), user, file descriptor, file type, and file name.
2. Common Options
lsof
provides several options to filter and customize the output. Here are some commonly used options:
-u
(user): Lists open files belonging to the specified user.
lsof -u username
-c
(command): Lists open files for processes whose names match the given pattern (case-insensitive).
lsof -c process_name
-p
(PID): Lists open files for the specified process ID.
lsof -p process_id
-i
(internet): Lists open network connections. You can provide an optional protocol and address to filter the results further.
lsof -i # Lists all open network connections lsof -i TCP # Lists only TCP connections lsof -i :22 # Lists connections on port 22
-d
(file descriptor): Lists open files that use the specified file descriptor. Common file descriptors are cwd
(current working directory), txt
(text file), mem
(memory-mapped file), and 0
, 1
, 2
(standard input, output, and error).
lsof -d txt
-t
(terse): Provides a terse output with just the process IDs, useful when scripting.
lsof -t -i :22
3. Examples
Here are a few examples of how to use the lsof
command:
List all open files by a specific user:
lsof -u username
List all processes using a specific file:
lsof /path/to/file
Find the process that is using a specific port:
lsof -i :port_number
List all open files in a specific directory:
lsof +D /path/to/directory
Kill all processes that belong to a specific user:
kill -9 $(lsof -t -u username)
In summary, the lsof
command is a versatile tool for listing and managing open files and network connections in Linux. Understanding how to use lsof
effectively will help you troubleshoot issues, identify resource usage, and better manage system processes. This tutorial provides an introduction to using the lsof
command, and the examples can be adapted to suit your specific needs.
How to use lsof in Unix:
Use the lsof
command to list open files and associated information:
lsof
List open files with lsof command:
Display a list of open files and processes using the lsof
command:
lsof /path/to/directory
lsof command examples and options:
-i
: Display open network connections.-p
: Specify a process ID.-u
: Specify a username.-c
: Specify a command name.Example:
lsof -i
Viewing network connections with lsof in Linux:
Use the -i
option to show open network connections:
lsof -i
Identifying processes using specific files with lsof:
Identify processes using a specific file or directory:
lsof /path/to/file
Filtering lsof output by process or file:
Filter lsof
output using options like -p
for a specific process or specifying a file path:
lsof -p <pid>
lsof command for file descriptor information:
Display file descriptor information using the -d
option:
lsof -d <fd>
Checking open files and sockets in Linux with lsof:
Use lsof
to check open files, directories, and network sockets:
lsof /path/to/directory
lsof vs netstat: comparing network-related tools:
lsof
: Lists open files and associated processes.netstat
: Displays network-related information, including open ports and connections.Example comparing both:
lsof -i netstat -an