watch linux

How to Watch Changes in Linux

Spread the love

In this short article we will show you how to watch for changes or execute a command periodically in a simple manner.

Let’s start!

Introduction

There are some scenarios when we’re waiting for an event to take place in our system, it could be the creation of a file, the execution of a process or anything similar you might need at the time. In these cases it’s quite handy to be able to watch for changes periodically, for example, every 5 seconds.

Let’s see how can we achieve that!

Using Loops

One of the ways of doing this is by running a shell script using a loop. For example, if we are waiting for the creation of a file under the current directory, we could run the following:

while true; do ls; sleep 5; echo "\n"; done

In this command we are execution “ls” command in the current directory and then wait (or sleep) for 5 seconds. We are also adding a break line just to be able to differentiate every execution easily.

Another option if you don’t want to wait forever, is to keep a counter in your loop. It’ll probably be cleaner if you create a bash script for it. For instance:

#!/bin/bash
x=0
while [ $x -le 120 ]
do
  ls
  echo -e "\n"
  x=$(( $x + 1 ))
  sleep 1
done

In the example above, we are checking the content of the current directory for a maximum of 2 minutes (120 seconds).

Also, if you prefer to specify how many seconds you’d like to wait for, you could write a script like the following:

#!/bin/bash
x=0

if [ -z "$1" ] then
    limit=30;
else
    limit=$1
fi

echo "Watching for $limit seconds"
while [ $x -le $limit ]
do
  ls
  echo -e "\n"
  x=$(( $x + 1 ))
  sleep 1
done

You can see how we are setting a default value of 30 seconds if no argument is specified, otherwise we will wait up to any given seconds specified as argument.

Is there any other way to do this? It’d be great if we don’t have to code the script ourselves, right? Of course there is, let’s see how!

Using Watch Command

The best way to watch for changes periodically is by using “watch” command.

Apart from being easier to write, it has the benefit of refreshing the content in place, in those cases where we don’t need to keep a history in our screen.

For example, the equivalent to the previous loop would be something as simple as this:

watch -n 5 ls

If you run this command, you will be shown a screen like the one shown below, where the data gets refreshed every 5 seconds as specified:

watch for changes in linux

Let’s create another tab using tmux (CTRL/CMD+D) to be able to see this command in action. We’re using colima to run a Linux system on our Mac, so we would have this to be able to see changes on one side:

watch for changes in linux

Let’s see what happens if we create a new file in that directory.

watch for changes in linux

You can see how our file is now shown on the left-hand side. If we delete that file now, we can see how it gets updated after 5 seconds too:

watch for changes in linux

You can always check the command’s help to check the options and how to use them:

$ watch --help

Usage: watch [-n SEC] [-t] PROG ARGS

Run PROG periodically

	-n SEC	Period (default 2)
	-t	Don't print header

Very simple but very useful at the same time, right?

If you are interested in knowing more about Linux or UNIX systems, we recommend the following books:

Conclusion

In this article we have seen two different ways to watch for changes in Linux, having “watch” as a preferred way to do it, unless that there’s a good reason not to use it.

That’s all from us in this short article which we hope you found useful!

Thanks for reading us! See you soon!

Leave a Reply