Definition of Linux WC
The Command WC (word count) in Linux OS allows to find out the word count, newline count, and the count of bytes or characters in a file that is mentioned by the file arguments. The output that is returned from word count command will give you the count of lines in a file or the number of words or character in a file.
Syntax:
The Syntax of WC Command is given as:
wc [options].. [file]..
Here options include:
- wc -l: To print the number of lines in a file.
- wc -c: To print the count of bytes in a file.
- wc -w: To print the number of words in a file.
- wc -m: To print the count of characters in a file.
- wc -L: To print the length of the longest line in a file.
Advantages of Wc Command
WC Command in Linux helps to find out the below points:
- Allows to find out the word count, newline count, and the count of bytes or characters in a file that is mentioned by the file arguments.
- The output produced is the four-columnar by default.
- The four columnar output arguments are: the first column gives the line that is in the file, the second column will give you the count of words in the file, the third column will give you the count of characters that is present in the file and the last column is the file name in the argument.
How does WC Command Work in Linux?
We can use –help command in Linux to find out the ways how WC Command works in Linux.
Syntax:
wc --help
Usage:
wc [OPTION]… [FILE]…
wc [OPTION]… –files0-from=F
We can print word, newline, and count the number of bytes for each FILE in the system, and also we can count the total number of lines if there are more than one FILE is mentioned in the arguments. A single word in the file is a non-zero-length of chars that might be delimited with white space. When there is no FILE, or if FILE is -, then we should be reading the standard input.
The options that are mentioned below might be used for selecting the counts that are printed and the ones that are always in the following order – newline, word, character, byte, the maximum length of the line.
Let us take an example file and perform wc command on it. Here we can list the data that is present in fruits.txt file by using cat command.
cat fruits.txt
wc fruits.txt
Output:
When we perform the WC command on the above file, we can see that the output displayed is a four columnar manner. Here the first column gives the line that is in the file that is 5, the second column will give you the count of words in the file that is 5, the third column will give you the count of characters that is present in the file which is 38 and the last column is the file name in the argument that is fruits.txt.
Examples of WC Command in Linux
Below are the examples of options that can be used in WC command with their syntax and examples for better understanding.
1. Option -l
To print only the number of lines that are there in the file, we can use the option ‘l’. The output generated will be two columnar in which the first one is the count of a number of lines and second is the name of the file.
Syntax:
wc -l filename
Example:
wc -l fruits.txt
Output:
2. Option -w
To print only the number of words that are there in the file, we can use the option ‘w’. The output generated will be two columnar in which the first one is the count of a number of words and second is the name of the file.
Syntax:
wc -w filename
Example:
wc -w fruits.txt
Output:
3. Option -c
To print only the number of bytes present in the file, we can use the option ‘c’. The output generated will be two columnar in which first one is the count of number of bytes and second is the name of the file.
Syntax:
wc -c filename
Example:
wc -c fruits.txt
Output:
4. Option -m
To print only the number of characters present in the file, we can use the option ‘m’. The output generated will be two columnar in which first one is the count of number of characters and second is the name of the file.
Syntax:
wc -c filename
Example:
wc -m fruits.txt
Output:
5. Option -L
The option ‘L’ will help to find out the longest (in terms of long characters) line in the file. A character in the file will be considered that will include space, newline or tab.
Syntax:
wc -L filename
Example:
wc -L fruits.txt
Output:
6. Option –version
The option of ‘version’ will help you to print the version of word count(wc) that is running on your system.
Syntax:
wc --version
Example:
wc --version
Output:
Recommended Articles
This is a guide to Linux WC. Here we also discuss the definition and how does wc command work in linux? along with different examples and its code implementation. You may also have a look at the following articles to learn more –
16 Online Courses | 3 Hands-on Projects | 160+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses