Yesterday I wrote a custom log file rotator script in bash for one of my embedded Linux boxes (yes, I know about logrotate but this was a special situation that required a custom script).
One of the building blocks I needed for this script was a function that could return the size of a file. The wc command seemed appropriate for this:
me@mycomputer:~$ wc --help Usage: wc [OPTION]... [FILE]... Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified. With no FILE, or when FILE is -, read standard input. -c, --bytes print the byte counts -m, --chars print the character counts -l, --lines print the newline counts -L, --max-line-length print the length of the longest line -w, --words print the word counts --help display this help and exit --version output version information and exit
Yep, the -c option was the one that I wanted. Here is an example of retrieving the size of the file called foo:
me@mycomputer:~$ wc -c foo 1071 foo
The size of the file is 1071 bytes. But what’s with the extra “foo” string? I didn’t want that. I justed wanted the 1071. What’s a boy to do? awk to the rescue. Awk is a general purpose programming language that is designed for processing text files and streams. For example, you can use it to parse data fields from text that is piped in from another command. Here is how I used awk to parse just the file size from the output of wc:
me@mycomputer:~$ wc -c foo | awk '{print $1}'
1071
I hope this helps someone else. If you have a better way of getting just the file size, please leave a comment.
UPDATE
Here is an easier (and better performing?) way of getting just the file size:
stat -c%s filename




wc -c < foo
Thanks Anonymous, that works great!
stat -c%s foo
wc will cause the entire file to be read from disk, so it’s better to use stat. That way it’ll take the same amount of time no matter how big the log file is :-)