How to Count Lines of a File by Command Line

Jan 2, 2018 - Leave a Comment

How to count lines of a file or document by command line

Need to get the line count of a text file or document? Counting lines of any file is easy at the command line, and the command for line counting is the same in all modern Unix based operating systems, meaning this line counting trick will work the same on Mac OS and Mac OS X, Linux, BSD, and even Windows with Bash shell.

For our purposes here we will use the most direct line counting tool available at the command line, wc. The wc utility is able to display line count, as well as reveal word count and character count. Our focus here is of course on the former, so we’ll be showing how to use wc for counting lines of any text file provided as input.


wc defines a line as “a string of characters delimited by a newline character”, meaning only unique new lines will be counted as a line. So if there are no newline characters in a file, and the file is just one enormous sentence or single command string, it would be reported as one line.

How to Count Lines of Files from the Terminal with wc

  1. Open a Terminal window if you have not done so already (in Mac OS the Terminal application is found in /Applications/Utilities/)
  2. At the command prompt enter the following command syntax, replacing “filename” with the file you want to count lines for
  3. wc -l filename

  4. Hit Return, you will see the line count of the file printed before the file name

How to count lines of a file or text document by command line

As you likely guessed, the -l flag (lower case L) is for “line”.

For example, running the wc -l command on a file located on the desktop called “exampleFileToCountLines.txt ” would look like the following:

% wc -l ~/Desktop/exampleFileToCountLines.txt
1213 /Users/Paul/Desktop/exampleFileToCountLines.txt

As you can see in this example, the line count of the text file given is 1213, meaning the file is 1,213 lines long.

The wc command works the same on any modern Unix based operating system, including Mac OS, Linux, FreeBSD, Windows with Bash, and more.

Using wc to Count Lines, Words, and Character Counts of a File

You can also run the wc command without the -l flag, which will then reveal the line count, the word count, and the character count, in that order. Like:

wc /etc/hosts
9 32 214 /etc/hosts

Contrast that command output to the same command with wc -l flag only:

wc -l /etc/hosts
9 /etc/hosts

This is only counting the lines of a text file as input and it does not modify the file at all. If you do wish to modify the file you can manually add line numbers to a text file via command line.

How to count lines of piped output data with wc

You can also use wc to count lines of any data piped into it, for example from cat or ls:

cat /etc/hosts | wc -l

The output in that scenario will simply be the number of lines in the file, like “9”.

The wc command is pretty neat, you can read the manual page for wc to get even more ideas and tricks for usage.

wc line count tool command line

This is obviously all geared for the command line, but there are other ways of getting line and character counts of files as well.

You can manually count them out for short files, third party Mac apps like BBEdit show line number by default, and if you get the little cousin to BBEdit known as TextWrangler then you can go about showing line numbers in TextWrangler too. You can also make a DIY Word and character counter tool for the Mac as a Service. And if you happen to already know the line number of a file you want to access, you can jump directly to a specific line number in TextEdit, though unfortunately TextEdit doesn’t show line numbers, which seems like an oversight.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Paul Horowitz in Command Line, Tips & Tricks

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac