wc Command: Tutorial & Examples

Count the number of lines, words, and characters in a file

The wc command in Linux is a command-line utility that is used to count the number of lines, words, and characters in a given file or input. The basic syntax for the command is wc [options] [file(s)].

The wc command can be used with several options:

  • -l: counts the number of lines in a file
  • -w: counts the number of words in a file
  • -c: counts the number of characters in a file
  • -m: counts the number of bytes in a file

For example, to count the number of lines in a file named example.txt, you would use the command wc -l example.txt. This would return the number of lines in the file, followed by the name of the file.

You can also use wc to count the number of lines, words and characters in multiple files by providing more than one file name as an argument. And you can also use it with pipelines, for example if you want to count the number of lines in the output of a command, you can use command | wc -l.

It is important to note that the wc command does not change the original file, it only provides a count of the lines, words, and characters.

Except where otherwise noted, content on this site is licensed under a CC BY-SA 4.0 license CC BY SA