📂 Command: lsof (LiSt Open Files)
In Linux, "everything is a file."
The lsof command is a powerful diagnostic tool that shows you exactly which files are being opened by which processes.
1. The "Why"
For an Arch Linux developer and power user, lsof is the ultimate "who is using this?" tool:
- The "Device Busy" Error: Ever tried to unmount a USB drive or delete a folder only to get "Device or resource busy"?
lsoftells you which app is holding it hostage. - Port Conflicts: If you're starting a local instance or a web server and get an "Address already in use" error,
lsofidentifies the culprit. - Security Auditing: See every network connection currently open on your system to ensure no unauthorized processes are communicating.
- Debugging: Monitor which libraries or configuration files your * app is loading at runtime.
2. Basic Syntax
lsof [OPTIONS]
Running it without options lists every open file on the system (it's a massive list).
3. Practical Examples for Your Workflow
A. Find Who is Using a Specific Folder
If you can't move or delete your project folder:
lsof +D /home/user/Documents/Preader/
B. Find Who is Using a Port
This is the most common use for developers. To see what is running on port 8080:
lsof -i :8080
You can then use kill -9 [PID] to stop that process and free the port.
C. List Files Opened by a Specific User
See everything your user account is currently touching:
lsof -u user
D. List Files Opened by a Specific Program
Check what your Java environment is doing:
lsof -c java
E. Finding Internet Connections (-i)
To see all active network connections (IPv4 and IPv6):
lsof -i
4. Understanding the Columns
| Column | Meaning |
|---|---|
| COMMAND | The name of the process (e.g., java, python, firefox). |
| PID | The Process ID (use this to kill the process). |
| USER | The user who owns the process. |
| FD | File Descriptor (cwd: current working directory, txt: program text/code). |
| TYPE | Type of file (REG: regular file, DIR: directory, IPv4: network). |
| NODE | The inode number on the disk. |
| NAME | The actual path or network address of the file. |
5. Pro-Tips
- Arch Linux Context: If you're trying to update your system and
pacmansays the database is locked, you can find the culprit with:lsof /var/lib/pacman/db.lck. - Combined Flags: You can use
-a(AND) to narrow things down. For example, to find files opened by useruserAND the commandjava:lsof -a -u user -c java - Recovery: If you accidentally delete a file that is still being written to by a process (like a log file),
lsofcan sometimes help you find the file descriptor in/proc/to recover the data.
6. Summary Reference
| Goal | Command |
|---|---|
| Find port owner | lsof -i :[port] |
| Find folder owner | lsof +D [/path/to/dir] |
| List by PID | lsof -p [PID] |
| All network files | lsof -i |