Linux-lsof

Linux-lsof

 

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 文件描述符

FDFD是文件的文件描述符编号或: wc -l
cwdcurrent working directory;当前工作目录381
txtprogram text (code and data);程序文件或共享库381
rtdroot directory;root目录381
memmemory-mapped file;内存映射文件;36728
mmapmemory-mapped device;内存映射设备; 
Lnnlibrary references (AIX);库引用(AIX); 
jldjail directory (FreeBSD);jail目录(FreeBSD); 
ltxshared library text (code and data);共享库文本(代码和数据); 
Mxxhex memory-mapped type number xx.十六进制内存映射类型号xx。 
m86DOS Merge mapped file;DOS合并映射文件; 
errinformation error (see NAME column);错误的FD信息错误; 
pdparent directory;父目录; 
trkernel trace file (OpenBSD);内核跟踪文件(OpenBSD); 
v86VP/ix mapped file;VP/ix映射文件; 
DEL  3743
    
ModeFD后跟其中一个字符,描述文件打开的模式:  
uu for read and write access;文件处于读取/写入模式128:5678
rr for read access;文件处于只读模式78:2924
ww for write access;文件处于写入模式92:2633
' ‘space if mode unknown and no lock未知模式,且未锁定 
-’-’ if mode unknown and lock未知模式,并锁定 
    
Lock模式字符后跟一个锁定字符,描述应用于文件的锁定类型:  
NN for a Solaris NFS lock of unknown type;未知类型的Solaris NFS锁; 
rr for read lock on part of the file;r用于部分文件的读锁定; 
RR for a read lock on the entire file;R表示整个文件的读锁定; 
ww for a write lock on part of the file;w对文件的一部分进行写锁定; 
WW for a write lock on the entire file;W表示整个文件的写锁定; 
uu for a read and write lock of any length;u表示任何长度的读写锁; 
UU for a lock of unknown type;U表示未知类型的锁; 
xx for an SCO OpenServer Xenix lock on part of the file;部分文件的SCO OpenServer Xenix锁; 
XX for an SCO OpenServer Xenix lock on the entire file;整个文件的SCO OpenServer Xenix锁; 
' ‘space if there is no lock.空格, 没有锁。 

2.2 Type 文件类型

