• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

watch command examples to run a command repeatedly or monitor dynamically changeable files (like /proc/*)

by admin

Watch command is a really neat tool and comes in handy in many situations. The watch command can be used to monitor any file or script periodically. It runs every 2 seconds by default and it will run until interrupted.

# watch -h
Usage: watch [-dhntv] [--differences[=cumulative]] [--help] [--interval=[n]] [--no-title] [--version] [command]
  -d, --differences[=cumulative]	highlight changes between updates
		(cumulative means highlighting is cumulative)
  -h, --help				print a summary of the options
  -n, --interval=[seconds]		seconds to wait between updates
  -v, --version				print the version number
  -t, --no-title			turns off showing the header

The basic syntax of watch command is :

# watch [-n seconds] [-d] [command]

Here,

-d flag will highlight the differences between successive updates.
-n flag is to specify the interval. The default value is 2 seconds.

Here’s a sample output:

# watch -n 10 -d ls -lt

Every 10.0s: ls -lt                                             Tue Feb 14 12:27:43 2017

total 0
-rw-r--r-- 1 root root 0 Feb 14 12:27 new_file_just_created
-rw-r--r-- 1 root root 0 Feb 14 10:46 file1
-rw-r--r-- 1 root root 0 Feb 14 10:46 file2
-rw-r--r-- 1 root root 0 Feb 14 10:46 file3

Here,

Every 10.0s : is the time interval to run the watch command. i.e. 10 seconds.
ls -lt : is the command to be executed every 10 seconds.
Tue Feb 14 12:27:43 2017 : is the current date and time.

Example 1 : Monitoring a dynamically changing file like /proc/meminfo

There is a way to monitor any file on the system with the command watch.

The command:

# watch -n 10 -d  cat /proc/meminfo

would produce an output of the meminfo status every 10 seconds on the screen and will highlight if any changes.

watch command to monitor dynamically changeable files

Example 2 : look for the change in the content of a directory

Another excellent use of the watch command is to keep an eye on the contents of the directory and see if any new file is getting added or removed.

# watch -d ls -lt

-lt switch in the ls command displays the latest modified file at the top.

watch command to chech changes in a directory

Example 3 : Removing the title/header from the output.

In case you do not want to print the header in the output of watch command, you can use the –no-title or -t option.

# watch -t -d ls -lt

total 0
-rw-r--r-- 1 root root 0 Feb 14 10:47 new_file_just_created
-rw-r--r-- 1 root root 0 Feb 14 10:46 file1
-rw-r--r-- 1 root root 0 Feb 14 10:46 file2
-rw-r--r-- 1 root root 0 Feb 14 10:46 file3

Example 4 : Highlighting cumulative difference

In case you want to highlight the cumulative difference in the output, you can use the -d=cumulative switch in the command. For example :
The output after adding a new file – new_file1 :

highlight cumulative difference in watch command

The output after adding another new file – new_file2 :

highlight cumulative difference in watch command - next file

Filed Under: Linux

Some more articles you might also be interested in …

  1. mountpoint: command not found
  2. What is umask in UNIX/Linux
  3. How to Disable IPv6 on Ubuntu 18.04 Bionic Beaver Linux
  4. Understanding /etc/xinetd.conf file in Linux
  5. lxi: command not found
  6. XFS error: Unable to mount filesystem With Noacl Permission in CentOS/RHEL 7
  7. aa-disable: command not found
  8. ipcmk Command Examples in Linux
  9. Linux Interview Questions : Open Files / Open File Descriptors
  10. numlockx Command Examples in Linux

You May Also Like

Primary Sidebar

Recent Posts

  • qsub Command Examples in Linux
  • qsub: command not found
  • qrcp Command Examples in Linux
  • qmrestore Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright