Counting Number Of Lines in a Big File – Linux Shell Command

By | May 20, 2008

Often it is necessary to work with big files, that cannot be viewed using standard editors. For example, If you have a 4Gb file, I think you won’t open it with any text editor. But what if you need to know how many bytes are in this file, of how many strings it contains? There is a linux command that allows you to do all this stuff faster than you think. Let’s show it: it’s wc. I’ll describe its options here.

-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

So in order to count the number of lines in a file, go to shell and issue the following command: wc -l <filename>. Enter wc -L <filename> to find the longest line of a file. I think other options are clear. If you’re looking for PHP usage of this command, you are welcome to use system() to gain its output and use for your needs. It can be useful for getting random lines from files, and many other things you might need.