texai
texai

Reputation: 3746

watch file size on linux

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

Upvotes: 56

Views: 55776

Answers (9)

Marcelo Pacheco
Marcelo Pacheco

Reputation: 276

#!/bin/bash  
# Watch File Size and Growth  
# Author: Marcelo Pacheco - [email protected]  
# 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

Upvotes: 0

kvantour
kvantour

Reputation: 26581

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.

Upvotes: 2

Paresh Solanki
Paresh Solanki

Reputation: 59

You can perform this like that:

while true; do
  du -s **file_or_directory**
 sleep **time_interval**
done

Upvotes: 0

Jacajack
Jacajack

Reputation: 779

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 :)

Upvotes: 24

Tim Chaubet
Tim Chaubet

Reputation: 736

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.

Upvotes: 2

myki
myki

Reputation: 763

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

Upvotes: 4

tamasgal
tamasgal

Reputation: 26329

watch -n 5 "du -h club_prod.sql"

Upvotes: 51

geekosaur
geekosaur

Reputation: 61457

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

Upvotes: 7

larsks
larsks

Reputation: 312680

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}'"

Upvotes: 87

Related Questions