4

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?

6

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
  • 1
    @RegisteredUser I recommend to accept this (steeldriver's) answer instead of mine because it gives right solution for awk and also sort syntax by stat! – Pandya Jul 20 '14 at 11:02
2

Use following command :

watch 'ls -hal ./file|cut -d " " -f 5'

And if you want to highlight difference than,

watch -d 'ls -hal ./file|cut -d " " -f 5'

This will give work as you want!

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.