Thanks to visit codestin.com
Credit goes to www.geeksforgeeks.org

Open In App

wc command in Linux with examples

Last Updated : 03 Nov, 2025
Comments
Improve
Suggest changes
37 Likes
Like
Report

The wc command in Linux is used to count lines, words, characters, and bytes in a file or from input you provide. Whether you're analyzing logs, reading large datasets, or just checking the size of your scripts, wc gives a quick summary. It can take one or more files as input or read directly from user input if no file is mentioned.

  • Line count – number of lines (each line ends with a newline character).
  • Word count – total number of words (words are separated by spaces, tabs, or newlines).
  • Byte count – total number of bytes (which may differ from characters in some cases).
  • Filename – name of the file that was processed.

Let us consider two files having name state.txt and capital.txt containing 5 names of the Indian states and capitals respectively.

$ cat state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh

$ cat capital.txt
Hyderabad
Itanagar
Dispur
Patna
Raipur

Example 1: Passing only one file name in the argument.

Command:

$ wc state.txt

Output

5  7 58 state.txt

here,

  • 5: Number of Lines
  • 7: Number of Words
  • 58: Number of bytes

Similarly,

Command:

$ wc capital.txt

Output:

5  5 39 capital.txt

Example 2: Passing more than one file name in the argument.

$ wc state.txt capital.txt
5 7 58 state.txt
5 5 39 capital.txt
10 12 97 total

Note : When more than file name is specified in argument then command will display four-columnar output for all individual files plus one extra row displaying total number of lines, words and characters of all the files specified in argument, followed by keyword total.

Options in wc Command

Here are some commonly used options in wc command in linux:

Syntax of wc command

wc [OPTION]... [FILE]...

If no file is specified, it will read from standard input, meaning you can type text manually or pipe it from another command.

1. Count the Number of Lines using `wc -l`

Displays the number of lines in a file.

Command:

$ wc -l state.txt

Output:

5 state.txt
  • With more than one file name

Command:

$ wc -l state.txt capital.txt

Output:

5 state.txt 
5 capital.txt
10 total

2. Count the Number of Words using `wc -w`

This option prints the number of words present in a file. With this option wc command displays two-columnar output, 1st column shows number of words present in a file and 2nd is the file name.

  • With one file name

Command:

$ wc -w state.txt

Output:

7 state.txt
  • With more than one file name.

Command:

$ wc -w state.txt capital.txt

Output:

7 state.txt
5 capital.txt
12 total

3. Count the Number of Bytes using `wc -c`

This option displays count of bytes present in a file. With this option it display two-columnar output, 1st column shows number of bytes present in a file and 2nd is the file name.

  • With one file name

Command:

$ wc -c state.txt

Output:

58 state.txt
  • With more than one file name

Command:

$ wc -c state.txt capital.txt

Output:

58 state.txt
39 capital.txt
97 total

4. Count the Number of Characters using `wc -m`

Using -m option 'wc' command displays count of characters from a file.

  • With one file name

Command:

$ wc -m state.txt

Output:

56 state.txt
  • With more than one file name

Command:

$ wc -m state.txt capital.txt

Output:

58 state.txt
39 capital.txt
97 total

5. Print the Length of the Longest Line using `wc -L`

  • The -L option in the wc command is used to display the length of the longest line (in terms of characters) in a file.
  • It helps identify which line in the file contains the maximum number of characters.
  • For example, in the file state.txt, the line “Arunachal Pradesh” has the longest length (17 characters).
  • Similarly, in capital.txt, the longest line is “Hyderabad” (10 characters).
  • When multiple files are provided as input, the last row (summary row) does not show a total — instead, it displays the maximum length among all files.

Command:

$ wc -L state.txt capital.txt

Output:

17 state.txt
10 capital.txt
17 total

Note: A character is the smallest unit of information that includes space, tab and newline.

6. Check version number using `wc --version`

This option is used to display the version of wc which is currently running on your system.

Command:

$ wc --version

Output:

wc (GNU coreutils) 8.26
Packaged by Cygwin (8.26-1)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
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.

Applications of wc Command

Here are some applications of wc command

1. To count all files and folders present in directory:

As we all know ls command in unix is used to display all the files and folders present in the directory, when it is piped with wc command with -l option it display count of all files and folders present in current directory.

$ ls gfg
a.txt
b.txt
c.txt
d.txt
e.txt
geeksforgeeks
India

$ ls gfg | wc -l
7

2. Display number of word count only of a file:

We all know that this can be done with wc command having -w option, wc -w file_name, but this command shows two-columnar output one is count of words and other is file name.

$ wc -w  state.txt
7 state.txt

So to display 1st column only, pipe(|) output of wc -w command to cut command with -c option. Or use input redirection(<).

$ wc -w  state.txt | cut -c1
7
OR
$ wc -w < state.txt
7

Explore