5 Linux wc command examples

By | 28/09/2012

The Linux wc command is a useful command that can come in handy if you ever want to know details like newline counts, byte counts, character counts etc for a file which is supplied as input. In this article, we will discuss 5 basic examples of this command.

Before jumping on to the examples, here is the syntax of this command :

NAME
wc – print newline, word, and byte counts for each file

SYNOPSIS
wc [OPTION]… [FILE]…
wc [OPTION]… –files0-from=F

DESCRIPTION
Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified. With no FILE, or when FILE is -, read standard input. A word is a non-zero-length sequence of characters delimited by white space. The options below may be used to select which counts are printed, always in the following order: newline, word, character, byte, maximum line length.

Linux wc command examples

1. A basic example

In its very basic form, the wc command can be run with a text file name as input.

For example :

$ wc testfile1
 6 12 49 testfile1

So we see that the wc command was run with testfile1 as input and three integers were produced in output. These three numbers correspond to number of lines, number of words and number of bytes. So, wc says that testfile1 contains 6 lines, 12 words and 49 bytes.

2. Print byte count with -c flag

The -c flag can be used with wc command to display only the number of bytes in the file.

For example :

$ wc -c testfile1
49 testfile1

So we see that wc command produced an output of 49. This means that there are 49 bytes in this file.

3. Print newline count with -l flag

The -l flag can be used with wc command to print the number of newline characters in the input file.

For example :

$ wc -l testfile1
6 testfile1

So we see that there are 6 newlines in the input file testfile1.

4. Print the length of longest line using -L flag

The -L flag can be used with wc command to find out the length of the longest line in the input file.

For example :

$ wc -L testfile1
11 testfile1

So we see that longest line has length of 11.

5. Print word count using -w flag

The -w flag can be used to print total number of words in the input file.

For example :

$ wc -w testfile1
12 testfile1

So we see that total number of words as shown by wc in output is 12.

Leave a Reply

Your email address will not be published. Required fields are marked *