How to Use the wc Command in Linux

wc is a command-line utility in Linux that stands for word count. It allows us to count the number of characters, words, and lines in a specified file. By default, this command returns three columns: the number of lines, words, and characters. If we use this command with a file, the fourth column shows the file name.

By default, the wc command returns the output to the console; however, we can redirect it to a specific file as well, if needed. In this tutorial, we will explore the Linux wc command with practical examples.

What is wc in Linux

The wc is a text processing command that counts lines, words, bytes, and characters. It returns the output in the following sequence: the number of lines, words, characters, bytes, and file name. Additionally, we can combine wc with the grep command in Linux, cat command, or ls command using pipes to handle more advanced tasks.

How to Use wc Command in Linux

The basic usage of the wc command implies wc followed by option (if any) and then the target file:

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

Here, FILE represents a target file whose newlines, words, characters, etc., need to be counted. The OPTION can be one of the following optional flags that are supported by the wc command:

  • -c, –bytes: It counts the total bytes.
  • -m, –chars: It counts the number of characters.
  • -l, –lines: It counts the newlines.
  • –files0-from=F: It tells the wc command to read input from specified files.
  • -L, –max-line-length: It prints the maximum display width.
  • -w, –words: It counts the total words.
  • –help: It shows the help page of the wc command.
  • –version: It returns version information of the wc command.

Practical Examples of the wc Command

In this section, we will explore some basic to advanced use cases of the Linux wc command, along with practical examples. 

Example 1: Using wc Command on a File

Run the wc command without any argument to display the number of lines, words, and bytes from the file:

wc exampleFile.txt

In the output, 4 shows the number of lines, 18 represents the total words, 129 shows byte count, and exampleFile.txt is the file name:

using wc command on a file

Example 2: Using wc Command on Multiple Files

You can specify the multiple files with space-separated syntax to show the count information of multiple files:

wc exampleFile.txt employeeNames.txt

The output displays the number of lines, words, and bytes, along with the names of both files:

using wc command on multiple files

Example 3: Using wc Command to Count Lines

You can run the wc command with the “-l” option to count only the number of total lines of the specified file:

wc -l exampleFile.txt

In the output screenshot, 4 represents the total lines and file name:

using wc command to count lines

Example 4: Using wc Command to Count Characters

Use the -m flag with the wc command to count only characters of the specified file:

wc -m exampleFile.txt

The output displays the total number of characters, followed by the file name:

using wc Command to Count Characters

Similarly, you can use the -c or -w option to count bytes or words in a file.

Example 5: Using wc Command to Count Total Files and Directories

The ls command returns all available files and folders within the current working directory. To count the total number of files and directories, including hidden ones, you can run the following command:

ls | wc -l
using wc command to count total files and directories

This command returns the total files and directories of the current working directory. To count the files and subdirectories in a specific directory, use the following command:

ls dirName | wc -l

Change dirName to the name of the directory you want to count to see the total number of files and folders in it.

Conclusion

The wc is a Linux command that lets us count lines, words, bytes, and characters in files. It supports various options to customize the output and can be used with other commands for more complex tasks. Users can effectively analyze text files and directory contents by understanding the syntax and practical applications of the wc command. In this article, we discussed Linux wc command usage through practical examples.

The wc command allows you to count lines, words, and bytes in files, making it a valuable tool for your tasks. To enhance your experience and boost performance, consider Ultahost’s fast VPS hosting. With a range of affordable VPS plans, you can easily find one that fits your needs while practicing the wc command efficiently.

FAQ

What is wc used for?
Can we count only the number of lines in a file using wc?
How to use wc command in Linux bash?
How can I count the number of files and directories in the current directory?
Can I use the wc command with multiple files?
How to count the contents of a specific directory?
What options does the wc command support?

Related Post

lsof Command in Linux with Examples

lsof command stands for "list open files" and it is inc...

Creating and Managing Files in Linux

Linux is a family of open-source Unix-like operating sy...

How to create and remove symbolic links in Li

When it comes to creating and managing a file in Linux,...

How to Use the ulimit Linux Command

ulimit stands for "user limits" and is used to set or d...

How to Install Maldet on Linux Server

Linux Malware Detect shorts for Maldet is a powerful ma...

How to Find a File with Find Command in Linux

The find command in Linux is a powerful and versatile t...

Leave a Comment