AnswerBun.com

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

Unix & Linux Asked by Tulains Córdova on October 10, 2020

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]?

5 Answers

watch 'command | othertool | yet-another-tool'

Correct answer by DopeGhoti on October 10, 2020

The simplest way is using inbuilt option -x,

watch -n 5 -x tail -4 output.log.  
watch -n 2 -x ls

The first eg. will display the last 4 lines of output.log file every 5 sec., second eg list content every 2 sec.

Answered by amol on October 10, 2020

Use a combination of single quotes (') and double quotes ("). For example:

watch -n 1 "links -dump 127.0.0.1/server-status | grep -e 'S' -Fe 'www.'"

Answered by Dimitar Velev on October 10, 2020

If you would like to list all files in subdirectories too, you can use find command with exec option.

watch will update every 30 seconds and find will search for all *.log files in current dir (subdirs included) and will print filenames and their last 10 line:

watch -n30 'find . -name "*.log" -print -exec tail -n10 {} ; '

Answered by yoyomanYo on October 10, 2020

watch -n 1 "ls -lrt | tail -n20; date"

let's you pipe and run in a row.

Answered by Soft Dot IE on October 10, 2020

Add your own answers!

Related Questions

iptables forwarding not working

1  Asked on January 23, 2021 by eugene-smith

   

No devices for “nmcli connection show “

0  Asked on January 22, 2021 by kylie

   

How to get KWin-like window snapping in Xfce?

1  Asked on January 22, 2021 by bluekingwhite

         

Out of disk space error when trying to write to frame buffer

0  Asked on January 21, 2021 by novicefedora

 

How to build a chroot using docker?

2  Asked on January 20, 2021 by wimateeka

   

Telnet connection to Debian machine refused

1  Asked on January 20, 2021 by hakk

       

Unable to mount using mount.cifs command

1  Asked on January 19, 2021 by sk23

   

Problem: My terminal settings are not saved

0  Asked on January 19, 2021 by sagi-aizenman

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP