Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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

Is this the correct way to show the line count of a specific file?

cat file | grep * -c
share|improve this question

You can use this command:

wc -l <file>

This will return the total line number count in the provided file.

share|improve this answer

Your shell will "expand" the asterisk in grep * -c to everything in the current directory, resulting in, for instance:

grep foo bar baz -c

Which is not what you want.

Try cat file | grep -c . to count the number of rows containing at least a printable character, or cat file | wc -l to count the number of lines.

If the input is a file, however, you may consider giving access to the file instead of piping it on stdin, to the command that does the counting. (for example wc -l file or grep . -c file).

If you don't want wc to show the filename when giving it a filename, you can extract the first word of the output of wc -l with your favorite filter, such as cut(1): wc -l foo | cut -d' ' -f 1 or awk(1): wc -l foo | awk '{print $1}', or something else with the same effect.

share|improve this answer

Number all non-empty lines:

cat file | nl

Or include everything:

cat file | nl -ba
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .