1. lsof簡介
2. 輸出列
2.1 FD 文件描述符
2.2 Type 文件類型
3. 經常使用選項
4. 示例
4.1 manpage-Examples
5. 特別用法
5.1 恢復刪除的文件
5.2 殺死某個特定用戶的全部活動
5.3 找回被刪除文件佔用的空間
1. lsof簡介
lsof(list open files)列出當前系統打開的文件。進程打開的文件;打開文件的進程;進程打開的端口(TCP,UDP);等...
因爲Linux系統一切皆文件,因此lsof打開的文件能夠是:
a regular file, 常規文件
a directory, 目錄
a block special file, 塊特殊文件
a character special file, 字符特殊文件
an executing text reference, 執行文本參考,
a library, 庫文件
a stream or a network file (Internet socket, NFS file or UNIX domain socket.) 流或網絡文件(網絡socket,NFS文件, UNIX域socket)
A specific file or all the files in a file system may be selected by path.
可用選項: $ lsof -h
詳細說明: $ man lsof
2. 輸出列
Lsof每次運行時動態調整輸出列的大小,保證每列的最小大小。它還保證每列與其前一列至少隔開一個空格。Lsof不會爲每一個進程或文件集生成全部字段,只會生成可用的字段。某些字段是互斥的:
- 文件設備字符和文件主要/次要設備號;
- 文件inode編號和協議名稱;
- 文件名和流識別;
- 文件大小和偏移量。(-s/-o)
$ lsof |less
COMMAND 命令名(默認長度爲9,+c w可設置長度,arch linux最大15);
PID 進程ID(-p 指定PID);
TID 線程ID;
TASKCMD 任務名(-K?);
PPID 父進程號(僅在使用-R時顯示)
PGID 與進程關聯的進程組號(僅在使用-g時顯示)
USER 用戶(-u 指定用戶);
FD 文件描述符 (詳見下表)(-d 排除或包含FD字段);
TYPE 與文件關聯的節點的類型 (詳見下表);
當n被指定爲+f時,NODE-ID(或某些方言的INODE-ADDR)包含文件節點的惟一標識符(一般是內核vnode或inode地址,但有時也是設備和節點號的串聯);
DEVICE 指定磁盤的名稱;
SIZE/OFF 文件的大小/偏移量(-s/-o);
NLINK 包含指定+L時的文件連接計數;
NODE 索引節點(文件在磁盤上的標識);
NAME 打開文件的確切名稱;
2.1 FD 文件描述符
FD | FD是文件的文件描述符編號或: | wc -l | |
cwd | current working directory; | 當前工做目錄 | 381 |
txt | program text (code and data); | 程序文件或共享庫 | 381 |
rtd | root directory; | root目錄 | 381 |
mem | memory-mapped file; | 內存映射文件; | 36728 |
mmap | memory-mapped device; | 內存映射設備; | |
Lnn | library references (AIX); | 庫引用(AIX); | |
jld | jail directory (FreeBSD); | jail目錄(FreeBSD); | |
ltx | shared library text (code and data); | 共享庫文本(代碼和數據); | |
Mxx | hex memory-mapped type number xx. | 十六進制內存映射類型號xx。 | |
m86 | DOS Merge mapped file; | DOS合併映射文件; | |
err | information error (see NAME column); | 錯誤的FD信息錯誤; | |
pd | parent directory; | 父目錄; | |
tr | kernel trace file (OpenBSD); | 內核跟蹤文件(OpenBSD); | |
v86 | VP/ix mapped file; | VP/ix映射文件; | |
DEL | 3743 | ||
Mode | FD後跟其中一個字符,描述文件打開的模式: | ||
u | u for read and write access; | 文件處於讀取/寫入模式 | 128:5678 |
r | r for read access; | 文件處於只讀模式 | 78:2924 |
w | w for write access; | 文件處於寫入模式 | 92:2633 |
' ‘ | space if mode unknown and no lock | 未知模式,且未鎖定 | |
- | ’-’ if mode unknown and lock | 未知模式,並鎖定 | |
Lock | 模式字符後跟一個鎖定字符,描述應用於文件的鎖定類型: | ||
N | N for a Solaris NFS lock of unknown type; | 未知類型的Solaris NFS鎖; | |
r | r for read lock on part of the file; | r用於部分文件的讀鎖定; | |
R | R for a read lock on the entire file; | R表示整個文件的讀鎖定; | |
w | w for a write lock on part of the file; | w對文件的一部分進行寫鎖定; | |
W | W for a write lock on the entire file; | W表示整個文件的寫鎖定; | |
u | u for a read and write lock of any length; | u表示任何長度的讀寫鎖; | |
U | U for a lock of unknown type; | U表示未知類型的鎖; | |
x | x for an SCO OpenServer Xenix lock on part of the file; | 部分文件的SCO OpenServer Xenix鎖; | |
X | X for an SCO OpenServer Xenix lock on the entire file; | 整個文件的SCO OpenServer Xenix鎖; | |
' ‘ | space if there is no lock. | 空格, 沒有鎖。 |
2.2 Type 文件類型
TYPE | is the type of the node associated with the file | TYPE是與文件關聯的節點的類型 | wc-l |
BLK | for a block special file | 用於塊特殊文件 | 1 |
CHR | for a character special file | 用於字符特殊文件 | 780 |
DIR | for a directory | 用於目錄 | 770 |
REG | for a regular file | 用於常規文件 | 46350 |
FIFO | for a FIFO special file | 用於FIFO特殊文件 | 903 |
IPv4 | for an IPv4 socket | 用於IPv4套接字 | 7 |
IPv6 | for an open IPv6 network file - even if its address is IPv4, mapped in an IPv6 address | 用於開放的IPv6網絡文件 - 即便其地址是IPv4,也映射在IPv6地址中 | 22 |
sock | for a socket of unknown domain | 用於未知域的套接字 | 217 |
unix | for a UNIX domain socket | unix套接字的 | 2925 |
a_inode | 1384 | ||
netlink | 134 | ||
unknown | 72 | ||
DEL | for a Linux map file that has been deleted | 已刪除的Linux映射文件的 | |
LINK | for a symbolic link file | 用於符號連接文件 | |
MPB | for a multiplexed block file | 用於多路複用塊文件 | |
MPC | for a multiplexed character file | 用於多路複用字符文件 | |
ax25 | for a Linux AX.25 socket | 用於Linux AX.25套接字 | |
inet | for an Internet domain socket | 用於Internet域套接字 | |
lla | for a HP-UX link level access file | 用於HP-UX鏈路級訪問文件 | |
rte | for an AF_ROUTE socket | 用於AF_ROUTE套接字 | |
x.25 | for an HP-UX x.25 socket | 用於HP-UX x.25套接字 | |
DOOR | for a VDOOR file | VDOOR文件的 | |
KQUEUE | for a BSD style kernel event queue file | 用於BSD樣式的內核事件隊列文件 | |
NOFD | for a Linux /proc/<PID>/fd directory that can't be opened -- the directory path appears in the NAME column, followed by an error message | 對於沒法打開的Linux/proc/<PID>/fd目錄的「NOFD - 目錄路徑出如今NAME列中,後跟一條錯誤消息 | |
PAS | for a /proc/as file | 用於/proc/as文件 | |
PAXV | for a /proc/auxv file | 用於/proc/auxv文件 | |
PCRE | for a /proc/cred file | 用於/proc/cred文件 | |
PCTL | for a /proc control file | 用於/proc控制文件 | |
PCUR | for the current /proc process | 用於當前/proc過程 | |
PCWD | for a /proc current working directory | 用於/proc當前工做目錄 | |
PDIR | for a /proc directory | 做爲/proc目錄 | |
PETY | for a /proc executable type (etype) | 用於/proc可執行類型(etype) | |
PFD | for a /proc file descriptor | 用於/proc文件描述符 | |
PFDR | for a /proc file descriptor directory | 用於/proc文件描述符目錄 | |
PFIL | for an executable /proc file | 表示可執行文件/proc文件 | |
PFPR | for a /proc FP register set | 用於/proc FP寄存器組 | |
PGD | for a /proc/pagedata file | 用於/proc/pagedata文件 | |
PGID | for a /proc group notifier file | 用於/proc組通知程序文件 | |
PIPE | for pipes | 管道 | |
PLC | for a /proc/lwpctl file | 表示/proc/lwpctl文件 | |
PLDR | for a /proc/lpw directory | 用於/proc/lpw目錄 | |
PLDT | for a /proc/ldt file | 用於/proc/ldt文件 | |
PLPI | for a /proc/lpsinfo file | 用於/proc/lpsinfo文件的 | |
PLST | for a /proc/lstatus file | 用於/proc/lstatus文件 | |
PLU | for a /proc/lusage file | 用於/proc/lusage文件 | |
PLWG | for a /proc/gwindows file | 用於/proc/gwindows文件 | |
PLWI | for a /proc/lwpsinfo file | 用於/proc/lwpsinfo文件 | |
PLWS | for a /proc/lwpstatus file | 用於/proc/lwpstatus文件 | |
PLWU | for a /proc/lwpusage file | 用於/proc/lwpusage文件 | |
PLWX | for a /proc/xregs file | 用於/proc/xregs文件 | |
PMAP | for a /proc map file (map) | 用於/proc映射文件(map) | |
PMEM | for a /proc memory image file | 用於/proc內存映像文件 | |
PNTF | for a /proc process notifier file | 用於/proc進程通知程序文件 | |
POBJ | for a /proc/object file | 用於/proc/object文件 | |
PODR | for a /proc/object directory | 用於/proc/object目錄 | |
POLP | for an old format /proc light weight process file | 用於舊格式/proc輕量級處理文件 | |
POPF | for an old format /proc PID file | 用於舊格式/proc PID文件 | |
POPG | for an old format /proc page data file | 用於舊格式/proc頁面數據文件 | |
PORT | for a SYSV named pipe | SYSV命名管道的 | |
PREG | for a /proc register file | 用於/proc寄存器文件 | |
PRMP | for a /proc/rmap file | 用於/proc/rmap文件 | |
PRTD | for a /proc root directory | 用於/proc根目錄 | |
PSGA | for a /proc/sigact file | 用於/proc/sigact文件 | |
PSIN | for a /proc/psinfo file | 用於/proc/psinfo文件 | |
PSTA | for a /proc status file | 用於/proc狀態文件 | |
PSXSEM | for a POSIX semaphore file | 用於POSIX信號量文件 | |
PSXSHM | for a POSIX shared memory file | 用於POSIX共享內存文件 | |
PTS | for a /dev/pts file | 用於/dev/pts文件 | |
PUSG | for a /proc/usage file | 用於/proc/usage文件 | |
PW | for a /proc/watch file | 用於/proc/watch文件 | |
PXMP | for a /proc/xmap file | 用於/proc/xmap文件 | |
SMT | for a shared memory transport file | 用於共享內存傳輸文件 | |
STSO | for a stream socket | 用於流套接字 | |
UNNM | for an unnamed type file | 用於未命名的類型文件 | |
XNAM | for an OpenServer Xenix special file of unknown type | 用於未知類型的OpenServer Xenix特殊文件 | |
XSEM | for an OpenServer Xenix semaphore file | 用於OpenServer Xenix信號量文件 | |
XSD | for an OpenServer Xenix shared data file | 用於OpenServer Xenix共享數據文件 | |
or the four type number octets if the corresponding name isn't known. | 若是相應的名稱未知,則爲四個類型數字八位字節。 |
3. 經常使用選項
-a And運算-b
+c w command寬度
-c command命令或程序
+d dir目錄
-d FD列內容的篩選
-D dir遞歸目錄
-p PID進程號
Defaults in parentheses; comma-separated set (s) items; dash-separated ranges. | 括號中的默認值; 逗號分隔的集合項目; 短劃線範圍。 | |
-?|-h | list help | 列表幫助 |
-v | list version info | 列表版本信息 |
-a | AND selections (OR) | AND選擇(OR) |
-c c | cmd c ^c /c/[bix] | cmd c ^ c / c / [bix] |
+c w | COMMAND width (9) | 命令寬度(9)最大15? |
-d s | select by FD set | 輸出列表中排除或包含的文件描述符(FD)列表。 文件描述符在逗號分隔集合s中指定 – 例如, ''cwd,1,3'',''^6,^2''.(集合中不該有空格) |
+d s | dir s files | dirs文件 注:不含符號連接,除非使用-x或-x l選項可包含 |
+D D | dir D tree *SLOW?* | dir D tree * SLOW?* 遞歸列出目錄下被打開的文件 |
-u s | exclude(^)|select login|UID set s | exclude(^)| |
-i | select IPv[46] files | 選擇IPv[4,6]tcp,udp等文件 |
-n | no host names | #不將IP地址轉換爲hostname,預設是轉換的 |
-P | no port names | #此參數禁止將port number轉換爲service name,預設爲轉換 |
-l | list UID numbers | 列出UID號碼, 禁止將UID轉換爲登陸名。 |
-t | terse listing | 簡潔列表, 只輸出PID |
-s | list file size | 列表文件大小 |
-o | list file offset | 它會將SIZE/OFF輸出列標題更改成OFFSET 偏移量 |
-U | select Unix socket | 選擇Unix套接字 |
+|-e s | exempt s *RISKY* | 免除路徑名爲s的文件系統受到可能阻塞的內核函數調用。 +e選項免除stat(2),lstat(2)和大多數readlink(2)內核函數調用. -e選項僅免除stat(2)和lstat(2)內核函數調用. 可使用單獨的+|-e規範指定多個文件系統,而且每一個文件系統能夠具備免除或不容許的readlink(2)調用. |
+|-w | Warnings (+) | 警告(+) |
-p s | exclude(^)|select PIDs | exclude(^)|選擇PID |
-R | list paRent PID | 列出PPID |
-g [s] | exclude(^)|select and print process group IDs | exclude(^)|選擇並打印進程組ID |
+|-L [l] | list (+) suppress (-) link counts < l (0 = all; default = 0) | 啓用(+)或禁用(-)文件連接計數列表,它們可用-例如,它們不適用於套接字或大多數FIFO和管道. 若是指定+L且沒有跟數字, 則將列出全部連接計數。 指定-L(默認值)時,不會列出任何連接計數。 當+L後跟一個數字時,只列出連接數小於該數字的文件。(沒有數字能夠跟隨-L) |
4. 示例
COMMAND | PID | USER | FD | TYPE | DEVICE | SIZE/OFF | NODE | NAME |
$ lsof /home/toma/Documents/IO.xls | 查找打開該文件的進程 | |||||||
soffice.b | 20084 | toma | 3uW | REG | 8,23 | 242688 | 1480016 | /home/toma/Documents/IO.xls |
$ lsof /home/toma/Downloads/mov/GoT.mp4 | ||||||||
Thunar | 676 | toma | 25u | REG | 8,23 | 11121302 | 1705712 | /home/toma/Downloads/mov/GoT.mp4 |
vlc | 7975 | toma | 22r | REG | 8,23 | 11121302 | 1705712 | /home/toma/Downloads/mov/GoT.mp4 |
$ lsof -c soffice |grep .xls | 查找該進程打開的 *.xls 文件 | |||||||
soffice.b | 20084 | toma | 3uW | REG | 8,23 | 242688 | 1480016 | /home/toma/Documents/IO.xls |
$ lsof -c vlc |grep .mp4 | 查找該進程打開的 *.mp4 文件 | |||||||
vlc | 7975 | toma | 23r | REG | 8,23 | 11121302 | 1705712 | /home/toma/Downloads/mov/GoT.mp4 |
$ lsof /bin/bash | 查看誰正在使用某個文件,也就是說查找某個文件相關的進程 | |||||||
sh | 528 | toma | txt | REG | 8,22 | 903464 | 1315133 | /usr/bin/bash |
$ lsof /usr/bin/fish | ||||||||
fish | 738 | toma | txt | REG | 8,22 | 1587184 | 1365002 | /usr/bin/fish |
$ lsof /usr/lib/libreoffice/program/soffice.bin | ||||||||
soffice.b | 22284 | toma | txt | REG | 8,22 | 14112 | 1456879 | /usr/lib/libreoffice/program/soffice.bin |
$ lsof -c soffice.b |grep office.bin | ||||||||
soffice.b | 22284 | toma | txt | REG | 8,22 | 14112 | 1456879 | /usr/lib/libreoffice/program/soffice.bin |
$ lsof -c soffice |grep office.bin | ||||||||
soffice.b | 22284 | toma | txt | REG | 8,22 | 14112 | 1456879 | /usr/lib/libreoffice/program/soffice.bin |
$ lsof /dev/sda10 | 列出 /dev/sda10 設備上打開的全部文件 | |||||||
vlc | 29350 | toma | 21r | REG | 8,10 | 8500479 | 9728 | /run/media/toma/TjOe/TDDownload/FROZEN2.mp4 |
4.1 manpage-Examples
For a more extensive set of examples, documented more fully, see the 00QUICKSTART file of the lsof distribution.有關更全面的示例,請參閱lsof發行版的00QUICKSTART文件。
To list all open Internet, x.25 (HP-UX), and UNIX domain files, use:
要列出全部打開的Internet,x.25(HP-UX)和UNIX域文件,請使用:
$ lsof -i -U
To list all open IPv4 network files in use by the process whose PID is 1234, use:
要列出PID爲1234的進程正在使用的全部打開的IPv4網絡文件,請使用:
$ lsof -i 4 -a -p 1234
Presuming the UNIX dialect supports IPv6, to list only open IPv6 network files, use:
假設UNIX方言支持IPv6,要僅列出打開的IPv6網絡文件,請使用:
$ lsof -i 6
To list all files using any protocol on ports 513, 514, or 515 of host wonderland.cc.purdue.edu, use:
要在主機wonderland.cc.purdue.edu的端口513,514或515上使用任何協議列出全部文件,請使用:
$ lsof -i @wonderland.cc.purdue.edu:513-515
To list all files using any protocol on any port of mace.cc.purdue.edu (cc.purdue.edu is the default domain), use:
要在mace.cc.purdue.edu的任何端口上使用任何協議列出全部文件(cc.purdue.edu是默認域),請使用:
$ lsof -i @mace
To list all open files for login name ''abe'', or user ID 1234, or process 456, or process 123, or process 789, use:
要列出登陸名「abe」,或用戶ID 1234,或流程456,流程123或流程789的全部打開文件,請使用:
$ lsof -p 456,123,789 -u 1234,abe
To send a SIGHUP to the processes that have /u/abe/bar open, use:
要將SIGHUP發送到打開/ u / abe / bar的進程,請使用:
$ kill -HUP 'lsof -t /u/abe/bar'
To find any open file, including an open UNIX domain socket file, with the name /dev/log, use:
要查找任何打開的文件,包括名爲/ dev / log的打開的UNIX域套接字文件,請使用:
$ lsof /dev/log
To find processes with open files on the NFS file system named /nfs/mount/point whose server is inaccessible, and presuming your mount table supplies the device number for /nfs/mount/point, use:
要在名爲/ nfs / mount / point且服務器不可訪問的NFS文件系統上查找具備打開文件的進程,並假設您的掛載表提供/ nfs / mount / point的設備編號,請使用:
$ lsof -b /nfs/mount/point
To do the preceding search with warning messages suppressed, use:
要在禁用警告消息的狀況下執行上述搜索,請使用:
$ lsof -bw /nfs/mount/point
To ignore the device cache file, use:
要忽略設備緩存文件,請使用:
$ lsof -Di
To obtain PID and command name field output for each process, file descriptor, file device number, and file inode number for each file of each process, use:
要爲每一個進程獲取PID和命令名稱字段輸出,文件描述符,文件設備編號和每一個進程的每一個文件的文件inode編號,請使用:
$ lsof -FpcfDi
To list the files at descriptors 1 and 3 of every process running the lsof command for login ID ''abe'' every 10 seconds, use:
要列出每10秒執行登陸ID「abe」的lsof命令的每一個進程的描述符1和3的文件,請使用:
$ lsof -c lsof -a -d 1 -d 3 -u abe -r10
To list the current working directory of processes running a command that is exactly four characters long and has an 'o' or 'O' in character three, use this regular expression form of the -c c option:
要列出運行命令的進程的當前工做目錄,該命令長度正好爲四個字符且在字符3中具備「o」或「O」,請使用-c c選項的此正則表達式形式:
$ lsof -c /^..o.$/i -a -d cwd
To find an IP version 4 socket file by its associated numeric dot-form address, use:
要經過其關聯的數字點形式地址查找IP版本4套接字文件,請使用:
$ lsof -i@128.210.15.17
To find an IP version 6 socket file (when the UNIX dialect supports IPv6) by its associated numeric colon-form address, use:
要經過關聯的數字冒號形式地址查找IP版本6套接字文件(當UNIX方言支持IPv6時),請使用:
$ lsof -i@[0:1:2:3:4:5:6:7]
To find an IP version 6 socket file (when the UNIX dialect supports IPv6) by an associated numeric colon-form address that has a run of zeroes in it - e.g., the loop-back address - use:
要經過關聯的數字冒號形式地址查找IP版本6套接字文件(當UNIX方言支持IPv6時),其中包含一連串的零 - 例如,回送地址 - 使用:
$ lsof -i@[::1]
To obtain a repeat mode marker line that contains the current time, use:
要得到包含當前時間的重複模式標記線,請使用:
$ lsof -rm====%T====
To add spaces to the previous marker line, use:
要向前一個標記行添加空格,請使用:
$ lsof -r "m==== %T ===="
5. 特別用法
5.1 恢復刪除的文件
對於許多應用程序,尤爲是日誌文件和數據庫,這種恢復刪除文件的方法很是有用。$ lsof |grep .mp4 播放視頻文件中... FROZEN2.mp4
vlc 30573 toma 20r REG 8,10 8500479 9728 /run/media/toma/TjOe/TDDownload/FROZEN2.mp4
$ lsof |grep .mp4 放入回收站後 顯示以下: 從回收站恢復便可
vlc 30573 toma 20r REG 8,10 8500479 9728 /run/media/toma/TjOe/.Trash-1001/files/FROZEN2.mp4
$ lsof |grep .mp4 直接刪除(shift + delete),顯示以下,後面多了(deleted)標記
vlc 30573 toma 20r REG 8,10 8500479 9728 /run/media/toma/TjOe/TDDownload/FROZEN2.mp4 (deleted)
$ ls -l /proc/30573/fd/20 列出pid信息,/proc/30573/fd/20 連接到已刪除文件。
lr-x------ 1 /proc/30573/fd/20 -> '/run/media/toma/TjOe/TDDownload/FROZEN2.mp4 (deleted)'
$ cat /proc/30573/fd/20 > /home/toma/te.mp4 使用查看命令打開並 重定向到新文件便可
$ ls -l /home/toma/te.mp4 列出恢復的新文件
-rwxrwxrwx 1 toma 8500479 te.mp4
5.2 殺死某個特定用戶的全部活動
# kill -9 `lsof -t -u named`對於服務器或多用戶共用的環境比較有用。