用途

列出进程已打开的文件,文件可以是常规文件,特殊文件,目录,socket,设备,共享库等。如果不带参数,lsof显示所有进程打开的所有文件。

用法

lsof  [  -?abChlnNOPRtUvVX  ]  [  -A  A  ] [ -c c ] [ +c c ] [ +|-d d ] [ +|-D D ] [ +|-e s ] [ +|-f
[cfgGn] ] [ -F [f] ] [ -g [s] ] [ -i [i] ] [ -k k ] [ +|-L [l] ] [ +|-m m ] [ +|-M ] [ -o [o] ] [ -p
s ] [ +|-r [t[m<fmt>]] ] [ -s [p:s] ] [ -S [t] ] [ -T [t] ] [ -u s ] [ +|-w ] [ -x [fl] ] [ -z [z] ]
[ -Z [Z] ] [ -- ] [names]

常用选项

-?, -h

显示帮助信息

-a

过滤选项是与的关系,也就是说,所有条件都需要满足

-c c

只输出结果以指定字符串开头的记录,如果字符串签名是^开头表示取反,如果是这样的格式:/xx/修饰符,表示是正则模式

+c w

设置命令名称显示的最大长度,系统规定的最大大小是15,超过了会截取掉,不够的部分使用空格填充

+d s

输出打开文件完整目录路径是s的记录

-d s

根据文件描述符过滤记录,多个使用英文逗号分隔;^开头表示取反;支持数字范围;

+D D

输出包含目录D的记录,部分包含也会输出

-i [i]

This option selects the listing of files any of whose Internet address matches the address specified in i. If no address is specified, this option selects the listing of all Internet and x.25 (HP-UX) network files.

#格式
[46][protocol][@hostname|hostaddr][:service|port] 46 specifies the IP version, IPv4 or IPv6
that applies to the following address.
’6’ may be be specified only if the UNIX
dialect supports IPv6. If neither ’4’ nor
’6’ is specified, the following address
applies to all IP versions.
protocol is a protocol name - TCP, UDP
hostname is an Internet host name. Unless a
specific IP version is specified, open
network files associated with host names
of all versions will be selected.
hostaddr is a numeric Internet IPv4 address in
dot form; or an IPv6 numeric address in
colon form, enclosed in brackets, if the
UNIX dialect supports IPv6. When an IP
version is selected, only its numeric
addresses may be specified.
service is an /etc/services name - e.g., smtp -
or a list of them.
port is a port number, or a list of them.

-l

用户id不转换为用户名(加了这个参数,不知道为啥都是0 _)

-n

This option inhibits the conversion of network numbers to host names for network files. Inhibiting conversion may make lsof run faster. It is also useful when host name lookup is not working properly.

-p s

根据进程id过滤,多个使用英文逗号隔开

--

表示选项已结束,后面的是参数

参数

names

打开的文件名(完整路径)

实践

1 显示所有打开的文件

[root@vm ~]# lsof

2 list all open Internet, x.25 (HP-UX), and UNIX domain files

[root@vm ~]# lsof -i -U

3 list all open IPv4 network files in use by the process whose PID is 1234

[root@vm ~]# lsof -i 4 -a -p 1234

4 list only open IPv6 network files

[root@vm ~]# lsof -i 6

5 list all files using any protocol on ports 513, 514, or 515 of host wonderland.cc.purdue.edu

[root@vm ~]# lsof -i @wonderland.cc.purdue.edu:513-515

6 list all files using any protocol on any port of mace.cc.purdue.edu

[root@vm ~]# lsof -i @mace

7 list all open files for login name ‘‘abe’’, or user ID 1234, or process 456, or process 123, or process 789

[root@vm ~]# lsof -p 456,123,789 -u 1234,abe

8 list all open files on device /dev/hd4

[root@vm ~]# lsof /dev/hd4

9 find the process that has /u/abe/foo open

[root@vm ~]# lsof /u/abe/foo

10 send a SIGHUP to the processes that have /u/abe/bar open

[root@vm ~]# kill -HUP 'lsof -t /u/abe/bar'

参考资料

【1】man lsof

