红穆笔记

lsof

列出打開的文件和相應的進程。 注意:列出其他人打開的文件需要 root 權限(或 sudo)

100% 瀏覽器本地處理 0 數據上傳

常用示例

查找打開了給定文件的進程

lsof {{路徑/到/文件}}

查找打開了本地互聯網端口的進程

lsof -i :{{端口}}

僅輸出進程 ID (PID)

lsof -t {{路徑/到/文件}}

列出給定用戶打開的文件

lsof -u {{用戶名}}

列出給定命令或進程打開的文件

lsof -c {{進程或命令的名稱}}

列出特定進程打開的文件(給定其 PID)

lsof -p {{pid}}

列出目錄中打開的文件

lsof +D {{路徑/到/目錄}}

查找正在監聽本地 IPv6 TCP 端口的進程,不轉換網絡或端口號

lsof -i6TCP:{{端口}} -sTCP:LISTEN -n -P

提示:花括號{{…}}是需要替換的參數(路徑、文件名等);命令只在本機終端執行,本站不保存任何輸入。

英文原文對照

展開英文說明與示例(來源 tldr-pages)

List open files and the corresponding processes. Note: Root privileges are required to list files opened by others. More information

Find the processes that have a given file open

lsof {{path/to/file}}

Find the process that opened a local internet port

lsof -i :{{port}}

Only output the process ID (PID)

lsof -t {{path/to/file}}

List files opened by the given user

lsof -u {{username}}

List files opened by the given command or process

lsof -c {{process_or_command_name}}

List files opened by a specific process, given its PID

lsof -p {{process_id}}

List open files in a directory

lsof +D {{path/to/directory}}

Find the process that is listening on a local IPv6 TCP port and don't convert network or port numbers

lsof -i6TCP:{{port}} -sTCP:LISTEN -n -P

命令信息

命令名lsof
用途列出打開的文件和相應的進程。 注意:列出其他人打開的文件需要 root 權限(或 sudo)
適用平臺通用
示例數量8 條
官方文檔https://manned.org/lsof
數據來源tldr-pages(CC-BY-4.0)· 社區中文翻譯

常見問題

lsof 命令是做什麼的?

列出打開的文件和相應的進程。 注意:列出其他人打開的文件需要 root 權限(或 sudo)

lsof 怎麼用?

頁面按場景列出了8 條常用示例:每條示例先說明用途,再給出可直接複製的完整命令(花括號裏是需要替換的參數)。

lsof 最常用的操作是什麼?

查找打開了給定文件的進程:lsof {{路徑/到/文件}}

命令裏的花括號是什麼意思?

花括號{{…}}表示需要替換成你自己的值,例如路徑、文件名;同名參數方括號(如 [-C|--directory])表示可選寫法。