How to Use the wc Command in Linux
Linux provides a vast number of command-line tools to help simplify your everyday tasks. One of these tools is the wc command.
wc is your go-to command when you need to know the number of words in a file or even how many files exist in a particular directory. But that's not all the wc command does. Read on to discover what the wc command is and how to use it effectively on Linux.
What Is the wc Command?
The wc command stands for "word count". It is a command-line tool used to count the number of words, lines, characters, and bytes in an output. It comes pre-installed in every Unix- and Linux-based operating system, so you do not need to install it manually.
The wc Command Syntax
To use wc, you need to specify a file or text output and the command options you want to use. The basic syntax of the wc command is:
wc [OPTION] [FILE]
There are many options available to use alongside the command, all of which we'll discuss later. To get command-line help regarding the wc command, check its manual page by running:
man wc
How to Use the wc Command
For this example, create a file: zen.txt. In this file, paste the following text:
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.[a]
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than right now.[b]
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea – let's do more of those!
This is the Zen of Python, and it is a set of 19 guiding principles written by Tim Peter to write simple, elegant, and concise Python codes.
If you use the cat command to create the file, leave a blank line before pasting the text.
Using the Default wc Command
By default, when you use the wc command with a file or output, it prints out the number of lines, words, and bytes present in the output.
Try it out with zen.txt by executing this command in your terminal:
wc zen.txt
The result:
19 137 824 zen.txt
You would see that it outputs four columns containing the number of lines, words, bytes, and the name of the file respectively.
Print the Number of Lines Present in a File
To count the number of lines present in a file or output, use the -l or --lines option. The syntax looks like this:
wc -l zen.txt
The result:
19 zen.txt
It shows that you have 19 lines in the file and also prints out the name of the text file.
Print the Number of Words in a File
To count the number of words in a file, you use the -w or --words option. Try it out:
wc -w zen.txt
The result:
137 zen.txt
Display the Number of Bytes
You can determine the exact number of bytes in a file by using the wc command alongside the -c or --bytes option. Execute this command to try it out:
wc -c zen.txt
The result:
824 zen.txt
Print the Number of Characters in a File
To print out the number of characters in a file, use the -m or --chars option. The syntax looks like this:
wc -m zen.txt
The result:
818 zen.txt
Print the Length of the Longest Line
In the event that you need to know the length of the longest line—the number of characters in that line—in a file, use the -L or the --max-line-length option with the wc command. It looks like this:
wc -L zen.txt
The result:
67 zen.txt
Using the wc Command With Multiple Files
You can use the wc command with more than one file or input. You will need to create two more files for this. The first file is letters.txt, which contains a list of the alphabet, while the second file is num.txt, containing a list of numbers from one to 10.
Alternatively, you can use any two text files. Let's try it out:
wc zen.txt letters.txt num.txt
The result:
19 137 824 zen.txt
26 26 52 letters.txt
10 10 21 num.txt
55 173 897 total
The first three rows contain the number of lines, words, and bytes of each file and the last row contains the total sum of each column.
Using the wc Command With Other Linux Commands
You can use wc with other commands through the pipe command. The pipe symbol redirects the output of one command as an input to another.
Count the Number of Files or Folders in a Directory
To do this you use the ls command to list the number of files in a directory and then pipe the input into the wc command. For example, to print the number of files on your Desktop, execute the following command:
ls Desktop | wc -l
Count the Number of Running Processes on Your System
Processes are tasks or programs your computer is working on or currently running. When you execute a command or open an application, it is registered as a process.
To count the number of processes, use the ps command with wc. Here, try it out:
ps | wc -l
Try Out Other Linux Commands With wc
There are a lot of commands available on Linux that have very unique functions and make the overall Linux experience seamless. You just need to know what they are and how to use them! Start your adventure today!
ncG1vNJzZmivp6x7rq3KnqysnZ%2Bbe6S7zGiunGWTpLqurc2dZKKmXaG2r8HXaA%3D%3D