Intro to wc Command in Linux

Mou
2 min readMay 11, 2023

--

The wc command in Linux is a tool used to count the number of lines, words, and characters in a file or a group of files. It is a basic and essential tool used for text processing in Linux. The wc command is also used to display the size of a file. The wc command takes one or more filenames as arguments, and it returns the number of lines, words, and characters in each file.

Linux wc Command Syntax:

The syntax of the wc command in Linux is as follows:

wc [OPTION]... [FILE]...

The square brackets indicate optional arguments. The wc command takes the following options:

  • -l: Count the number of lines in the file.
  • -w: Count the number of words in the file.
  • -c: Count the number of characters in the file.

Passing only one file name in the argument:

To count the number of lines, words, and characters in a single file, the following command can be used:

wc filename.txt

This command will return the number of lines, words, and characters in the file.

Passing more than one file name in the argument:

To count the number of lines, words, and characters in multiple files, the following command can be used:

wc file1.txt file2.txt file3.txt

This command will return the number of lines, words, and characters in each file.

The wc command has several applications, including:

  1. To count all files and folders present in the directory:

The following command can be used to count the number of files and folders present in a directory

ls -1 | wc -l

This command will return the total number of files and folders present in the current directory.

  1. Display number of word count only of a file:

The following command can be used to display the number of words in a file:

wc -w filename.txt

This command will return the number of words in the file.

  1. Detect hidden characters with wc:

The following command can be used to detect hidden characters in a file:

cat filename.txt | LC_ALL=C tr -dc '\0-\177' | wc -c

This command will return the number of hidden characters in the file.

  1. Get the size of a file with wc:

The following command can be used to get the size of a file:

wc -c filename.txt

This command will return the size of the file in bytes.

Conclusion:

In conclusion, the wc command is a useful tool for text processing in Linux. It can be used to count the number of lines, words, and characters

--

--