wc Command in Linux/Unix with Examples

The wc command in Linux is a command-line utility that is used to count the number of lines, words, and characters in a text file or standard input. The name "wc" stands for "word count".

The basic syntax for the wc command is:

wc [options] [file]

1) To count the number of lines in a text file, you can use the following command:

wc -l file.txt

This command will display the number of lines in the file "file.txt".

2) To count the number of words in a text file, you can use the following command:

wc -w file.txt

This command will display the number of words in the file "file.txt"

3) To count the number of characters in a text file, you can use the following command:

wc -c file.txt

This command will display the number of characters in the file "file.txt"

4) To count the number of lines, words, and characters in a text file, you can use the following command:

wc file.txt

This command will display the number of lines, words, and characters in the file "file.txt" in that order.

5) To count the number of lines, words, and characters in multiple files, you can use the following command:

wc file1.txt file2.txt file3.txt

This command will display the number of lines, words, and characters in each of the specified files.

6) To count the number of lines, words, and characters in standard input, you can use the following command:

wc -

This command will read the standard input and display the number of lines, words, and characters in the input. This can be useful when working with piped input from other commands.

7) To suppress the display of the file name when using wc command on multiple files, you can use the following command:

wc -l -w -c file1.txt file2.txt file3.txt

This command will display only the number of lines, words, and characters in each of the specified files, without the file name.

8) To display only the total number of lines, words, and characters across multiple files, you can use the -L option. This option will display the maximum length of the lines in the specified files, rather than the count of lines, words and characters in each file.

wc -L file1.txt file2.txt file3.txt

9) To display the number of bytes instead of characters, you can use the -m option.

wc -m file1.txt

10) To display the number of newline characters instead of the number of lines, you can use the -l option. This can be useful when working with files that have a different line ending than the default (usually, newline characters on Linux and Carriage Return + newline characters on Windows).

wc -l file1.txt

11) To display the number of words, characters, and bytes in multiple files, in a tab-separated format, you can use the -T option.

wc -T file1.txt file2.txt file3.txt

12) To display the number of words, characters, and bytes in multiple files, in a tab-separated format, you can also use the --tabsize option. With this option, you can specify the number of spaces for each tab character.

wc --tabsize=4 file1.txt file2.txt file3.txt

13) To count the number of lines, words, and characters in multiple files in a directory, you can use the find command in combination with wc. The find command can be used to search for files in a directory, and the -exec option can be used to execute a command on each file found.

find /path/to/directory -type f -exec wc {} +

This command will search for all files in the directory "/path/to/directory" and execute the wc command on each file found, displaying the number of lines, words, and characters in each file.

14) To count the number of unique words in a text file, you can use the sort command in combination with uniq and wc.

cat file.txt | tr -s ' ' '\n' | sort | uniq -c | wc -w

This command will first use cat to display the contents of the file, then tr command will replace all spaces with newlines, the sort command will sort the words in the file, uniq -c command will count the unique words and wc -w will count the number of lines which contains the number of unique words.

15) To count the number of specific words in a text file, you can use the grep command in combination with wc.

grep -o "word" file.txt | wc -w

This command will first use the grep command to search for the word "word" in the file, the -o option will display only the matched word and then wc -w will count the number of lines which contains the matched word.

16) To count the number of specific characters in a text file, you can use the tr command in combination with wc.

tr -cd 'a' < file.txt | wc -c

This command will first use the tr command to count the number of occurrences of the character 'a' in the file, the -c option will complement the set of characters specified and the -d option will delete all characters that are not in the set. The wc -c will then count the number of characters remaining.

In conclusion, the wc command can be used in various ways to count different things in a text file.