wc Command in Linux

wc Command in Linux

The wc command in Linux is a powerful utility used to count the number of lines, words, and characters in one or more text files.

It’s an indispensable tool for tasks like analyzing document length, counting code lines, or assessing file size.

By following the wc command options examples of this tutorial, you will provide the file paths as arguments, quickly obtain detailed statistics about their content.

wc Command Syntax:

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

The output counts the number of lines, words, and characters in the considered [File].

Here’s a breakdown of the syntax:

  • wc: This is the command name.
  • [OPTION]: This indicates that you can specify zero or more options.
  • [FILE]: This indicates that you can specify zero or more files.

Prerequisites to Use wc Command in Linux

Provide the options below to let this tutorial work correctly and move on.

  • A Linux VPS running a compatible Linux distribution (e.g., Ubuntu, Debian, CentOS).
  • A non-root user with sudo privileges.
  • Access to Terminal.

17 Practical Examples of Linux wc Command

Once you purchased your Linux VPS, you would need to learn how to work with Linux commands.

The wc command is pre-installed on most Linux distributions by default. You can find it in /usr/bin/wc.

Let’s go through the examples in this guide to learn How to use wc command in Linux:

1. Counting Lines

The -l option in the wc command is used specifically to count the number of lines within a file or multiple files.

Tip: It provides a quick and efficient way to determine the total number of lines in a text document.

Syntax:

wc -l [FILE]...

Example:

To count the lines in a file named my_document.txt, run the command below:

wc -l my_document.txt

This command will output the total number of lines present in the my_document.txt file.

2. Counting Lines in Multiple Files

When you use the wc command with multiple files, it provides the line count, word count, and character count for each file individually.

Tip: This is especially useful when you need to compare the statistics of different files or analyze the content of multiple documents.

Example:

Use the following command when you have three files named file1.txt, file2.txt, and file3.txt, and you want to count the lines, words, and characters in each file separately:

wc file1.txt file2.txt file3.txt

The output would typically look like this:

10  50 300 file1.txt
 20  80 500 file2.txt
 30 120 700 file3.txt

3. Counting Words

The -w option in the wc command is specifically designed to count the number of words within a file or multiple files.

Tip: It provides a convenient way to determine the total word count in a text document.

Syntax:

wc -w [FILE]...

Example:

To count the words in a file named my_article.txt, run the command below:

wc -w my_article.txt

This command will output the total number of words present in the my_article.txt file.

Note:

When you want to count the words in multiple files, you can list them after the -w option, separated by spaces:

wc -w document1.txt document2.txt document3.txt

This will display the word count for each file individually.

4. Counting Characters

The -c option in the wc command is used to count the number of characters within a file or multiple files.

Tip: This includes all characters, including spaces, punctuation, and special characters.

Syntax:

wc -c [FILE]...

Example:

To count the characters in a file named my_script.sh, run the following command:

wc -c my_script.sh

This command will output the total number of characters present in the my_script.sh file.

5. Counting Multibyte Characters

The -m option in the wc command is used to count the number of multibyte characters within a file.

Tip: This is particularly useful when dealing with text encoded in character sets that use more than one byte to represent a single character, such as UTF-8.

Syntax:

wc -m [FILE]...

Example:

Use the command below to count the multibyte characters in a file named japanese_text.txt.

wc -m japanese_text.txt

This command will output the total number of multibyte characters present in the japanese_text.txt file.

6. Counting the Longest Line

The -L option in the wc command is used to determine the length of the longest line within a file or multiple files.

Tip: It provides a way to identify the maximum number of characters present in any single line of a text document.

The -L option is particularly useful when analyzing text files for formatting or layout purposes, as it can help identify lines that might be exceeding certain length limits or causing formatting issues

Syntax:

wc -L [FILE]...

Example:

To find the length of the longest line in a file named long_lines.txt, run the command below:

wc -L long_lines.txt

This command will output the number of characters in the longest line of the long_lines.txt file.

7. Counting Lines, Words, and Characters for Multiple Files

When using the -L option with multiple files, the wc command will determine the length of the longest line among all the specified files.

Tip: This is particularly useful when you want to find the maximum line length across a group of documents.

Syntax:

wc -L file1.txt file2.txt file3.txt ...
  • -L: This option tells wc to find the longest line.
  • file1.txt file2.txt file3.txt …: This is a list of the files you want to analyze.

The output will be a single number representing the length of the longest line found in any of the specified files.

For Example:

If you have three files: file1.txt, file2.txt, and file3.txt, and file2.txt has the longest line, the output might look like the below:

80

Which indicates that the longest line is 80 characters long.

8. Combine wc with cat Command

The cat command and wc commands can be effectively combined to perform various text processing tasks.

To combine multiple files and then count specific elements (lines, words, or characters) in the combined output, use these two commands as below:

Syntax:

cat file1.txt file2.txt | wc -[option]

Example:

To concatenate the contents of file1.txt and file2.txt and then count the total number of lines in the combined output, run:

cat file1.txt file2.txt | wc -l

By combining cat and wc, you can perform various text processing tasks efficiently, such as analyzing multiple files, counting elements in standard input, and more.

Also, you can run the command below to read text from standard input (e.g., piped from another command) and count elements:

echo "This is Opervps test" | wc -w

This command echoes the string “This is OperaVPS test” to standard input, which is then piped to wc -w to count the number of words.

9. Combine wc with find Command

To find files matching a specified pattern and then count elements (lines, words, or characters) in those files, you can combine wc with find command.

Syntax:

find . -type f -name "*.txt" | wc -[option]

Example:

