This question already has an answer here:

How can I find how many lines a text file contains without opening the file in an editor or a viewer application? Is there a handy Unix console command to see the number?

marked as duplicate by don_crissti, Jeff Schaller, jimmij, Anthon, roaima Apr 30 '17 at 22:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Indeed there is. It is called wc, originally for word count, I believe, but it can do lines, words, characters, bytes (and with some implementations, the length in bytes of the longest line or the display width of the widest one). The -l option tells it to count lines (in effect, it counts the newline characters, so only properly delimited lines):

wc -l mytextfile

Or to only output the number of lines:

wc -l < mytextfile

(beware that some implementations insert blanks before that number).

  • 5
    Yes wc is very useful, but it is worth noting that the longest line length option -L is quirky... see wc -L reports a line-length of 8 for a tab-char – Peter.O Jan 19 '12 at 5:16
  • when I use this command, the system not only gives my number of lines, but also the 'name of the file' right next to the 'number of lines'. I am using bash shell on Ubuntu 12.04 32 bit – Abhinav Oct 19 '13 at 8:04
  • @Abhinav Yes, it does that. If you need just the number, pipe it through awk: wc -l mytextfile | awk '{print $1}' – Kevin Oct 19 '13 at 14:23
  • Wow, this would actually be a legitimate use of cat: cat mytextfile | wc -l. – terdon Jul 9 '14 at 3:18
  • 1
    @Kevin piping the output of wc through awk makes no sense. In the case of only wanting the number, this can be accomplished in pure awk with no difficulty (see my answer). – HalosGhost Aug 24 '14 at 19:29

Another option would be to use grep to find the number of times a pattern is matched:

grep --regexp="$" --count /path/to/myfile.txt

In this example,

  • $ is an expression that evaluates to a new line (enter button is pressed)
  • --count suppresses normal output of matches, and displays the number of times it was matched.
  • The /path/to/myfile.txt is pretty obvious, I hope :)

EDIT: As mentioned by @hesse in the comments, this can be shortened to

grep -c $ path/to/file

Which would also make it standard and portable to non-GNU grep implementations.

  • 6
    Indeed another option... a terribly over-engineered option, granted, but an option. – a CVn Jan 19 '12 at 10:50
  • 4
    This could also be written shorter: grep -c $ some_file – user13742 Jan 19 '12 at 11:46
  • 1
    @ MichaelKjörling Dunno about 'terribly' over-engineered. If I was using this in a script or similar I would definitely want to use the lighter-weight wc. If I'm at a prompt and I'm curious about a file, I'd probably use grep, because I'm more familiar with it. – DavidDraughn Jan 20 '12 at 21:59
  • 1
    In my tests (on a GNU system), I find that grep -c '^' is significantly faster than grep -c '$'. – Stéphane Chazelas Oct 3 '16 at 14:12

I would also add that it is quite easy to do this in pure awk if you, for some reason, wished to not use wc.

$ awk 'END { print NR }' /path/to/file

The above prints the number of records (NR) present in the file at /path/to/file.

Note: unlike wc, this will not print the name of the file. So, if you only wanted the number, this would be a good alternative to cat file | wc -l.

  • This is good because if there's an unterminated line at the end, i.e. there's no newline character after it, wc will undercount. And it's not as overengineered as the grep option. – ijoseph Jun 9 '18 at 0:32

As @Kevin suggested, you can use wc command to count lines in a file. However, wc -l test.txt will include the file name in the result. You can use:

wc -l < test.txt

to just get the number of lines without file name in it. Give it a try.


As per my test, I can verify that the AWK is way faster than the other tools (GREP, SED, AWK, PERL, WC)

Here is the result of the test that I ran here

date +%x_%H:%M:%S; grep -c $ my_file.txt; date +%x_%H:%M:%S; 

10/03/16_09:41:51 24579427 10/03/16_09:42:40 ~49 Seconds

date +%x_%H:%M:%S; wc -l my_file.txt; date +%x_%H:%M:%S; 

10/03/16_09:35:05 24579427 my_file.txt 10/03/16_09:35:45 ~40 Seconds

date +%x_%H:%M:%S; sed -n '$=' my_file.txt; date +%x_%H:%M:%S; 

10/03/16_09:33:50 24579427 10/03/16_09:34:29 ~39 Seconds

date +%x_%H:%M:%S; perl -ne 'END { $_=$.;if(!/^[0-9]+$/){$_=0;};print "$_" }' my_file.txt; date +%x_%H:%M:%S; 

10/03/16_09:36:11 24579427 10/03/16_09:36:36 ~25 Seconds

date +%x_%H:%M:%S; awk 'END { print NR }' my_file.txt; date

10/03/16_09:43:36 24579427 10/03/16_09:43:58 ~22 Seconds

  • You may want to specify your system and describe your modus operandi (size and shape of your file, is the file loaded in cache, on how many invocations did you take an average...). In all my tests, wc always comes out significantly faster than anything else as you'd expect from a specialised application. – Stéphane Chazelas Oct 3 '16 at 14:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.