What does lsof do and when is it useful?

Answer

lsof (list open files) shows all files currently opened by processes — and in Linux, "everything is a file" (network sockets, pipes, devices). Key uses: lsof -i :8080 shows what process is listening on port 8080 (useful when you get "address already in use"). lsof -p PID lists all files opened by a specific process. lsof -u alice shows all files opened by a user. lsof /path/to/file shows which processes have a particular file open. lsof -i TCP -n -P lists all TCP connections without hostname resolution. lsof is invaluable for diagnosing port conflicts, finding which process is holding a file that can't be deleted, and investigating resource leaks.