Wednesday, May 15, 2024
7
rated 0 times [  7] [ 0]  / answers: 1 / hits: 4619  / 2 Years ago, thu, november 24, 2022, 1:26:57

I want to keep an eye on a file as it is created. So I used this command



ls -hal ./file |awk '{print $5}'


which gives the size of the file I am looking for. I use awk because I don't need other stuff, just the file size.



But I'm unable to use this with watch command, because if I try



watch ls -hal ./file |awk '{print $5}'


then watch only accepts ls -hal ./file and then pipes it to awk, giving no output.



If I try



watch "ls -hal ./file |awk '{print $5}'"


then it gets weird showing wrong command and whole output.



Every 2.0s: ls -hal ./file |awk '{print }'              Sun Jul 20 15:52:18 2014

-rw-rw---- 1 aditya aditya 1.7K Jul 20 15:52 ./file


You can see there is no $5 in the awk command.



Further quoting creates various similar errors.



So what is the right way to quote this command?


More From » command-line

 Answers
3

One option would be to escape the $ in your awk expression



watch "ls -hal ./file |awk '{print $5}'"


Alternatively, you could avoid the issue altogether by using stat instead of parsing the output of ls



watch stat -c '%s' ./file

[#24057] Friday, November 25, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ligdesig

Total Points: 164
Total Questions: 106
Total Answers: 114

Location: Japan
Member since Sat, Jun 6, 2020
4 Years ago
;