Linux wc command with examples

Linux wc command with examples

In this article, we will see Linux wc command with examples.

wc stands for word count. It is terminal command used to find number of words, lines, bytes and characters count in the files specified in the file arguments. By default it displays four-columnar output.

First column shows number of lines present in a file specified, second column shows number of words present in the file, third column shows number of characters present in file and fourth column itself is the file name which are given as argument.

Syntax:

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

Options:

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.

Linux wc command examples

1. Basic Example of wc Command.

wc ls.txt

Linux wc command with examples

The ‘wc‘ command without passing any parameter will display a basic result of ls.txt file. The three numbers shown below are 243 (number of lines), 951 (number of words) and 8170 (number of bytes) of the file.

2. Count number of lines of a file in Linux.

We can find the number of lines of a given file using wc command with -l option.

wc -l ls.txt

3. Count number of words of a file in Linux.

We can find the number of words of a given file using wc command with -w option.

wc -w ls.txt

4. Count number of bytes of file in Linux.

We can find the number of bytes of a given file using wc command with -c option.

wc -c ls.txt

5. Count number of characters of file in Linux.

We can find the number of characters of a given file using wc command with -m option.

wc -m ls.txt

6. Find longest line of file in Linux.

We can find the longest line of file using -L option. It prints number of characters of longest line of a file.

wc -L ls.txt

7. Count number of lines, words and bytes of more than a file in Linux.

wc ls.txt addlogins.sh

8. Count no of files and folders of given directory in Linux.

ls /home/r2schools | wc -l ls /home | wc -l

9. To get help of wc command run either of following commands.

wc --help man wc