The “wc” or the word count command in Bash is considered extremely useful as it helps in finding out various statistics of a file. This command can be used in multiple different variations. However, in this guide, we’re going to learn the basic usage of this command in Bash.
Method of Using the “wc” Command in Bash in Linux Mint 20:
Before starting to use the “wc” command in Bash, we would first like to show you the version of this command that is installed on our Linux Mint 20 system.
To check the version of the “wc” command installed on our system, we have shown the output of the above-mentioned command in the image below:
We also want to state here that for demonstrating the usage of the “wc” command in Bash in Linux Mint 20, we have created a random file named wc.txt. We have used the very same file in all the eight examples described below. You can see the contents of this file by running this command.
The content of this dummy file is shown in the following image:
After showing this, we are all set to explain to you some examples regarding the usage of the “wc” command in Bash in Linux Mint 20.
Example # 1: Displaying the Number of Lines, Words, Characters, and the Name of a File:
If you want to display the number of lines, words, characters, and the name of a file in Linux Mint 20, then you can run the “wc” command without any additional flags in this manner:
Here, you should replace the File with the name of the file whose line count, word count, character count, and name you want to be displayed on your terminal. We have replaced it with wc.txt, i.e., the name of our dummy file.
The output of the default “wc” command is shown below:
Here, the first column represents the line count; the second shows the word count, the third displays the character count, whereas the fourth states the name of the file.
Example # 2: Printing only the Number of Words and the Name of a File:
If you only want to print the total number of words in a file along with its name, then you can use the “wc” command with the “-w” flag.
Here, you should replace the File with the name of the file whose word count and name you want to be displayed on your terminal. We have replaced it with wc.txt, i.e., the name of our dummy file.
The modification of output of the “wc” command is shown below:
Example # 3: Printing only the Number of Lines and the Name of a File:
If you only want to print the total number of lines in a file along with its name, then you can use the“-l” flag along with the “wc” command.
Here, you should replace the File with the name of the file whose line count and name you want to be displayed on your terminal. We have replaced it with wc.txt, i.e., the name of our dummy file.
The modification of output of the “wc” command is shown below:
Example # 4: Printing only the Number of Characters and the Name of a File:
If you only want to print the total number of characters in a file along with its name, then you can use the “wc” command with the “-m” flag.
Here, you should replace File with the name of the file whose character count and name you want to be displayed on your terminal. We have replaced it with wc.txt, i.e., the name of our dummy file.
The modification of output of the “wc” command is shown below:
Example # 5: Printing only the Number of Bytes and the Name of a File:
If you only want to print the total number of bytes in a file along with its name, then you can use the “wc” command with the “-c” flag.
Here, you should replace the File with the name of the file whose byte count and name you want to be displayed on your terminal. We have replaced it with wc.txt, i.e., the name of our dummy file.
The modification of output of the “wc” command is shown below:
Example # 6: Printing only the Length of the Longest Line (number of characters in the longest line) and the Name of a File:
If you just want to display the longest line length and name of a file, then you can use the “wc” command with the “-L” flag.
Here, you should replace File with the name of the file whose longest line’s character count and name you want to be displayed on your terminal. We have replaced it with wc.txt, i.e., the name of our dummy file.
The modification of output of the “wc” command is shown below:
Example # 7: Printing the Total Number of Files and Folders in the Current Directory:
The “wc” command can also be combined with the “ls” command to print the total number of files and folders in a directory in the following manner:
The total number of files and folders in our current directory, as shown in the image below:
Example # 8: Displaying the Number of Lines, Words, Characters, and the Names of more than one file along with the Total Count:
If you want to display the line count, word count, character count, and names of more than one file, then you can run the “wc” command in the following manner:
Here, you should replace File1 and File2 with the exact names of the files whose statistics you want to be displayed on your terminal. We have replaced them with wc.txt and abc.txt, i.e., the names of our dummy files. You can even mention the names of more than two files.
The modification of output of the “wc” command is shown below:
Here, the last row represents the total line count, word count, and character count of both of our files.
Conclusion:
By going through this tutorial on the usage of the “wc” command in Bash, you can easily use this command to find all the necessary details about your files in Linux Mint 20.