Master the Linux ‘wc’ Command: A Comprehensive Guide

Peter Hou
3 min readMay 20, 2023

--

This guide delves deep into the ‘wc’ command in Linux, giving a comprehensive look at its history, usage, parameters, and common use cases. The ‘wc’ command is an essential tool for any Linux user, with capabilities that extend beyond simple word counting to provide valuable information about files and outputs. By the end of this article, readers will be able to confidently use the ‘wc’ command in their everyday Linux usage.

Instructions

This article provides a detailed explanation of the ‘wc’ command in Linux. We will cover its history, usage, common parameters, other supported parameters, common use cases, tricks, and things to note. By the end of this guide, you will have a deep understanding of the ‘wc’ command.

History

The ‘wc’ command is a standard Linux command and was one of the original commands in the first version of Unix. The command name ‘wc’ stands for ‘word count’, and it is primarily used to count the number of lines, words, and characters in files.

When and why to use it

The ‘wc’ command is extremely useful when you need to quickly count the number of lines, words, or characters in a file. It’s a fundamental tool in text processing tasks, and especially useful in scripting and programming tasks where you need to automate the process of counting.

How to use it

To use the ‘wc’ command, simply type ‘wc’ followed by the file name.

$ wc filename.txt
24 150 1025 filename.txt

In the output above, 24 represents the number of lines, 150 represents the number of words, and 1025 represents the number of characters in the file.

The commonly used parameters

Here are some commonly used parameters:

  • -l This counts the number of lines in a file.
$ wc -l filename.txt
24 filename.txt
  • -w This counts the number of words in a file.
$ wc -w filename.txt
150 filename.txt

Other supported parameters

  • -c This counts the number of bytes in a file.
  • -m This counts the number of characters in a file.
  • -L This finds the length of the longest line in a file.

Most common use cases

One common use case of the ‘wc’ command is in conjunction with other commands using pipes. For example, to count the number of files in a directory, you could use:

$ ls | wc -l
6

The tricky skills

‘wc’ can be combined with other commands to perform complex tasks. For example, to count the number of processes running on your system, you can use:

$ ps aux | wc -l
87

What needs to be noted

The ‘wc’ command counts newline characters as part of its character and byte count. Therefore, the character and byte count may be larger than the actual text content in some cases.

Conclusion

The ‘wc’ command is a powerful and versatile tool in Linux, allowing users to count lines, words, characters, and bytes in files quickly. Although it has a simple premise, its true potential is realized when combined with other commands in scripting and programming.

--

--

Peter Hou
Peter Hou

Written by Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.

No responses yet