15. wc and nl

In Linux, analyzing text files is a common task. Two fundamental utilities for this are wc and nl, which help you count content and number lines, respectively.

Counting with the wc Command

The wc (word count) command is a powerful tool for basic file analysis. When run on a file, it provides a summary of its contents.

$ wc /etc/passwd
 96     265    5925 /etc/passwd

The output displays three numbers followed by the filename. From left to right, these numbers represent:

  1. The number of lines.
  2. The number of words (the Linux word count).
  3. The number of bytes.

Getting Specific Counts

Often, you only need one piece of information. You can use options to display a specific count instead of all three.

  • -l: Shows only the line count.
  • -w: Shows only the word count.
  • -c: Shows only the byte count.

For example, to see just the number of lines in the /etc/passwd file, you would use:

$ wc -l /etc/passwd
96

Numbering Lines with the nl Command

Another useful command for inspecting files is nl (number lines). As its name suggests, it reads a file and outputs its content with line numbers added to the beginning of each line. This is especially helpful for reviewing scripts or configuration files.

Consider a file named file1.txt with the following content:

i
like
turtles

Using the nl command, you can easily add Linux line numbers:

$ nl file1.txt
     1 i
     2 like
     3 turtles

Both wc and nl are essential commands for everyday text processing on the Linux command line.

Sign in to save your learning progress

Sign in

Exercises

To master these commands, hands-on practice is key. Try these exercises to solidify your skills in text counting and line numbering on Linux:

  1. Linux wc Command: Text Counting - Practice counting words, lines, and characters in text files using the wc command.
  2. Linux nl Command: Line Numbering - Learn to number lines in text files with the nl command.
  3. Word Count and Sorting - Apply your knowledge of wc to count lines, words, and characters, and combine it with sorting for practical text analysis tasks.

These labs will help you apply the concepts in real-world scenarios and build confidence with text processing in Linux.

Quiz

Which command and option would you use to display only the total word count of a file? Please answer using only the command and its option in English. The answer is case-sensitive.