N天学习一个linux命令之lsof的更多相关文章

  1. N天学习一个Linux命令之帮助命令:man

    前言 工作中每天都在使用常用的命令和非常用的命令,忘记了用法或者参数,都会bing一下,然后如此循环.一直没有真正的系统的深入的去了解命令的用法,我决定打破它.以前看到有人,每天学习一个linux命令 ...

  2. N天学习一个Linux命令之free

    用途 查看系统内存(物理/虚拟/缓存/共享)使用情况 用法 free [-b | -k | -m | -g | -h] [-o] [-s delay ] [-c count ] [-a] [-t] [ ...

  3. N天学习一个linux命令之ping

    用途 检测主机是否可到达,也就是说,目标主机是否可以联网,还可以用于检测网速.通过发送ICMP ECHO_REQUEST数据包检测. 用法 ping [options] destination 常用选 ...

  4. N天学习一个linux命令之kill

    用途 用于终止进程 用法 kill [-s signal|-p] [--] pid... kill -l [signal] 说明 1.默认发送信号15(请求终止进程,程序可以捕获,操作系统会杀死没有对 ...

  5. N天学习一个linux命令之du

    用途 统计文件或者目录占用硬盘空间大小 用法 du [OPTION] [FILE]du [OPTION] --files0-from=F 常用参数 -a, --all统计所有文件,不仅仅是目录 -b, ...

  6. N天学习一个linux命令之scp

    用途 通过ssh通道,不同主机之间复制文件 用法 scp [options] [user@host:]file1 [user2@host2:]file2 常用参数 -1使用 ssh 1协议 -2使用s ...

  7. 每天学习一个Linux命令-目录

    在工作中总会零零散散使用到各种Linux命令,从今天开始详细的学习一下linux常用命令,坚持每天一个命令,学习的主要参考资料为: 1.竹子-博客(https://www.cnblogs.com/pe ...

  8. 每天一个linux命令(38)--lsof命令

    lsof (list open files )是一个列出当前系统打开文件的工具,在Linux 环境下,任何事情都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.所以如传输控 ...

  9. N天学习一个linux命令之umask

    前言 umask不是linux命令,而是shell内置的指令,俗称用户权限掩码,用于对用户创建的文件和目录设置默认权限.默认的权限掩码是0022,也就是说新创建的文件权限是0644,新创建的目录权限是 ...

随机推荐

  1. [App Store Connect帮助]三、管理 App 和版本(6.1)转让 App:App 转让概述

    当您将某个 App 出售给其他开发者,或想要将其移至其他 App Store Connect 帐户或组织时,您需要转让该 App. 您无需将 App 从 App Store 下架,即可将其所有权转让给 ...

  2. [Swift通天遁地]四、网络和线程-(7)检测服务器接口的访问状态:验证请求结果和可访问性

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. akka设计模式系列-Aggregate模式

    所谓的Aggregate模式,其实就是聚合模式,跟masterWorker模式有点类似,但其出发点不同.masterWorker模式是指master向worker发送命令,worker完成某种业务逻辑 ...

  4. ACM_填格子

    填格子 Time Limit: 2000/1000ms (Java/Others) Problem Description: 在一个n*n格子里边已经填了部分大写字母,现在给你个任务:把剩下的格子也填 ...

  5. Android 使用 Application 简单介绍

    Application 配置全局Context 第一步.写一个全局的单例模式的MyApplication继承自Application 覆盖onCreate ,在这个方法里面实例化Application ...

  6. Elasticsearch之CURL命令的PUT和POST对比

    PUT是幂等方法,而POST并不是. PUT用于更新操作,POST用于新增操作比较合适. PUT,DELETE操作是幂等的,所谓幂等就是指不管进行多少次操作,结果都一样. 比如,我用PUT修改一篇文章 ...

  7. SQL Server 2008R2 Set IDENTITY_INSERT 表名 ON/OFF不能与insert into select 的语句一起执行?

    大家都知数据库表中的列可以自增长,但是有时候我们需要插入数据的时候会指定这一列的数据. 这时候我们可以很简单的利用sql语句来执行新增一条的数据,如下: set IDENTITY_INSER 表名 o ...

  8. 单例模式在多线程环境下的lazy模式为什么要加两个if(instance==null)

    刚才在看阿寻的博客”C#设计模式学习笔记-单例模式“时,发现了评论里有几个人在问单例模式在多线程环境下为什么lazy模式要加两个if进行判断,评论中的一个哥们剑过不留痕,给他们写了一个demo来告诉他 ...

  9. PHP 之文件锁解决并发问题

    一.参数说明 $handle: 文件资源 $operation: 锁的类型 LOCK_SH: 共享锁 LOCK_EX: 排他锁 LOCK_UN: 释放锁 $wouldblock: 设置为true的时候 ...

  10. JavaFX桌面应用开发-Button(按钮)与事件

    1:Button样式的操作原始代码: package application; import javafx.application.Application;import javafx.scene.Gr ...