How to Use the “wc” Command in Bash?

The “wc” or the word count command in Bash is considered extremely useful as it helps in finding out various statistics of a file. In this post, we’re going to learn the basic usage of this command in Bash.

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, then you can run the “wc” command without any additional flags in this manner

$ wc File

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.

$ wc -w File

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.

$ wc -l File

On the another hand, “-L” flag prints only the length of the longest Line (number of characters in the longest line).

$ wc -L File

Printing only the Number of Characters and the Name of a File

With option “-m” the “wc” command will print the total number of characters in a file along with its name.

$ wc -m File

Printing only the Number of Bytes and the Name of a File

To print only the number of bytes and the name of a file, you can use “-c” flag.

$ wc -c File

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.

$ ls | wc -l

By going through this post on the usage of the “wc” command in Bash, you can easily use this command to find all the necessary details about your files.
Thank you for your reading!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s