Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 43 down vote accepted

Indeed there is. It is called wc, originally for word count, I believe, but it can do lines, words, characters, bytes, and the longest line length. The -l option tells it to count lines.

wc -l mytextfile
share|improve this answer
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
@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
share|improve this answer
Indeed another option... a terribly over-engineered option, granted, but an option. – Michael Kjörling Jan 19 '12 at 10:50
This could also be written shorter: grep -c $ some_file – user13742 Jan 19 '12 at 11:46
@ 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

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.

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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