6 Wc Command to Count Number of Lines, Words, and Characters in File

wc (short for word count) is a command line tool in Unix/Linux operating systems, which is used to find out the number of newline count, word count, byte and character count in the files specified by the File arguments to the standard output and hold a total count for all named files.

When you define the File parameter, the wc command prints the file names as well as the requested counts. If you do not define a file name for the File parameter, it prints only the total count to the standard output.

In this article, we will discuss how to use the wc command to calculate a file’s newlines, words, characters, or byte count with practical examples.

wc Command Syntax

The syntax of the wc command is shown below.

# wc [options] filenames

The followings are the options and usage provided by the wc command.

  • 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’s see how we can use the ‘wc‘ command with the few available arguments and examples in this article. We have used the ‘tecmint.txt‘ file for testing the commands.

Let’s find out the output of the tecmint.txt file using the cat command as shown below.

$ cat tecmint.txt

Red Hat
CentOS
AlmaLinux
Rocky Linux
Fedora
Debian
Scientific Linux
OpenSuse
Ubuntu
Xubuntu
Linux Mint
Deepin Linux
Slackware
Mandriva

1. A Basic Example of WC Command

The ‘wc‘ command without passing any parameter will display a basic result of the ‘tecmint.txt‘ file. The three numbers shown below are 12 (number of lines), 16 (number of words), and 112 (number of bytes) of the file.

$ wc tecmint.txt

12  16 112 tecmint.txt

2. Count Number of Lines in a File

Count the number of newlines in a file using the option ‘-l‘, which prints the number of lines from a given file. Say, the following command will display the count of newlines in a file.

In the output, the first field is assigned as count and the second field is the name of the file.

$ wc -l tecmint.txt

12 tecmint.txt

3. Count Number of Words in a File

The -w argument with the wc command prints the number of words in a file. Type the following command to count the words in a file.

$ wc -w tecmint.txt

16 tecmint.txt

4. Count Number of Characters in a File

When using option -m with the wc command will print the total number of characters in a file.

$ wc -m tecmint.txt

112 tecmint.txt

5. Count Number of Bytes in a File

When using option -c will print the number of bytes of a file.

$ wc -c tecmint.txt

112 tecmint.txt

6. Display Length of Longest Line in File

The ‘wc‘ command allows an argument ‘-L‘, it can be used to print out the length of the longest (number of characters) line in a file.

So, we have the longest character line (‘Scientific Linux‘) in a file.

$ wc -L tecmint.txt

16 tecmint.txt

7. Check wc Command Options

For more information and help on the wc command, simply run the ‘wc --help‘ or ‘man wc‘ from the command line.

$ wc --help
OR
$ man wc
wc Command Usage
Usage: wc [OPTION]... [FILE]...
  or:  wc [OPTION]... --files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified.  A word is a non-zero-length sequence of
characters delimited by white space.

With no FILE, or when FILE is -, read standard input.

The options below may be used to select which counts are printed, always in
the following order: newline, word, character, byte, maximum line length.
  -c, --bytes            print the byte counts
  -m, --chars            print the character counts
  -l, --lines            print the newline counts
      --files0-from=F    read input from the files specified by
                           NUL-terminated names in file F;
                           If F is - then read names from standard input
  -L, --max-line-length  print the maximum display width
  -w, --words            print the word counts
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/wc>
or available locally via: info '(coreutils) wc invocation'

In this article, you’ve learned about the wc command, which is a simple command-line utility to count the number of lines, words, characters, and byes in text files. There are lots of such other Linux commands, you should learn and master your command-line skills.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

26 Comments

Leave a Reply
    • @Thomas,

      To count occurrence of word in Linux text file, you should use following command.

      # cut -f 1 filename | grep -i "Yourword" | wc -l
      
      Reply
    • The previous cut | grep | wc still only counts lines, not word occurrences, and would consider “anthem,” “these,” etc matches for the substring “the.”

      This is a way to do what you want-

      # perl -lne '$c += () = /\bthe\b/g; END{ print $c }' YourFileGlob ListofFiles
      
      Reply
  1. I’ve been using wc -l ‘find *.csv -type f’ for some time, but I would like to reduce the count of each file by 1 to not include the header records. Is this possible?

    Reply
  2. Help,

    This all looks very straightforward if you know what you’re doing. Whereabouts do I type in the command wc -w to try and find out how many words are in a document, please?

    I’d greatly appreciate step by step from the beginning, pics would be good :)

    Reply
    • @Ezza,

      Here is the example command of how wc -c works.

      $ wc -w w_syst02.pdf 
      
      76522 w_syst02.pdf
      

      Here 76522 are number of words in a document w_syst02.pdf.

      Reply
      • Can you help me…this is the code I’ve made, but I want to count the lines of 01:19:21.

        How to write the code just to count the all 01:19:21 lines????

        11:01:11
        03:53:28
        01:19:21
        01:19:21
        01:19:21
        01:19:21
        01:19:21
        01:19:21
        01:19:21
        01:19:21

        Reply
        • The command is attempting to filter and count the lines containing the string “01:19:21” in a text file or a stream of text.

          $lines_of_text | grep 01:19:21
          
          Reply
        • @TTT

          If you want to count the occurrences of the line “01:19:21” in a text file using a Linux command, you can use the following command:

          grep -c "01:19:21" data.txt
          

          This will output the number of lines in the file that contain “01:19:21”.

          Reply
  3. Write a shell script to count the number of lines, characters, words in a file (without the use of commands). Also delete the occurrence of word “Linux” from the file wherever it appears and save the results in a new file.
    anybody plz help

    Reply
    • @Hasan,

      Your question is incomplete, could you explain your question in more detailed fashion, so that we can understand better and give you a proper solution..

      Reply
      • The tool for excelency is a computer (smartphone, tablet, etc) and need a software in special to work, to interactive with you like user. The best option to handle that is Linux. That is the reason.

        Reply
  4. When I do a wc -l in a directory then the output is something like –
    1 File1.txt
    2 File2.txt
    3 File3.txt
    6 Total

    I do not want to display the last Total. How can we do that? Any insights..!

    Reply
    • You can try this

      wc file*|grep -v total

      grep with -v option removes the lines which have the word “total” in them., so except last line all files with word count will shown. Hope it helps

      Reply
    • while read line; do echo “$line” | wc -m ;done < filename

      if your file contains '/' then use 'read -r' in the above command

      Another simple way without wc is

      awk '{print length($0) }' filename

      Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.