Skip to content

wc command in Linux

The Linux wc command is used to count the number of words, lines, and characters in a file. It provides a quick and efficient way to analyze the contents of text files. By using various options with wc, you can customize the output to suit your needs. This handy command is a valuable tool for anyone working with text files in a Linux environment.

wc Syntax:

Terminal window
wc [option] [file]

Linux wc Options:

OptionDescription
-cPrint the byte counts
-mPrint the character counts
-lPrint the newline counts
-wPrint the word counts

wc Parameters:

ParameterDescription
fileThe file to process

How to use wc command:

Count the number of lines in a file

Terminal window
wc -l file.txt

This command counts the number of lines in the file.txt.

Count the number of words in a file

Terminal window
wc -w file.txt

Counts the number of words in the file.txt.

Count the number of characters in a file

Terminal window
wc -m file.txt

Calculates the total number of characters in the file.txt.

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

Terminal window
wc file.txt

Shows the counts of lines, words, and characters in the file.txt.

Count the number of lines in multiple files

Terminal window
wc -l file1.txt file2.txt

Counts the number of lines in file1.txt and file2.txt.

Count the total number of lines in all files in a directory

Terminal window
wc -l *

Calculates the total number of lines in all files within the current directory.

Display only the total line count of multiple files

Terminal window
wc -l file1.txt file2.txt | tail -n 1

Displays only the total line count of file1.txt and file2.txt by using the tail command.

Count the number of lines in a file excluding empty lines

Terminal window
grep -v '^$' file.txt | wc -l

Counts the number of lines in file.txt excluding empty lines using grep in combination with wc.

How do I use wc in Linux?

To use the wc command in Linux, execute the following command:

Terminal window
wc --option <value>

How can I count the number of lines in a file using wc?

To count the number of lines in a file using wc, use the following command:

Terminal window
wc -l filename.txt

How to get the number of words in a file with wc?

To obtain the count of words in a file using wc, run the following command:

Terminal window
wc -w filename.txt

How can I count the number of characters in a file using wc?

To count the number of characters in a file using wc, use the following command:

Terminal window
wc -c filename.txt

How do I display the byte count of a file with wc?

To display the byte count of a file using wc, execute the following command:

Terminal window
wc -c filename.txt

How can I count the number of lines, words, and characters in a file simultaneously with wc?

To count the lines, words, and characters in a file simultaneously using wc, run the following command:

Terminal window
wc filename.txt

How do I exclude newline characters from the line count with wc?

To exclude newline characters from the line count using wc, execute the following command:

Terminal window
wc -l -L filename.txt

How can I count the number of words in multiple files with wc?

To count the number of words in multiple files using wc, run the following command:

Terminal window
wc -w *.txt

How do I get a total count of lines, words, and characters in multiple files with wc?

To get a total count of lines, words, and characters in multiple files using wc, use the following command:

Terminal window
wc -l -w -c *.txt

Applications of the wc command

  • Counting the number of lines in a file
  • Counting the number of words in a file
  • Counting the number of characters in a file
  • Displaying the byte, word, and line counts for a file
  • Counting the number of characters in a file excluding newline characters
  • Applying multiple options such as -c, -w, and -l together for comprehensive output
  • Performing word count operations on multiple files simultaneously