54

I want to watch the growing size of a single file, so i use this command:

texai@maelstrom ~$ ls -lh club_prod.sql | awk '{print $5}'
116M

Now I want to watch that result each 5 seconds so:

texai@maelstrom ~$ watch -n 5 ls -lh club_prod.sql | awk '{print $5}'

but this command doesn't return any result

larsks
  • 277,717
  • 41
  • 399
  • 399
texai
  • 3,696
  • 6
  • 31
  • 41

9 Answers9

81

You're piping the output of watch into awk. If you simplify your command line, what you have is:

 watch <some arguments> | awk '{print $5}'

That's not what you want. Try:

watch -n 5 "ls -lh club_prod.sql | awk '{print \$5}'"
larsks
  • 277,717
  • 41
  • 399
  • 399
  • watch -n 5 "ls -lh acro | awk '{print \$5}' >> acro_size.txt" works for me, when I do it like above it only shows the size one time? (acro is just my test file) – Emanuel Berg May 11 '12 at 19:05
  • Hmm, works for me as written. If I change the size of the file, the display updates every 5 seconds. Your example in the comment above appears to be redirecting the output from awk to a file...which means you're hardly benefiting from using `watch`, which is meant as a display tool. – larsks May 11 '12 at 19:23
  • 1
    Aha, now I see, it worked for me too (probably), only I expected to get I list, not an update of the entire screen. If you would like to do it to a file as I did, there is probably a better command for just repeating without showing. My mistake. – Emanuel Berg May 12 '12 at 18:31
  • `while :; do ls -lh club_prod.sql >> acro_size.txt; sleep 60; done` – larsks May 12 '12 at 19:44
  • 2
    Why use `ls`/`awk` and piping, when there is a single command `du` for it? – tamasgal Nov 02 '12 at 08:53
  • I was answering the question of why the `watch` statement wasn't working. There are certainly more effective ways of getting at the size of a file. – larsks Nov 02 '12 at 11:24
50
watch -n 5 "du -h club_prod.sql"
tamasgal
  • 24,826
  • 18
  • 96
  • 135
  • 1
    This works well on directory with subdirectories psql dumps with the -s flag: ```watch -n 5 "du -sh directory_with_subdirectories/"``` – pizoelectric Dec 16 '21 at 14:28
24

Not exactly related, but if you want to monitor growth rate of some file, you could use following command:

tail -f yourfile.txt | pv > /dev/null

  • tail -f - outputs data appended to file
  • pv - measures data flow through pipe
  • > /dev/null - standard output gets discarded

Note: sometimes pv may be not preinstalled

I hope this will help somebody :)

Jacajack
  • 759
  • 2
  • 11
  • 23
7

You need to quote the pipeline so that it is done within watch.

watch -n 5 "ls -lh club_prod.sql | awk '{print \$5}'"

Note also the \ added to \$5 because the outer quotes are now double quotes, in which $-variables are expanded. (Other methods of quoting are generally uglier than this.)

geekosaur
  • 59,309
  • 11
  • 123
  • 114
4
watch -n 5 "ls -lh club_prod.sql | awk '{print \$5}'"
myki
  • 773
  • 5
  • 7
  • Note that `$5` in this example will get eaten by the shell. – larsks May 11 '12 at 16:21
  • In my case using Ubuntu server I had to do print \$6 not sure if this is because linux now has more output on an ls -lh? But if someone runs into this try 6 ;) – Uncle Iroh Jul 17 '18 at 16:49
2

For fast detailed growth watching of a file, every 0.1 second:

watch -n 0.1 "ls -l /mnt/some/file | awk '{print \$5}' | sed -re ' :rep ; s/([0-9])([0-9]{3})($|[^0-9])/\1,\2\3/ ; t rep '"

This will produce something like 62,673,539,072.

Tim Chaubet
  • 498
  • 1
  • 7
  • 15
2

The usage of watch is correct, but the usage of ls I would avoid. I would recommend the usage of stat or du, but this depends on what you want.

  • du: If you want the space occupied on your drive
  • stat: If you want the number of bytes your file contains (how many bytes can I read from the file)

Imagine working with a compressed file system, or with processing sparse files, internal fragmentation, indirect blocks ...

For both cases, the result would be:

$ watch -n 5 'stat --printf "%s\n" file'
$ watch -n 5 'du -B1 file'

Both results can actually be obtained in a single command with stat:

$ watch -n 5 'stat --printf "%s %b %B\n" file'

The product of the last two columns is the result of du.

kvantour
  • 25,269
  • 4
  • 47
  • 72
0

You can perform this like that:

while true; do
  du -s **file_or_directory**
 sleep **time_interval**
done
Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44
0
#!/bin/bash  
# Watch File Size and Growth  
# Author: Marcelo Pacheco - marcelo@m2j.com.br  
# Syntax: watchfilesize filetomonitor
nm="$1"  
while true  
do  
  sz=$(stat -c %s "$nm")  
  sleep 1m  
  sz1=$(stat -c %s "$nm")  
  echo Growth: $(((sz1-sz)/1024))KB/min Size: $((sz1/1024/1024))MB  
  sz=$sz1  
done
Marcelo Pacheco
  • 152
  • 1
  • 5