Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is WC Command in Linux?
3.
Brief History of wc
4.
Syntax of wc Command
5.
Command Options and Examples
5.1.
-l: Counting Lines
5.1.1.
Example with one file:
5.1.2.
Example with multiple files:
5.2.
-w: Counting Words
5.2.1.
Example with one file:
5.2.2.
Example with multiple files:
5.3.
-m: Counting Characters
5.3.1.
Example with one file
5.3.2.
Example with multiple files
5.4.
-c: Counting Bytes
5.4.1.
Example with one file
5.4.2.
Example with multiple files
5.5.
-L: Finding the Longest Line Length
5.5.1.
Example with one file
5.5.2.
Example with multiple files
6.
Applications of wc Command
7.
Advantages
8.
Disadvantages
9.
Frequently Asked Questions
9.1.
What does Linux command wc do?
9.2.
What does wc +1 mean in Linux?
9.3.
What is wc character count?
9.4.
How do I exit wc command?
10.
Conclusion
Last Updated: Mar 27, 2024
Easy

WC Command in Linux

Author Pallavi singh
0 upvote

Introduction

The wc (word count) command is an indispensable tool in the Linux command line for text processing. It provides a concise yet comprehensive way to count lines, words, characters, and bytes in text files. 

wc command in linux

This article will provide a detailed exploration of the wc command, with a focus on its various options and their practical applications.

What is WC Command in Linux?

The wc command in Linux is used to count the number of lines, words, and characters in a text file or input stream. The name "wc" stands for "word count," but it provides more than just word count. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Brief History of wc

The wc command has a history rooted in early Unix systems. The command was designed to provide users with a quick and efficient way to obtain counts of lines, words, and characters in text files. The concept of "word count" was initially a simple utility for assessing the size and content of text-based files, aiding users in managing and analyzing textual data.

As Unix and its derivatives evolved, the wc command remained a fundamental tool in the command-line toolkit. It became standard in Unix-like operating systems, including Linux, and has maintained its core functionality over the years.

Syntax of wc Command

The wc command is invoked with the following syntax:

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


Without specifying any options, wc will return the line, word, and byte count for the input files.

Command Options and Examples

-l: Counting Lines

The -l option directs wc to count the number of lines in a file.

Example with one file:


$ wc -l report.txt
42 report.txt


This output indicates that report.txt contains 42 lines.

Example with multiple files:

$ wc -l report.txt summary.txt
42 report.txt
27 summary.txt
69 total


Here, report.txt has 42 lines, summary.txt has 27 lines, and the total line count for both files is 69.

-w: Counting Words

The -w option instructs wc to count the number of words in a file.

Example with one file:

$ wc -w document.txt
350 document.txt


This command shows that document.txt contains 350 words.

Example with multiple files:

$ wc -w document.txt overview.txt
350 document.txt
120 overview.txt
470 total


In this case, document.txt has 350 words, overview.txt has 120 words, and the combined word count is 470.

-m: Counting Characters

The -m option causes wc to count the number of characters in a file.

Example with one file

$ wc -m notes.txt
2048 notes.txt


notes.txt is composed of 2048 characters.

Example with multiple files

$ wc -m notes.txt summary.txt
2048 notes.txt
1024 summary.txt
3072 total


notes.txt has 2048 characters, summary.txt has 1024 characters, and the total character count is 3072.

-c: Counting Bytes

The -c option tells wc to count the number of bytes in a file.

Example with one file

$ wc -c data.bin
4096 data.bin


data.bin is 4096 bytes in size.

Example with multiple files

$ wc -c data.bin report.bin
4096 data.bin
2048 report.bin
6144 total


data.bin is 4096 bytes, report.bin is 2048 bytes, and the total byte count is 6144.

-L: Finding the Longest Line Length

The -L option outputs the length of the longest line in a file.

Example with one file

$ wc -L state.txt
17 state.txt


The longest line in state.txt is 17 characters long.

Example with multiple files

$ wc -L state.txt capital.txt
17 state.txt
10 capital.txt
17 total


The longest line in state.txt is 17 characters, in capital.txt it is 10 characters, and the longest line among both files is 17 characters. Note that the total here represents the length of the longest line found in any of the files, not a sum.

Applications of wc Command

wc is used in various scenarios, such as:

Applications of wc Command
  • Scripting: Automating text processing in shell scripts.
     
  • Programming: Counting code lines or blocks to assess the size of a project.
     
  • Data Analysis: Preliminary analysis of data files by counting entries.
     
  • System Administration: Monitoring log files by counting entries over time.

Advantages

  • Efficiency: wc is incredibly fast, even with large files. For example, counting the words in a 500MB text file takes mere seconds.
     
  • Versatility: It can be combined with other commands through pipes, such as grep to count specific entries:
grep 'pattern' filename.txt | wc -l
  • Simplicity: wc is straightforward to use, with an easy-to-remember syntax.

Disadvantages

  • Limited Context: wc doesn't provide context for the counts. For instance, it can tell you how many words are in a document, but not where they are located.
     
  • Text-Based: It's designed for text data and may not handle binary files as expected.
     
  • Locale Sensitivity: Word boundaries are determined by the current locale, which might not match the user's expectations in all cases.

Frequently Asked Questions

What does Linux command wc do?

The wc command in Linux counts lines, words, and characters in a text file or input stream.

What does wc +1 mean in Linux?

wc +1 is not a valid syntax. The correct option is -l, which counts lines.

What is wc character count?

wc -c returns the character count in a file or input stream using the wc command in Linux.

How do I exit wc command?

The wc command processes and exits automatically, displaying the counts specified (lines, words, and characters) in the terminal.

Conclusion

The wc command is a testament to the power and simplicity of Linux command-line tools. It's a utility that, despite its straightforward nature, plays a vital role in text processing and analysis. Whether you're a developer, a data scientist, or a system administrator, understanding and utilizing the wc command can significantly streamline your workflow. Armed with the knowledge from this guide, you're now ready to wield the wc command to its full potential in your daily tasks.

Recommended Reads: 

Features of linux operating system

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Previous article
mv Command in Linux
Next article
How to Boot Linux from USB?
Live masterclass