Guide to the Linux wc Command


Introduction

The wc command, which is short for "word count", is a simple yet powerful tool that allows you to quickly and easily count the number of lines, words, and characters in a file. But the wc command is capable of much more than counting words. In this guide, we'll explore all the features and options available when using the wc command, so you can take full advantage of its capabilities. So, grab a cup of coffee and dive into the world of the Linux wc command.

Basic use of the wc command

The basic syntax of the wc command is as follows −

$ wc file_name

This command will give you the count of lines, words and characters in the specified file. For example, running the command below will give you output like this −

$ wc file.txt
13 45 267 file.txt

The first number is the line count, the second is the word count, and the third is the character count in the file. The last is the file name.

Counting Lines, Words, and Characters

The wc command can also be used to separately count the number of lines, words and characters in a file. You can use the options -l, -w and -c respectively to do this. For example, if you want to know the number of lines in a file, you can use the wc -l file.txt command. The output will be −

$ wc -l file.txt
13 file.txt

Similarly, you can use “wc -w file.txt” to find out the number of words in the file and “wc -c file.txt” to find out the number of characters in the file.

Handling Multiple Files

The wc command can also be used to manage multiple files at once. You can specify file names separated by spaces. For example, if you want to know the number of lines, words, and characters in two files file1.txt and file2.txt, you can use the command “wc file1.txt file2.txt”. The output will be −

$ wc file1.txt file2.txt 
13 45 267 file1.txt
10 55 280 file2.txt

And if you want to know only the number of lines in both files, you can use the “wc -l file1.txt file2.txt” command.

Handling Input from a Pipe

The wc command can also be used with pipe input. For example, if you want to know the number of lines, words, and characters in file.txt but don't want to open the file, you can use the command “cat file.txt | wc”. The output will be −

$ cat file.txt | wc
13 45 267

And if you just want to know the number of lines in the file, you can use the command “cat file.txt | wc-l”. The output will be 13.

Advanced usage and options

The wc command has a few more options for advanced use. For example, the -m option is used to count the number of characters in a file. Running the wc -m file.txt command will give you the number of characters in the file. Another useful option is the -L option, which indicates the length of the longest line in a file. For example, running “wc -L file.txt” will give you the length of the longest line in the file.

The -w option can be used to count the number of words in a file. For example, running wc -w file.txt will give you the number of words in the file. Another useful option is the ‘-x’ option, which indicates the number of bytes in a file. For example, running “wc -x file.txt” will give you the number of bytes in the file. Finally, the ‘-b’ option indicates the number of blocks in a file. For example, running “wc -b file.txt” will give you the number of blocks in the file.

Conclusion

In this guide, we've covered the basics and advanced usage of the Linux wc command. With the help of this command, you can quickly and easily count the number of lines, words and characters in a file or even multiple files. We also discussed some advanced options and using the wc command.

Updated on: 13-Feb-2023

383 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements