Intermediate LevelLinux Commands & SysAdmin
How does lsof (List Open Files) identify processes holding file handles or network sockets open?
❓ Interview Prompt: Explain lsof (List Open Files), inspecting file handles, port holders (lsof -i :port), directory locks, and unlinked file recovery.
In Linux, everything is a file. 'lsof' (List Open Files) inspects open files, sockets, network ports, and devices across all active processes:
Common Use Cases:
•
Find Process Holding Port: 'lsof -i :8080'
•
Find Files Opened by Process: 'lsof -p <PID>'
•
Find Process Locking Directory: 'lsof +D /var/www/app'
•
Find Unlinked Deleted Files: 'lsof | grep deleted' (identifies deleted files still consuming disk space because a process holds them open!).
Code Implementation Example
bash
# Locate process listening on port 8080
sudo lsof -i :8080
# Find open files holding a specific directory locked
sudo lsof +D /var/log
# Identify deleted files still occupying disk space via open handles
sudo lsof | grep deleted💡Identifying open files, port holders, and deleted file handles using lsof.
🎯 Interview Key Takeaway
Use lsof -i :port to identify process owners of blocked ports, and lsof | grep deleted to find ghost disk consumption.
#lsof#Open Files#Debugging#Sockets#Intermediate
Back to all Linux Commands & SysAdmin Questions→