How to count lines in a document?

LinuxBashCommand LineScripting

Linux Problem Overview

I have lines like these, and I want to know how many lines I actually have...

09:16:39 AM  all    2.00    0.00    4.00    0.00    0.00    0.00    0.00    0.00   94.00
09:16:40 AM  all    5.00    0.00    0.00    4.00    0.00    0.00    0.00    0.00   91.00
09:16:41 AM  all    0.00    0.00    4.00    0.00    0.00    0.00    0.00    0.00   96.00
09:16:42 AM  all    3.00    0.00    1.00    0.00    0.00    0.00    0.00    0.00   96.00
09:16:43 AM  all    0.00    0.00    1.00    0.00    1.00    0.00    0.00    0.00   98.00
09:16:44 AM  all    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
09:16:45 AM  all    2.00    0.00    6.00    0.00    0.00    0.00    0.00    0.00   92.00

Is there a way to count them all using linux commands?

Linux Solutions

Solution 1 - Linux

Use wc:

wc -l <filename>

This will output the number of lines in <filename>:

$ wc -l /dir/file.txt
3272485 /dir/file.txt

Or, to omit the <filename> from the result use wc -l < <filename>:

$ wc -l < /dir/file.txt

You can also pipe data to wc as well:

$ cat /dir/file.txt | wc -l
$ curl --silent | wc -l

Solution 2 - Linux

To count all lines use:

$ wc -l file

To filter and count only lines with pattern use:

$ grep -w "pattern" -c file  

Or use -v to invert match:

$ grep -w "pattern" -c -v file 

See the grep man page to take a look at the -e,-i and -x args...

Solution 3 - Linux

wc -l <file.txt>


command | wc -l

Solution 4 - Linux

there are many ways. using wc is one.

wc -l file

others include

awk 'END{print NR}' file

sed -n '$=' file (GNU sed)

grep -c ".*" file

Solution 5 - Linux

The tool wc is the "word counter" in UNIX and UNIX-like operating systems, but you can also use it to count lines in a file by adding the -l option.

wc -l foo will count the number of lines in foo. You can also pipe output from a program like this: ls -l | wc -l, which will tell you how many files are in the current directory (plus one).

Solution 6 - Linux

wc -l does not count lines.

Yes, this answer may be a bit late to the party, but I haven't found anyone document a more robust solution in the answers yet.

Contrary to popular belief, POSIX does not require files to end with a newline character at all. Yes, the definition of a POSIX 3.206 Line is as follows:

> A sequence of zero or more non- <newline> characters plus a terminating character.

However, what many people are not aware of is that POSIX also defines POSIX 3.195 Incomplete Line as:

> A sequence of one or more non- <newline> characters at the end of the file.

Hence, files without a trailing LF are perfectly POSIX-compliant.

If you choose not to support both EOF types, your program is not POSIX-compliant.

As an example, let's have look at the following file.

1 This is the first line.
2 This is the second line.

No matter the EOF, I'm sure you would agree that there are two lines. You figured that out by looking at how many lines have been started, not by looking at how many lines have been terminated. In other words, as per POSIX, these two files both have the same amount of lines:

1 This is the first line.\n
2 This is the second line.\n
1 This is the first line.\n
2 This is the second line.

The man page is relatively clear about wc counting newlines, with a newline just being a 0x0a character:

       wc - print newline, word, and byte counts for each file

Hence, wc doesn't even attempt to count what you might call a "line". Using wc to count lines can very well lead to miscounts, depending on the EOF of your input file.

POSIX-compliant solution

You can use grep to count lines just as in the example above. This solution is both more robust and precise, and it supports all the different flavors of what a line in your file could be:

$ grep -c ^ FILE

Solution 7 - Linux

If you want to check the total line of all the files in a directory ,you can use find and wc:

find . -type f -exec wc -l {} +

Solution 8 - Linux

Use wc:

wc -l <filename>

Solution 9 - Linux

If all you want is the number of lines (and not the number of lines and the stupid file name coming back):

wc -l < /filepath/filename.ext

As previously mentioned these also work (but are inferior for other reasons):

awk 'END{print NR}' file       # not on all unixes
sed -n '$=' file               # (GNU sed) also not on all unixes
grep -c ".*" file              # overkill and probably also slower

Solution 10 - Linux

Use nl like this:

nl filename

From man nl:

> Write each FILE to standard output, with line numbers added. With > no FILE, or when FILE is -, read standard input.

Solution 11 - Linux

I've been using this:

cat myfile.txt | wc -l

I prefer it over the accepted answer because it does not print the filename, and you don't have to use awk to fix that. Accepted answer:

wc -l myfile.txt

But I think the best one is GGB667's answer:

wc -l < myfile.txt

I will probably be using that from now on. It's slightly shorter than my way. I am putting up my old way of doing it in case anyone prefers it. The output is the same with those two methods.

Solution 12 - Linux

Above are the preferred method but "cat" command can also helpful:

cat -n <filename>

Will show you whole content of file with line numbers.

Solution 13 - Linux

I saw this question while I was looking for a way to count multiple files lines, so if you want to count multiple file lines of a .txt file you can do this,

cat *.txt | wc -l

it will also run on one .txt file ;)

Solution 14 - Linux

cat file.log | wc -l | grep -oE '\d+'
  • grep -oE '\d+': In order to return the digit numbers ONLY.

Solution 15 - Linux

wc -l <filename>

This will give you number of lines and filename in output.


wc -l 24-11-2019-04-33-01-url_creator.log


63 24-11-2019-04-33-01-url_creator.log


wc -l <filename>|cut -d\ -f 1

to get only number of lines in output.


wc -l 24-11-2019-04-33-01-url_creator.log|cut -d\ -f 1



Solution 16 - Linux

