wc Command in Linux – A Complete Guide To Counting Variables in Files 

wc Command in Linux

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

The wc command in Linux is one of the simplest yet the most powerful utilities used to count lines, words, characters, and bytes in a text file. Whether you are a system administrator looking to analyze files or a developer reviewing code, wc command helps provide quick insights into file content without requiring a full-blown editor. 

In this guide, you will learn how to use the wc command in Linux, understand the important flags, and explore practical examples. 

Why Use the wc Command in Linux?

The wc command in Linux is crucial for anyone who is working with text files in Linux. It helps you quickly determine the number of lines, words, characters, or bytes in a file or an output. This is particularly useful for:

  • Analyzing large log files.
  • Counting lines of code.
  • Validating output in scripts.
  • Getting quick file statistics without opening the file.

It’s a fast and lightweight alternative to opening files in an editor just to see basic metrics.

Related Article: Linux cat Command: A Complete Guide to Viewing and Combining Files

Basic Syntax of wc

The basic syntax of the wc command in Linux is: 

Tech Delivered to Your Inbox!

Get exclusive access to all things tech-savvy, and be the first to receive 

the latest updates directly in your inbox.

wc [OPTION]… [FILE]…

  • [OPTION]: Controls what wc counts (e.g., lines, words, characters).
  • [FILE]: Specifies the file(s) you want to analyze. If no file is given, wc reads from standard input.
  • Common Options:
  • -l → Count lines
  • -w → Count words
  • -c → Count bytes
  • -m → Count characters (useful for multibyte characters)
  • -L → Print the length of the longest line

Example:

wc -l filename.txt

This command displays the number of lines in filename.txt.

Counting Lines with wc -l

Use the -l option to count the number of lines in a text file. This is mainly crucial when you are analyzing log files or large databases. 

wc -l filename.txt

This displays the total number of lines in filename.txt.

Counting Words with wc -w

To count the total number of words in the text file, use the -w option:

wc -w filename.txt

This would display the word count in filename.txt.

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

Counting Bytes with wc -c

The -c option counts the total number of bytes in the specified text file. This is mainly helpful to check the file size without using ls or du.

wc -c filename.txt

This shows the file filename.txt has X number of bytes.

Counting Characters with wc -m

Use the -m to count the number of characters, which can be different from the bytes if your file contains multibyte characters like UTF-8. 

wc -m filename.txt

Counting Length of the Longest Line with wc -L

The -L option returns the exact number of characters in the longest line of the text file. 

wc -L filename.txt

How to Use wc with Multiple Files

If you need to pass multiple files to the wc command and compare their lines, words, or character counts side-by-side, use this command-line:

wc -l file1.txt file2.txt

This shows the number of lines in each file, followed by a total.

You can use any combination of wc flags with multiple files.

Using wc in Pipelines and Scripts

wc is often used in combination with pipes (|) to analyze the output of other commands. Here are a few examples:

  • Count lines in a directory listing:

ls | wc -l

  • Count the number of users currently logged in:

who | wc -l

  • Count the number of .txt files in a folder:

ls *.txt | wc -l

You can also embed wc in shell scripts to automate file analysis.

Common Use Cases and Examples

Here are a few real-world scenarios where wc is especially useful:

  • Log file analysis: Count the number of log entries in a file.

wc -l /var/log/syslog

  • Word count in documents:

wc -w report.txt

  • Checking file sizes in bytes:

wc -c *.csv

  • Scripting file checks:

if [ $(wc -l < somefile.txt) -gt 100 ]; then

  echo “File has more than 100 lines.”

fi

Troubleshooting Common Errors

IssueCauseFix
wc: file.txt: No such file or directoryFile doesn’t exist or wrong path usedVerify file path and spelling
wc: Permission deniedYou don’t have read access to the fileUse sudo or adjust file permissions
Unexpected high byte countFile contains binary or non-ASCII charactersUse wc -m to count characters instead
wc returns no output in a scriptInput may be empty or command is piped incorrectlyTest input and check syntax of redirection or pipeline
Total count not shown with multiple filesOnly one file was providedProvide at least two files to see the total row

Conclusion

The wc command in Linux is a super simple and powerful utility that helps you quickly count the lines, words, bytes, characters, and even the length of the longest line in a file. So, if you are analyzing files, writing shell scripts, or just exploring the file, wc can be of huge help to you! 

FAQs

Can I use wc with other commands?

Yes, you can pipe output. For example:

cat file.txt | grep "Linux" | wc -l
This counts lines containing “Linux.”

What’s the difference between wc -c and wc -m?

- wc -c counts bytes.
- wc -m counts characters. (They may differ when dealing with multibyte characters like UTF-8.)

Can I use wc with multiple files?

Yes, you can specify multiple filenames, and wc will display counts for each file along with a total.

Marium Fahim
Hi! I am Marium, and I am a full-time content marketer fueled by an iced coffee. I mainly write about tech, and I absolutely love doing opinion-based pieces. Hit me up at [email protected].
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!