79

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
  • 100,643
  • 105
  • 254
  • 328

4 Answers4

121

You can quote the commands:

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

And they'll be executed together.

Oli
  • 289,791
  • 117
  • 680
  • 835
  • Thanks Oli. actually office PC so not daring to do experiments. so posted here for sure solution. Thank you. – Raja G Mar 12 '15 at 13:18
  • Hey you sure they will work ? because when execute I am getting some permission denied error" df: `/usr/raja/hostctrl/new': Permission denied " – Raja G Mar 12 '15 at 13:24
  • That seems to be a separate issue. You'd see that issueeven without it being wrapped in watch. – Oli Mar 12 '15 at 20:56
  • 4
    Be aware this works because those example commands return with _true_. Should the first command return _false_ the second command wouldn't be executed (because of `boolean evaluation shortcut`) – karatedog Mar 20 '16 at 13:31
  • 3
    FTR, my command uses `&&` because the OP uses `&&`. The combination of commands and their linking is incidental to the answer. – Oli Jul 01 '17 at 16:52
  • `watch "du -h filename.txt ; df -h` gets around the problem karatedog mentioned. Further reading: https://stackoverflow.com/questions/13077241/execute-combine-multiple-linux-commands-in-one-line – Nathan majicvr.com Apr 20 '18 at 16:44
31

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 &&.

pranavcode
  • 411
  • 4
  • 4
  • 3
    This semicolon form seems best to me as an answer to the original question. If you want to "run two commands", use a semicolon! If you want to "run one command, then maybe another" use && .... – JeremyDouglass Jul 28 '18 at 22:11
7

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
  • 171
  • 1
  • 4
0

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
  • 63
  • 1
  • 1
  • 8
  • c1 & c2 will not run both commands parallel, if c1 is successful run then only c2 will run. – Raja G May 07 '19 at 11:38
  • 2
    Actually running 'c1 & c2' what it does is run c1 in background and run c2. You can test it out yourself: `(exit 1) && echo "Success"` will not print "Success", while `(exti 1) & echo` "Success" will print "Success" – Kerruba Feb 03 '21 at 09:20
  • 1
    This gave me some insights that I appreciated, therefore upvoting. – Rojan Gh. Sep 10 '21 at 08:48