smart wc(1)

As well all know, the -c options of (1) command in gives the number of bytes in a file. wc -c never reads the entire file for counting the number of bytes. Instead, it does a stat on the file and gets the number of bytes occupied by the file. This is the case if we say wc -c file. Now consider the case of wc -c < file. That also gives the same output – the number of bytes occupied by the file. In this case, the difference is that, the redirection is setup by the shell and the contents of the file is fed to the stdin of wc. Some smart implementations of wc will figure out the actual filename by doing a stat on the stdin and gets the byte count by doing stat on the file. This is highly efficient compared to reading the entire file and counting the number of bytes. wc on latest linux distributions does this way. The same on FreeBSD or solaris will take longer time to execute the same command.

$ ls -lh somebigfile
-rw-r--r-- 1 jemshad jemshad 472M Apr  1  2009 somebigfile

$ time wc -c somebigfile
494786425 somebigfile

real    0m0.003s
user    0m0.000s
sys    0m0.004s

$ time wc -c < somebigfile
494786425

real    0m0.003s
user    0m0.000s
sys    0m0.004s

Source: #bash channel at irc.freenode.net

This entry was posted in linux, tech and tagged , , . Bookmark the permalink.

Leave a Reply