Linux Terminal Command: lsof
The lsof command is an essential tool in System Information & Hardware. In this tutorial, we will explore what lsof does, look at everyday examples, and cover advanced options to supercharge your command-line workflow.
Concept & Explanation
The lsof command lists all open files and active network sockets on the system, including the processes that opened them.
Common Options & Syntax
lsof [options] [arguments]
Here are the most common flags used with lsof:
- Simple Usage: Basic default commands.
- Detailed View: Shows diagnostic information.
- Advanced Actions: Can chain parameters for scripting.
1. Interactive Example (Simple)
Here is how most people run the command:
# Example
lsof /var/log/syslog
What it does: Lists the active processes that have the syslog file open.
2. Power-User Example (Advanced)
For scripting and advanced diagnostics, use this configuration:
# Advanced
sudo lsof -i :8080 -t | xargs kill -9
What it does: Locates the process running on network port 8080 (-i :8080), returns only the process PID (-t), and terminates it immediately.
⚙️ Warning & Common Pitfalls
[!WARNING]
lsofmust query system tables and can be slow. Requiressudoprivileges to view files opened by other users.
🔗 Related Commands
Here are some related posts on cli_tty1 you might want to check out:
- sudo : Execute a command as another user (typically superuser).