1

I am trying to run this:

watch -n 0.25 --color "unbuffer iostat -h && ls -lAh ./backup.tar.gz | awk '{ print $5 }'"

Now it almost does what it should, except the awk part which seems to be fully ignored. I get the full output of ls instead of just the file size:

-rw-r--r-- 1 root root 2,7G Jul 16 17:52 ./backup.tar.gz

I assume I need to put apostrophes or so in there and tried around, but couldn't find a solution there. At least not using awk.

Thanks!

Dominik
  • 31
  • 5
  • 1
    When you use double quotes on the outside that makes `$5` be expanded by the parent shell. – Charles Duffy Jul 16 '23 at 15:59
  • Assuming your shell has ANSI C-like string support (like bash and zsh do), use `watch -n 0.25 --color $'unbuffer iostat -h && ls -lAh ./backup.tar.gz | awk \'{ print $5 }\''` – Charles Duffy Jul 16 '23 at 16:00
  • ...that said, using `watch` is not ideal in the first place; it's a _lot_ more efficient if you have a long-running copy of `awk` and a long-running shell (and maybe also a long-running iostat), and have the shell rerun _only_ `ls` over and over instead of restarting the shell _and_ the copy of iostat _and_ the copy of awk every quarter second; the cost of starting new processes adds up. – Charles Duffy Jul 16 '23 at 16:02
  • For example, you can pipe `iostat` into a script that copies its output from stdin to stdout _until_ it sees two blank lines next to each other and then runs `ls` whenever that happens before resuming the loop (assuming your copy of iostat is like mine and prints two lines after each readout when run in `iostat 1` mode [where it infinitely loops]). – Charles Duffy Jul 16 '23 at 16:06
  • I'd also suggest using `stat --format` or `find -printf` instead of `ls | awk` when all you want is to read file size; no reason to print data you don't care about and then discard it, when you could only get what you want in the first place. – Charles Duffy Jul 16 '23 at 16:07
  • Thank you everyone for the details, that helped! – Dominik Jul 17 '23 at 15:11

0 Answers0