TYPEis the type of the node associated with the fileTYPE是与文件关联的节点的类型wc-l
BLKfor a block special file用于块特殊文件1
CHRfor a character special file用于字符特殊文件780
DIRfor a directory用于目录770
REGfor a regular file用于常规文件46350
FIFOfor a FIFO special file用于FIFO特殊文件903
IPv4for an IPv4 socket用于IPv4套接字7
IPv6for an open IPv6 network file - even if its address is IPv4, mapped in an IPv6 address用于开放的IPv6网络文件 - 即使其地址是IPv4,也映射在IPv6地址中22
sockfor a socket of unknown domain用于未知域的套接字217
unixfor a UNIX domain socketunix套接字的2925
a_inode  1384
netlink  134
unknown  72
DELfor a Linux map file that has been deleted已删除的Linux映射文件的 
LINKfor a symbolic link file用于符号链接文件 
MPBfor a multiplexed block file用于多路复用块文件 
MPCfor a multiplexed character file用于多路复用字符文件 
ax25for a Linux AX.25 socket用于Linux AX.25套接字 
inetfor an Internet domain socket用于Internet域套接字 
llafor a HP-UX link level access file用于HP-UX链路级访问文件 
rtefor an AF_ROUTE socket用于AF_ROUTE套接字 
x.25for an HP-UX x.25 socket用于HP-UX x.25套接字 
DOORfor a VDOOR fileVDOOR文件的 
KQUEUEfor a BSD style kernel event queue file用于BSD样式的内核事件队列文件 
NOFDfor 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列中,后跟一条错误消息 
PASfor a /proc/as file用于/proc/as文件 
PAXVfor a /proc/auxv file用于/proc/auxv文件 
PCREfor a /proc/cred file用于/proc/cred文件 
PCTLfor a /proc control file用于/proc控制文件 
PCURfor the current /proc process用于当前/proc过程 
PCWDfor a /proc current working directory用于/proc当前工作目录 
PDIRfor a /proc directory作为/proc目录 
PETYfor a /proc executable type (etype)用于/proc可执行类型(etype) 
PFDfor a /proc file descriptor用于/proc文件描述符 
PFDRfor a /proc file descriptor directory用于/proc文件描述符目录 
PFILfor an executable /proc file表示可执行文件/proc文件 
PFPRfor a /proc FP register set用于/proc FP寄存器组 
PGDfor a /proc/pagedata file用于/proc/pagedata文件 
PGIDfor a /proc group notifier file用于/proc组通知程序文件 
PIPEfor pipes管道 
PLCfor a /proc/lwpctl file表示/proc/lwpctl文件 
PLDRfor a /proc/lpw directory用于/proc/lpw目录 
PLDTfor a /proc/ldt file用于/proc/ldt文件 
PLPIfor a /proc/lpsinfo file用于/proc/lpsinfo文件的 
PLSTfor a /proc/lstatus file用于/proc/lstatus文件 
PLUfor a /proc/lusage file用于/proc/lusage文件 
PLWGfor a /proc/gwindows file用于/proc/gwindows文件 
PLWIfor a /proc/lwpsinfo file用于/proc/lwpsinfo文件 
PLWSfor a /proc/lwpstatus file用于/proc/lwpstatus文件 
PLWUfor a /proc/lwpusage file用于/proc/lwpusage文件 
PLWXfor a /proc/xregs file用于/proc/xregs文件 
PMAPfor a /proc map file (map)用于/proc映射文件(map) 
PMEMfor a /proc memory image file用于/proc内存映像文件 
PNTFfor a /proc process notifier file用于/proc进程通知程序文件 
POBJfor a /proc/object file用于/proc/object文件 
PODRfor a /proc/object directory用于/proc/object目录 
POLPfor an old format /proc light weight process file用于旧格式/proc轻量级处理文件 
POPFfor an old format /proc PID file用于旧格式/proc PID文件 
POPGfor an old format /proc page data file用于旧格式/proc页面数据文件 
PORTfor a SYSV named pipeSYSV命名管道的 
PREGfor a /proc register file用于/proc寄存器文件 
PRMPfor a /proc/rmap file用于/proc/rmap文件 
PRTDfor a /proc root directory用于/proc根目录 
PSGAfor a /proc/sigact file用于/proc/sigact文件 
PSINfor a /proc/psinfo file用于/proc/psinfo文件 
PSTAfor a /proc status file用于/proc状态文件 
PSXSEMfor a POSIX semaphore file用于POSIX信号量文件 
PSXSHMfor a POSIX shared memory file用于POSIX共享内存文件 
PTSfor a /dev/pts file用于/dev/pts文件 
PUSGfor a /proc/usage file用于/proc/usage文件 
PWfor a /proc/watch file用于/proc/watch文件 
PXMPfor a /proc/xmap file用于/proc/xmap文件 
SMTfor a shared memory transport file用于共享内存传输文件 
STSOfor a stream socket用于流套接字 
UNNMfor an unnamed type file用于未命名的类型文件 
XNAMfor an OpenServer Xenix special file of unknown type用于未知类型的OpenServer Xenix特殊文件 
XSEMfor an OpenServer Xenix semaphore file用于OpenServer Xenix信号量文件 
XSDfor 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.括号中的默认值; 逗号分隔的集合项目; 短划线范围。
-?|-hlist help列表帮助
-vlist version info列表版本信息
-aAND selections (OR)AND选择(OR)
-c ccmd c ^c /c/[bix]cmd c ^ c / c / [bix]
+c wCOMMAND width (9)命令宽度(9)最大15?
-d sselect by FD set输出列表中排除或包含的文件描述符(FD)列表。
文件描述符在逗号分隔集合s中指定 – 例如,
''cwd,1,3'',''^6,^2''.(集合中不应有空格)
+d sdir s filesdirs文件 注:不含符号链接,除非使用-x或-x l选项可包含
+D Ddir D tree *SLOW?*dir D tree * SLOW?* 递归列出目录下被打开的文件
-u sexclude(^)|select login|UID set sexclude(^)|
-iselect IPv[46] files选择IPv[4,6]tcp,udp等文件
-nno host names#不将IP地址转换为hostname,预设是转换的
-Pno port names#此参数禁止将port number转换为service name,预设为转换
-llist UID numbers列出UID号码, 禁止将UID转换为登录名。
-tterse listing简洁列表, 只输出PID
-slist file size列表文件大小
-olist file offset它会将SIZE/OFF输出列标题更改为OFFSET 偏移量
-Uselect Unix socket选择Unix套接字
+|-e sexempt s *RISKY*免除路径名为s的文件系统受到可能阻塞的内核函数调用。
+e选项免除stat(2),lstat(2)和大多数readlink(2)内核函数调用. 
-e选项仅免除stat(2)和lstat(2)内核函数调用.
可以使用单独的+|-e规范指定多个文件系统,并且每个文件系统可以具有免除或不允许的readlink(2)调用.
+|-wWarnings (+)警告(+)
-p sexclude(^)|select PIDsexclude(^)|选择PID
-Rlist paRent PID列出PPID
-g [s]exclude(^)|select and print process group IDsexclude(^)|选择并打印进程组ID
+|-L [l]list (+) suppress (-) link counts < l (0 = all; default = 0)启用(+)或禁用(-)文件链接计数列表,它们可用-例如,它们不适用于套接字或大多数FIFO和管道.
如果指定+L且没有跟数字, 则将列出所有链接计数。
指定-L(默认值)时,不会列出任何链接计数。
当+L后跟一个数字时,只列出链接数小于该数字的文件。(没有数字可以跟随-L)

