wc command in Linux with Examples

wc command in Linux with Examples

wc command in Unix/Linux operating systems is used to find out number of line count, word count, byte and characters count in a files specified by the file arguments. This tutorial explains the wc command in detail.

Syntax

The syntax for wc command with its options are

wc [options] filenames

Usage of the commands

  • wc -l : Prints the number of lines in a file.
  • wc -w : prints the number of words in a file.
  • wc -c : Displays the count of bytes in a file.
  • wc -m : prints the count of characters from a file.
  • wc -L : prints only the length of the longest line in a file.

Let us start with textfile.odt file you can use text files,pdf,..etc.

wc Command without any options

Run the following command to display the basic result of textfile.odt file.

[root@linuxhelp Desktop]# wc textfile.odt 
48   270 12286 textfile.odt

In the output 48 is no of lines, 270 is no of words, 12286 is no of bytes.

Using wc command with &ndash l options

The below command is used to Count only Number of Lines in the file.

[root@linuxhelp Desktop]# wc -l textfile.odt 
48 textfile.odt

Using wc command with &ndash w options

The below command helps to Count only Number of words in the file.

[root@linuxhelp Desktop]# wc -w textfile.odt 
270 textfile.odt

To Count Number of Bytes and Characters

The below command is used to Count only Number of Bytes in the file.

[root@linuxhelp Desktop]# wc -c textfile.odt 
12286 textfile.odt

The below command Count only Number of characters in the file.

[root@linuxhelp Desktop]# wc -m textfile.odt 
8065 textfile.odt

Using wc command with &ndash L options

The below command is used to print out the length of longest line in a file.

[root@linuxhelp Desktop]# wc -L textfile.odt 
999 textfile.odt

To Check the version wc command

Using the following command, you can check the version of wc command.

[root@linuxhelp Desktop]# wc --version
wc (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later < http://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.
Written by Paul Rubin and David MacKenzie.
Tag : WC command
FAQ
Q
How to fetch the wrod counts in certain file using the "wc" command?
A
You can use the option of "-w" with "wc" command to fetch the wrod counts in certain file using the "wc" command. For syntax: "wc -w file.txt".
Q
How to find byte counts for any file using the "wc" count?
A
You can use the option of '-c" with "wc" command to find byte counts for any file using the "wc" count. For syntax: "wc -c fine.txt"
Q
How to find the new lines count in any file using the "wc" command?
A
You can use the option of "-l" with "wc" command to find the new lines count in any file using the "wc" command. For syntax: "wc -l file.txt".
Q
How can I check the version of "wc" command in Linux?
A
You can run the following command to check the version of "wc" command in Linux. For Syntax: "wc -V".
Q
How to find the length of the longest line in any file using the "wc" command?
A
Use the option of "-L" with "wc" command to find the length of longest line in any file using the "wc" command. For syntax: "wc -L file.txt".