-
-
Notifications
You must be signed in to change notification settings - Fork 1
LSOF List Open Files
Mattscreative edited this page Dec 5, 2025
·
2 revisions
- Find What's Using a Port
- Find What's Using a File
- Find All Files for a User
- Find Process Preventing Unmount
-
lsof(List Open Files) shows which processes have files open - Displays files, directories, network connections, and devices in use
- Essential for troubleshooting "file in use" errors
- Usually requires root privileges for full information
What lsof can do:
- Find what process is using a file
- Show all files opened by a process
- Find processes using a port
- Display network connections
- Troubleshoot file locking issues
sudo lsofWhat this does:
- Shows all open files for all processes
- Very long output
- Usually filtered or used with specific options
Note: May require sudo for full information.
sudo lsof /path/to/fileWhat this does:
- Shows which process has the file open
- Useful for "file in use" errors
- Helps identify what's preventing file operations
Example:
sudo lsof /var/log/syslogsudo lsof +D /path/to/directoryWhat this does:
- Shows processes using files in a directory
- Recursive (includes subdirectories)
- Useful for unmounting filesystems
sudo lsof -p 1234What this does:
- Shows all files opened by process ID 1234
- Useful for seeing what a process is doing
- Helps identify resource usage
sudo lsof -c processnameWhat this does:
- Shows files opened by processes matching the name
- Partial matches work
- Useful for finding all instances of a program
Example:
sudo lsof -c nginxsudo lsof -i :80What this does:
- Shows which process is using port 80
- Very useful for "port already in use" errors
- Alternative to
ssornetstat
Example:
sudo lsof -i :22 # SSH port
sudo lsof -i :443 # HTTPS portsudo lsof -iWhat this does:
- Shows all network connections
- Displays TCP and UDP connections
- Similar to
ssornetstat
sudo lsof -iTCPWhat this does:
- Shows only TCP connections
- Filters out UDP
sudo lsof -iTCP -sTCP:LISTENWhat this does:
- Shows only listening TCP ports
- Useful for finding open services
- Similar to
ss -tln
sudo lsof -i :80Shows process using port 80.
sudo lsof /path/to/fileShows process with file open.
sudo lsof -u usernameShows all files opened by a user.
sudo lsof +D /mntShows what's preventing unmounting /mnt.
sudo lsof # All open files
sudo lsof /path/to/file # File in use
sudo lsof +D /path/to/dir # Directory in use
sudo lsof -p 1234 # Process files
sudo lsof -c processname # Process name
sudo lsof -i :80 # Port in use
sudo lsof -iTCP -sTCP:LISTEN # Listening ports
sudo lsof -u username # User's filesFor network connections, see the SS Network Troubleshooting Guide. For processes, see the PS Process Management Guide.