You can use the command below to find all regular files with a .txt extension in the current directory and its subdirectories, then count the total number of lines in those files.

find . -type f -name "*.txt" | wc -l

10. Combine wc with ls Command

Combining ls command and wc causes listing files in a directory and then counting elements (lines, words, or characters) in those files.

Syntax:

ls | wc -[option]

Example:

ls | wc -l

This command lists all files in the current directory and then counts the total number of lines in those files.

11. Combine wc with grep Command

The grep command and wc commands can be effectively combined to perform various text analysis tasks.

Combining grep and wc helps to filter lines in a file based on a pattern and then count elements (lines, words, or characters) in the filtered results.

Syntax:

grep "pattern" your_file.txt | wc -[option]

Example:

grep "error" your_log.txt | wc -l

This command filters lines in your_log.txt that contain the word “error” and then counts the total number of lines that match the pattern.

12. Combine wc with sort Command

Combining sort and wc lets Linux users sort lines in a file and then count elements (lines, words, or characters) in the sorted output.

Syntax:

sort your_file.txt | wc -[option]

Example:

sort your_file.txt | wc -l

This command sorts the lines in your_file.txt and then counts the total number of lines in the sorted output.

You can also use this combination to count the number of unique occurrences of lines in a file:

Syntax:

sort your_file.txt | uniq | wc -l

Example:

sort your_file.txt | uniq | wc -l

This command sorts the lines in your_file.txt, removes duplicate lines using uniq, and then counts the number of remaining lines, which represents the number of unique occurrences.

13. Combine wc with tail Command

The wc and tail command can be effectively combined to analyze the tail end of a file, which is often useful for examining recent log entries or tracking changes in large files.

Syntax:

  • tail -n [number of lines] your_file.txt | wc -[option]
  • tail -n [number of lines]: This part extracts the last [number of lines] from the specified file your_file.txt.
  • |: The pipe symbol (|) sends the output of tail to wc.
  • wc -[option]: This part applies the desired wc option to count lines, words, or characters in the extracted tail section.

Example:

Use the command below to count the number of lines in the last 100 lines of a file named access.log.

tail -n 100 access.log | wc -l

This command will extract the last 100 lines of access.log and then count the number of lines in that extracted portion.

14. Combine wc with head Command

The wc and head commands can be effectively combined to analyze the beginning of a file, which is often useful for examining initial data or checking for specific patterns.

Syntax:

  • head -n [number of lines] your_file.txt | wc -[option]
  • head -n [number of lines]: This part extracts the first [number of lines] from the specified file your_file.txt.
  • |: The pipe symbol (|) sends the output of head to wc.
  • wc -[option]: This part applies the desired wc option to count lines, words, or characters in the extracted head section.

Example:

To count the number of lines in the first 100 lines of a file named access.log, run:

head -n 100 access.log | wc -l

This command will extract the first 100 lines of access.log and then count the number of lines in that extracted portion.

15. Displaying Version Information

The -version option in the wc command is used to display the version number and copyright information of the wc utility.

Tip: This is helpful for determining the specific version of wc that is installed on your system and for checking for updates or compatibility issues

Syntax:

wc --version

Example:

wc --version

This command will typically output something like:

wc (GNU coreutils) 8.32
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free   
 software; you are free to change and redistribute it.
There is no warranty, to the extent permitted by law.

16. Comparing Statistics for Multiple Files

To display statistics for a list of files using the wc command, you can simply provide the file paths as arguments.

The command will then output the line count, word count, and character count for each file.

Syntax:

wc [file1.txt] [file2.txt] ... [fileN.txt]

Example:

wc file1.txt file2.txt file3.txt

This will output the total number of lines in the file, the total number of words in the file, and the total number of characters in the file.

The output format would be like this:

10  50 300 file1.txt
20  80 500 file2.txt
30 120 700 file3.txt

In this way, wc command in Linux helps in comparing the statistics of different files and getting a comprehensive overview of their content.

17. Comparing File Size

While wc is primarily used to count lines, words, and characters, it can also be used indirectly to compare file sizes.

You can use wc -c to count the number of characters in each file. The character count is directly related to the file size, especially for text-based files.

Example:

wc -c file1.txt file2.txt

This will output the number of characters in each file. You can compare these numbers to determine which file is larger.

wc Command Options

OptionDescriptionExample
-lCount the number of lines.wc -l file.txt
-wCount the number of words.wc -w file.txt
-cCount the number of characters.wc -c file.txt
-mCount the number of multibyte characters.wc -m file.txt
-LCount the length of the longest line.wc -L file.txt
-versionDisplay version information.wc --version

wc Combinations with Other Commands

CombinationExample
grepFilter lines based on a pattern and count elements.
sortSort lines and count elements.
headCount elements in the first few lines.
tailCount elements in the last few lines.
findFind files and count elements in them.
lsList files and count elements.
catConcatenate files and count elements.

How to get the Number without the Filename in wc Command Output?

To get the number without the filename in wc command in Linux, you can:

  • -w option to count words
  • -l to count lines
  • -c to count characters

and then pipe the output through cut command to extract the desired number.

Counting words

To output only the number of words in the file:

wc -w your_file.txt | cut -d " " -f 1

Counting lines

To output only the number of lines in the file:

wc -l your_file.txt | cut -d " " -f 1

Counting characters

To output only the number of characters in the file:

wc -c your_file.txt | cut -d " " -f 1

Conclusion

The explained examples of this tutorial help in understanding count lines, words, characters, and bytes in a file Linux and also finding the longest line of a Linux file.

Also, you learned about wc command for multiple files and how to combine it with other Linux commands.

Leave a Reply

Your email address will not be published. Required fields are marked.