惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
aimingoo的专栏
aimingoo的专栏
F
Fortinet All Blogs
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
MongoDB | Blog
MongoDB | Blog
月光博客
月光博客
The Cloudflare Blog
量子位
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
MyScale Blog
MyScale Blog
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
G
Google Developers Blog
D
DataBreaches.Net
V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
U
Unit 42
博客园 - 聂微东
有赞技术团队
有赞技术团队
A
About on SuperTechFans

二丫讲梵

学习周刊-总第258期-2026年第15周 学习周刊-总第257期-2026年第14周 学习周刊-总第256期-2026年第13周 学习周刊-总第255期-2026年第12周 学习周刊-总第254期-2026年第11周 学习周刊-总第253期-2026年第10周 临时插播一条羊毛,免费领取450元大模型API代金券 学习周刊-总第252期-2026年第09周 学习周刊-总第251期-2026年第08周 学习周刊-总第250期-2026年第07周 学习周刊-总第249期-2026年第06周 诚邀评论,聊聊你所知欲知的我 学习周刊-总第248期-2026年第05周 我的QQ动态之2015年 我的QQ动态之2014年 我的QQ动态之2013年 我的QQ动态之2012年 我的QQ动态-2010-2011年 我的QQ动态-创栏小叙 学习周刊-总第247期-2026年第04周 Nexus社区版权益阉割--一文告诉你有哪些版本可以选择 学习周刊-总第246期-2026年第03周 学习周刊-总第245期-2026年第02周 学习周刊-总第244期-2026年第01周 学习周刊-总第243期-2025年第52周 学习周刊-总第242期-2025年第51周 开源项目ZenOps:带你领略禅意运维 学习周刊-总第241期-2025年第50周 用京东金融,享负债人生 学习周刊-总第240期-2025年第49周
Linux好用命令之watch命令
二丫讲梵 · 2022-10-17 · via 二丫讲梵

watch 命令能够将其他命令的输出定时输出到终端,从而实现监听的能力,在我们要对一些命令状态进行实时监听的场景中,有非常好的应用场景。

# 参数

$watch -h

Usage:
 watch [options] command

Options:
  -b, --beep             如果命令具有非零退出,则发出蜂鸣音
  -c, --color            解释ANSI颜色和样式序列
  -d, --differences[=<permanent>]
                         高亮显示两次更新之间的变化
  -e, --errexit          如果命令有非零退出,则退出
  -g, --chgexit          当命令的输出发生变化时退出
  -n, --interval <secs>  两次更新之间的等待秒数
  -p, --precise          尝试以精确的时间间隔运行命令
  -t, --no-title         关闭watch命令在顶部的时间间隔,命令,当前时间的输出
  -x, --exec             将命令传递给exec,而不是 "sh -c"

 -h, --help     display this help and exit
 -v, --version  output version information and exit

For more details see watch(1).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# 常用例子

比较常用的参数有 -n -d

监听当前目录下文件变化:

监听系统中 TCP 连接状态的变化:

watch -n1 -d "netstat -an  | awk '/tcp/ {print \$6}'| sort | uniq -c"

1

  • watch 后边如果带有管道符,则用双引号将后边的命令包裹成一个整体。
  • 另外,当 awk 的 print 被双引号包裹之后,需要在$符号前边加个转义符。