6 Wc Command to Count Number of Lines, Words, and Characters in File

wc (short for word count) is a command line tool in Unix/Linux operating systems, which is used to find out the number of newline count, word count, byte and character count in the files specified by the File arguments to the standard output and hold a total count for all named files.

When you define the File parameter, the wc command prints the file names as well as the requested counts. If you do not define a file name for the File parameter, it prints only the total count to the standard output.

In this article, we will discuss how to use the wc command to calculate a file’s newlines, words, characters, or byte count with practical examples.

wc Command Syntax

The syntax of the wc command is shown below.

# wc [options] filenames

The followings are the options and usage provided by the wc command.

  • wc -l – Prints the number of lines in a file.
  • wc -w – prints the number of words in a file.
  • wc -c – Displays the count of bytes in a file.
  • wc -m – prints the count of characters from a file.
  • wc -L – prints only the length of the longest line in a file.

Let’s see how we can use the ‘wc‘ command with the few available arguments and examples in this article. We have used the ‘tecmint.txt‘ file for testing the commands.

Let’s find out the output of the tecmint.txt file using the cat command as shown below.

$ cat tecmint.txt

Red Hat
CentOS
AlmaLinux
Rocky Linux
Fedora
Debian
Scientific Linux
OpenSuse
Ubuntu
Xubuntu
Linux Mint
Deepin Linux
Slackware
Mandriva

1. A Basic Example of WC Command

The ‘wc‘ command without passing any parameter will display a basic result of the ‘tecmint.txt‘ file. The three numbers shown below are 12 (number of lines), 16 (number of words), and 112 (number of bytes) of the file.

$ wc tecmint.txt

12  16 112 tecmint.txt

2. Count Number of Lines in a File

Count the number of newlines in a file using the option ‘-l‘, which prints the number of lines from a given file. Say, the following command will display the count of newlines in a file.

In the output, the first field is assigned as count and the second field is the name of the file.

$ wc -l tecmint.txt

12 tecmint.txt

3. Count Number of Words in a File

The -w argument with the wc command prints the number of words in a file. Type the following command to count the words in a file.

$ wc -w tecmint.txt

16 tecmint.txt

4. Count Number of Characters in a File

When using option -m with the wc command will print the total number of characters in a file.

$ wc -m tecmint.txt

112 tecmint.txt

5. Count Number of Bytes in a File

When using option -c will print the number of bytes of a file.

$ wc -c tecmint.txt

112 tecmint.txt

6. Display Length of Longest Line in File

The ‘wc‘ command allows an argument ‘-L‘, it can be used to print out the length of the longest (number of characters) line in a file.

So, we have the longest character line (‘Scientific Linux‘) in a file.

$ wc -L tecmint.txt

16 tecmint.txt

7. Check wc Command Options

For more information and help on the wc command, simply run the ‘wc --help‘ or ‘man wc‘ from the command line.

$ wc --help
OR
$ man wc
wc Command Usage
Usage: wc [OPTION]... [FILE]...
  or:  wc [OPTION]... --files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified.  A word is a non-zero-length sequence of
characters delimited by white space.

With no FILE, or when FILE is -, read standard input.

The options below may be used to select which counts are printed, always in
the following order: newline, word, character, byte, maximum line length.
  -c, --bytes            print the byte counts
  -m, --chars            print the character counts
  -l, --lines            print the newline counts
      --files0-from=F    read input from the files specified by
                           NUL-terminated names in file F;
                           If F is - then read names from standard input
  -L, --max-line-length  print the maximum display width
  -w, --words            print the word counts
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/wc>
or available locally via: info '(coreutils) wc invocation'

In this article, you’ve learned about the wc command, which is a simple command-line utility to count the number of lines, words, characters, and byes in text files. There are lots of such other Linux commands, you should learn and master your command-line skills.

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Leave a Reply to Larry Cancel reply

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.