4. 示例

COMMANDPIDUSERFDTYPEDEVICESIZE/OFFNODENAME
$ lsof /home/toma/Documents/IO.xls 查找打开该文件的进程
soffice.b20084toma3uWREG8,232426881480016/home/toma/Documents/IO.xls
$ lsof /home/toma/Downloads/mov/GoT.mp4  
Thunar676toma25uREG8,23111213021705712/home/toma/Downloads/mov/GoT.mp4
vlc7975toma22rREG8,23111213021705712/home/toma/Downloads/mov/GoT.mp4
         
$ lsof -c soffice |grep .xls 查找该进程打开的 *.xls 文件
soffice.b20084toma3uWREG8,232426881480016/home/toma/Documents/IO.xls
$ lsof -c vlc |grep .mp4 查找该进程打开的 *.mp4 文件
vlc7975toma23rREG8,23111213021705712/home/toma/Downloads/mov/GoT.mp4
         
$ lsof /bin/bash 查看谁正在使用某个文件,也就是说查找某个文件相关的进程
sh528tomatxtREG8,229034641315133/usr/bin/bash
$ lsof /usr/bin/fish  
fish738tomatxtREG8,2215871841365002/usr/bin/fish
         
$ lsof /usr/lib/libreoffice/program/soffice.bin  
soffice.b22284tomatxtREG8,22141121456879/usr/lib/libreoffice/program/soffice.bin
$ lsof -c soffice.b |grep office.bin  
soffice.b22284tomatxtREG8,22141121456879/usr/lib/libreoffice/program/soffice.bin
$ lsof -c soffice |grep office.bin  
soffice.b22284tomatxtREG8,22141121456879/usr/lib/libreoffice/program/soffice.bin
         
$ lsof /dev/sda10 列出 /dev/sda10 设备上打开的所有文件
vlc29350toma21rREG8,1085004799728/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`
对于服务器或多用户共用的环境比较有用。

5.3 找回被删除文件占用的空间

找到仍被打开,但已被删除的文件
$ lsof | grep deleted

查看后关闭相应程序或杀死进程即可
$ ps -ef | grep pid
$ sudo kill -9 pid
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值