watch:有时候我们会需要观察一个命令输出结果的变化,比如你正在拷贝文件,你可能要监视磁盘大小的变化,watch命令可以帮你做到这点。
watch功能描述
$watch --help
Usage:
watch [options] command
Options:
-b, --beep beep if command has a non-zero exit
-c, --color interpret ANSI color and style sequences
-d, --differences[=<permanent>]
highlight changes between updates
-e, --errexit exit if command has a non-zero exit
-g, --chgexit exit when output from command changes
-n, --interval <secs> seconds to wait between updates
-p, --precise attempt run command in precise intervals
-t, --no-title turn off header
-x, --exec pass command to exec instead of "sh -c"
-h, --help display this help and exit
-v, --version output version information and exit
以下简单列举几种用法
执行磁盘压测命令后观察/root目录下空间的变化
stress --hdd 1 --hdd-bytes 1024G & #在/root目录执行该命令,当前目录的空间会不断被占用
watch 'df -h /root' #执行该命令进行观察,可以看到空间大小及百分比的变化
可以看到输出结果默认每2s改变一次,下图为其中一次的输出结果:
-n参数
** -n 加一个输出更新时间,默认是2s**
-n, --interval seconds to wait between updates
watch -n 5 'ls /root/pert' #执行该命令进行观察,如果有新的内容输出,每5s可以看到一次更新
-n 5表示设置每5s更新一次输出结果,从输出的页面也能看到这个设置(红色框中显示的内容),也可以实际查看下输出变化是否是设置的5s
-g参数
-g 当命令的输出更改时退出
在同一台设备上打开两个终端,观察改参数是否能够正常工作
watch -g -n 5 'ls' #在终端1 /root目录执行该命令
输出为:
touch watch #在终端2 /root目录执行该命令
观察终端1中输出的变化,发现watch -g -n 5 ‘ls’ 命令退出了,-g参数起作用了
-d参数
-d 高亮显示更新的部分
高亮如下图箭头所指部分(注意:高亮并不会持续很长时间,注意观察,以免错过)
注:如果新加入的文件名称不是放在最后一个,有时会显示过多的高亮内容,感觉是一个bug