Sample Header Ad - 728x90

How to use watch command with a piped chain of commands/programs

236 votes
8 answers
121609 views
I usually use watch Linux utility to watch the output of a command repeatedly every n seconds, like in watch df -h /some_volume/. But I seem not to be able to use watch with a piped series of command like: $ watch ls -ltr|tail -n 1 If I do that, watch is really watching ls -ltr and the output is being passed to tail -n 1 which doesn't output anything. If I try this: $ watch (ls -ltr|tail -n 1) I get $ watch: syntax error near unexpected token `ls' And any of the following fails some reason or another: $ watch <(ls -ltr|tail -n 1) $ watch < <(ls -ltr|tail -n 1) $ watch $(ls -ltr|tail -n 1) $ watch ls -ltr|tail -n 1) And finally if do this: $ watch echo $(ls -ltr|tail -n 1) I see no change in the output at the given interval because the command inside $() is run just once and the resulting output string is always printed ("watched") as a literal. **So, how do I make the watch command work with a piped chain of commands [other that putting them inside a script]?**
Asked by Tulains C&#243;rdova (3376 rep)
Oct 25, 2016, 05:42 PM
Last activity: May 9, 2024, 06:53 PM