Counting Lines, Words, or Characters in a File

Problem

You need to know how many lines, words, or characters are in a given file.

Solution

Use the wc (word count) command with awk in a command substitution.

The normal output of wc is something like this:

$ wc data_file
       5     15       60 data_file

# Lines only
$ wc -l data_file
       5 data_file

# Words only
$ wc -w data_file
      15 data_file

# Characters (often the same as bytes) only
$ wc -c data_file
      60 data_file

# Note 60B
$ ls -l data_file
-rw-r--r--  1 jp  users   60B Dec    6 03:18 data_file

You may be tempted to just do something like this:

data_file_lines=$(wc -l "$data_file")

That won’t do what you expect, since you’ll get something like "5 data_file" as the value. Instead, try this:

data_file_lines=$(wc -l "$data_file" | awk '{print $1}')

Discussion

If your version of wc is locale aware, the number of characters will not equal the number of bytes in some character sets.

Get bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.