Linux wc Command - File Count Options

The linux wc utility in Linux counts the number of bytes, characters, words, and lines in a file or in standard input. This is a small but very useful utility provided among various utilities in Linux. This article explores wc command.

Wc commamd

By default, wc takes its input from STDIN. The default output counts number of bytes, number of words and number of lines.

$ wc
hello
world
2 2 12

To provide input from STDIN, we just invoke wc command with no option or argument. When we are done inputing, we enter ctrl+d to provide end of file mark. This output says that the input has 2 lines, two words and 12 bytes. Now if your counting of characters is different from the output, then remember to include hidden characters like newline character. Lets see another example:

$ wc
Hello all
This is demonstration of wc command.
2 8 47

Character and byte count

To print no of bytes of a file, -c or --bytes option is used.

$ cat wcfile.txt
Hello world. First Line.
Second
Another line

$ wc -c wcfile.txt
45 wcfile.txt

$ wc --bytes wcfile.txt
45 wcfile.txt

For number of characters, -m or --chars options are used.

$ wc -m wcfile.txt
45 wcfile.txt

$ wc --chars wcfile.txt
45 wcfile.txt

Word count

If we wish to print just the number of words in a the input, we use -w option.

$ wc -w wcfile.txt
7 wcfile.txt

$ wc --words wcfile.txt
7 wcfile.txt

The --words option does the same.

Line count

The number of lines in a file are printed by -l option. This can also be achieved with --lines option.

$ wc -l wcfile.txt
3 wcfile.txt

$ wc --lines wcfile.txt
3 wcfile.txt

The length of longest line is printed by -L or --max-line-length option.

$ wc -L wcfile.txt
24 wcfile.txt

$ wc --max-line-length wcfile.txt
24 wcfile.txt

In this case, the first line is the longest line having 24 characters.

File count in directory

Below commands will find the total file count in a directory in Linux. Find might be bit faster if you want to count huge number of files. ls -f (this will not sort) will be more faster than normal ls.

$ ls -l | wc -l

or

$ find ./ -type f | wc -l

Category: LINUX COMMANDS, LINUX HOWTO

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
offtopic ? Comment gets deleted.
All comments are subject to moderation.