92

I have to do watch two commands in the same terminal windows. I mean something like

watch du -h filename.txt && df -h

But its showing only one output.

So what I am thinking is may be its not possible to use watch to run multiple commands on the same window.

If there is any way , Please let me know.

Thank you.

Raja G
  • 105,327
  • 107
  • 262
  • 331

4 Answers4

142

You can quote the commands:

watch "du -h filename.txt && df -h"

And they'll be executed together.

Oli
  • 299,380
35

If you want to make sure both commands execute, one of the ways is to separate them with ; instead of &&.

watch 'du -h filename.txt; df -h'

&& allows the execution of second command (second operand, to the right of &&) only if the first command executed successfully (exit status 0). If this is intended behaviour, go with &&.

8

For completeness sake...

 watch 'du -h filename.txt || true && df -h'

The '|| true' part causes the first command to evaluate as true even if it fails for some reason. This will allow the next command after the && to execute no matter the output of first. This is most likely unnecessary for the scenario, just showing it to be possible.

Geofferey
  • 181
2

For multiple commands to run simultaneously, use single & operator between the commands. Like:

dothis & dothat

To clear any confusion, here's how different operators work:

c1 & c2  # Run both commands parallelly
c1 ; c2  # Run both commands one by one
c1 && c2 # Run c2 only if c1 exits successfully
c1 || c2 # Run c2 only if c1 fails
Vibhum Bhardwaj
  • 83
  • 1
  • 1
  • 8