Install Watch Command on Mac OS X
If there was one command I would really complain about not being on Mac OS X, it would be “watch”. Watch is one of those great pieces of software that is tiny and completely out of the way, but when needed it will be a life saver. We’re going to show you three different ways to install the watch command; through a precompiled binary, with HomeBrew, and with MacPorts. Plus, we’ll show you a little about how to use watch and why it’s useful.
What is ‘watch’ and how does it work?
What is the watch command? For the unfamiliar, it’s simple to explain watch; Watch will run a command repeatedly and then display the output in an “ncurses” friendly manner. Another way to explain this might be to say, you can turn any command line program output into a “real time” display. A good example is using watch to monitor disk usage.
Now of course it’s hard to demonstrate the command running in a repetitive manner using a still image, but this might be something you would want to run when monitoring the amount of space left on your hard drive while transferring a large file(s). If you examine the screen shot closely, in the upper left hand corner you will notice that the command is being run every 5.0s, or 5 seconds. The command being run, is “df -kh”. The output tells us the disk size, used space, available space and percentage used (capacity) in a human readable format (thats what the “h” stands for in df -kh). As we delete a large portion of files, or copy new files to our computer we will see these values change. If you wanted to see smaller increments you would remove the “h” and just run “df -k”.
The basic usage for watch is: watch -n number_of_seconds “command”
So, watch is pretty great. Let’s get watch working on your Mac.
There are a few different ways to install Watch command in Mac OS X, including with Homebrew, MacPorts, or with a precompiled binary. If you really want to, you can even compile watch yourself. We’ll cover the easiest three methods of getting watch on the Mac.
Installing Watch in Mac OS X with Homebrew or MacPorts
If you’re a user of HomeBrew or MacPorts, you can also install watch through those tools:
For Homebrew, install the watch command with:
brew install watch
For MacPorts, you can install watch with:
sudo port install watch
Both of these will also install watch onto the Mac, use whichever approach you are comfortable with. With either Homebrew or Macports, you can simply type ‘watch’ after the install has been completed to run the command.
How to Install & Run Watch in OS X with a precompiled binary
As mentioned, we have three choices; installing watch as a precompiled binary, installing watch with Homebrew, or installing Watch with MacPorts. The precompiled binary will work if you do not have homebrew or ports installed in OS X.
Download the precompiled “watch” command from Terminal.app
We are using “curl”, a command line “browser”, this will download watch to your Mac, but if you want you can use MacPorts or Homebrew to install watch as well, which are shown below:
curl -O http://ktwit.net/code/watch-0.2-macosx/watch
Make “watch” executable
By doing this we tell Mac OS that this is a program that can run
chmod +x watch
Test the program
Let’s make sure everything is in working order.
Optional: By following this next step we are placing watch into a system location that will allow you to run it from any location in the Terminal (You will be prompted for your password)
sudo mv watch /usr/local/bin/
Congrats, you have added the watch command to your Mac OS X System.
Great tip. Another option is to use this little bash script which I found on a guys website, can’t remember the name of the author but it has been in my .profile for a long time:
put this into a file ‘diywatch’:
# usage: diywatch [command] [sleep duration]
while :; do
bash -c "$1"
Then chmod +x diywatch
Works basically the same as watch, great little tip. If you know the original creator, tell htem thanks!
On OS X 10.8.2, there is no /usr/local/bin directory. Putting it in /usr/bin instead works.
Thanks for this!
Great little tech note, thanks!
or you compile it yourself…
make watch PKG_LDFLAGS=-Wl
why the hassle? the precompiled binary is not working on legacy power pc systems…
Very helpful, thanks. However, the install command does not appear to work for me. The directory is not recognized.
Use single or double quotes to surround your watch command.
watch “ls -hal | grep something”
Am I mistaken or is there no possibility to use the pipe with this ‘watch’? I tried “ls -hal | grep something” and it returned “h | grep command not found”.
[…] useful when combined with the Unix watch command (if you’re on OS X, you may have to install it manually), which reruns the command periodically so you can see how things are changing in […]
ehh, download a file from somewhere you haven’t even mentioned before in the post and make it executable, then run it. It’s like the world’s most straight forward virus. I know, it probably isn’t, but one wouldn’t know either way.
You’d probably be safer with macports or fink.
Fink provides `watch’ and a bunch of other useful command line utilities. See http://en.wikipedia.org/wiki/Fink and http://www.finkproject.org/
This is a life-saver!!! I’ve been running mac and Linux all this time and even I didn’t know about ‘watch’… I’ve been looking for a real-time display app… something like htop/top but for other app on the shell.
sudo port install watch
This is nice one.
This would be perfect for using with GeekTool!
Thanks, very useful.