wc -l file.txt | cut -f3 -d" "

Returns only the number of lines

Solution 17 - Linux

Redirection/Piping the output of the file to wc -l should suffice, like the following:

cat /etc/fstab | wc -l

which then would provide the no. of lines only.

Solution 18 - Linux

Or count all lines in subdirectories with a file name pattern (e.g. logfiles with timestamps in the file name):

wc -l ./**/*_SuccessLog.csv

Solution 19 - Linux

count number of lines and store result in variable use this command:

count=$(wc -l < file.txt) echo "Number of lines: $count"

Solution 20 - Linux

This drop-in portable shell function" title="Read More">[ℹ]  works like a charm. Just add the following snippet to your .bashrc file (or the equivalent for your shell environment).

# ---------------------------------------------
#  Count lines in a file
#  @1 = path to file
#  EXAMPLE USAGE: `count_file_lines $HISTFILE`
# ---------------------------------------------
count_file_lines() {
    local subj=$(wc -l $1)
    echo ${subj//[[:space:]]}

This should be fully compatible with all POSIX-compliant shells in addition to bash and zsh.

Solution 21 - Linux

wc -l file_name

for eg: wc -l file.txt

it will give you the total number of lines in that file

for getting last line use tail -1 file_name

Solution 22 - Linux

I tried wc -l to get the number of line from the file name

To do more filtering for example want to count to the number of commented lines from the file use grep '#' Filename.txt | wc -l

echo  "No of files in the file $FILENAME"
wc -l < $FILENAME
echo total number of commented lines
grep '#' $FILENAME | wc -l

Solution 23 - Linux

Awk saves livestime (and lines too):

awk '{c++};END{print c}' < file

If you want to make sure you are not counting empty lines, you can do:

awk '{/^./ && c++};END{print c}' < file

Solution 24 - Linux

Just in case. It's all possible to do it with many files in conjunction with the find command.

find . -name '*.java' | xargs wc -l 

Solution 25 - Linux

I know this is old but still: Count filtered lines

My file looks like:

Number of files sent
Company 1 file: foo.pdf OK
Company 1 file: foo.csv OK
Company 1 file: foo.msg OK
Company 2 file: foo.pdf OK
Company 2 file: foo.csv OK
Company 2 file: foo.msg Error
Company 3 file: foo.pdf OK
Company 3 file: foo.csv OK
Company 3 file: foo.msg Error
Company 4 file: foo.pdf OK
Company 4 file: foo.csv OK
Company 4 file: foo.msg Error

If I want to know how many files are sent OK:

grep "OK" <filename> | wc -l


grep -c "OK" filename

Solution 26 - Linux

As others said wc -l is the best solution, but for future reference you can use Perl:

perl -lne 'END { print $. }'

$. contains line number and END block will execute at the end of script.

Solution 27 - Linux

I just made a program to do this ( with node )

npm install gimme-lines
gimme-lines verbose --exclude=node_modules,public,vendor --exclude_extensions=html

Solution 28 - Linux

if you're on some sort of BSD-based system like macOS, i'd recommend the gnu version of wc. It doesn't trip up on certain binary files the way BSD wc does. At least it's still somewhat usable performance. On the other hand, BSD tail is slow as ............zzzzzzzzzz...........

As for AWK, only a minor caveat though - since it operates under the default assumption of lines, meaning \n, if your file just happens not to have a trailing new line delimiter, AWK will over count it by 1 compared to either BSD or GNU wc. Also, if you're piping in things with no new lines at all, such as echo -n, depending on whether you're measuring at the END { } section or FNR==1, the NR will be different.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlucardView Question on Stackoverflow
Solution 1 - Linuxuser85509View Answer on Stackoverflow
Solution 2 - LinuxLauro OliveiraView Answer on Stackoverflow
Solution 3 - LinuxJohn KugelmanView Answer on Stackoverflow
Solution 4 - Linuxghostdog74View Answer on Stackoverflow
Solution 5 - Linuxbuilt1nView Answer on Stackoverflow
Solution 6 - LinuxChiruView Answer on Stackoverflow
Solution 7 - LinuxstorenView Answer on Stackoverflow
Solution 8 - LinuxVivin PaliathView Answer on Stackoverflow
Solution 9 - Linuxggb667View Answer on Stackoverflow
Solution 10 - LinuxdecimalView Answer on Stackoverflow
Solution 11 - LinuxButtle ButkusView Answer on Stackoverflow
Solution 12 - LinuxYogeshView Answer on Stackoverflow
Solution 13 - LinuxtalsibonyView Answer on Stackoverflow
Solution 14 - LinuxAechoLiuView Answer on Stackoverflow
Solution 15 - LinuxHarsh SarohiView Answer on Stackoverflow
Solution 16 - LinuxUmur KontacıView Answer on Stackoverflow
Solution 17 - Linuxtk3000View Answer on Stackoverflow
Solution 18 - LinuxjwebuserView Answer on Stackoverflow
Solution 19 - LinuxKonstantin FView Answer on Stackoverflow
Solution 20 - Linuxblizzrdof77View Answer on Stackoverflow
Solution 21 - LinuxVikas SharmaView Answer on Stackoverflow
Solution 22 - LinuxSudeep Krishnan MView Answer on Stackoverflow
Solution 23 - Linuxsmac89View Answer on Stackoverflow
Solution 24 - LinuxJorge TovarView Answer on Stackoverflow
Solution 25 - LinuxDiego Fernando Villarroel DiazView Answer on Stackoverflow
Solution 26 - LinuxMajid AzimiView Answer on Stackoverflow
Solution 27 - LinuxdanschView Answer on Stackoverflow
Solution 28 - LinuxRARE Kpop ManifestoView Answer on Stackoverflow