×

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.


Related Topics

Difference Between Linux and Windows

Linux vs. Windows Here in this tutorial, we are going to discuss several features of Linux and Windows. These features or characteristics help us to come to a conclusion about which...

5 minutes read.

netstat Command in Linux/Unix with Examples

The "netstat" command in Linux is a command-line utility that displays network connections, routing tables, and various network statistics. It is used to display the status of network connections, both...

3 minutes read.

touch Command in Linux/Unix with Examples

The "touch" command in Linux (and other Unix-like operating systems) is used to create new empty files or update the timestamps of existing files. The touch command is a simple...

4 minutes read.

Linux Commands with Examples

Linux Commands with Examples All the Linux commands are executed on the terminal provided by the Linux system. This terminal is a command-line interface to implement and display command outputs. Linux...

10 minutes read.

telnet Command in Linux/Unix with Examples

The telnet command in Linux is a command-line utility that allows you to connect to a remote host using the Telnet protocol. Telnet is a network protocol that provides a...

3 minutes read.

awk Command in Linux/Unix with Examples

The 'awk' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

4 minutes read.

Linux Commands List

Linux Commands List The Linux systems are filled with commands for several use cases. We have divided these commands into some of the most common implementations for a better understanding of...

2 minutes read.

cd Command in Linux/Unix with Examples

The 'cd' command in Linux is used to change the current working directory. The command stands for "change directory" and it is used to navigate the file system on a...

3 minutes read.

chmod Command in Linux/Unix with Examples

The chmod command in Linux is a command-line utility that is used to change the permissions of files and directories. It stands for "change mode" and it is used to...

4 minutes read.

Linux apt-get command

Apt-get is a command-line that helps to handle the packages in Linux. Its major task is to assist in retrieving data and packages from trusted sources for purposes of installation,...

6 minutes read.

Rename Folder in Linux

Rename Folder in Linux In the Linux system, there is not any single dedicated command for renaming the files and directories. Mostly, the mv command is used extensively for the purpose...

3 minutes read.

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...

4 minutes read.

Top 20 Linux Commands

1) clear command clear is a standard operating system command which is used to clear the terminal screen. This command brings the command line on the top of the terminal. By pressing the...

10 minutes read.

cut Command in Linux/Unix with Examples

The "cut" command in Linux (and other Unix-like operating systems) is used to remove sections from each line of a file or stream of data. The cut command is often...

3 minutes read.

wget Command in Linux/Unix with Examples

The wget command is a command-line utility in Linux that is used to download files from the internet. It supports various protocols including HTTP, HTTPS, and FTP and can be...

5 minutes read.

rename Command in Linux/Unix with Examples

The rename command in Linux is used to rename multiple files at once, using a specified pattern. The command is also known as rename, ren, and mv. This command is...

3 minutes read.

How to change the ApacheHTTP port in Linux

What is an Apache HTTP server in Linux? When we talk about the most commonly used web servers, one of the web servers that cannot be missed is Apache. With the...

4 minutes read.

Linux cd

Linux cd: As in GUI operating systems like Windows 10, we use the mouse to travel to various folders back and forth; similarly, we have the “cd” command in Linux...

4 minutes read.

find Command in Linux/Unix with Examples

The find command in Linux is a command-line utility that is used to search for files and directories in a file system. It is a versatile command that can be...

5 minutes read.

ls Command in Linux/Unix with Examples

The ls command in Linux is a command-line utility that is used to list the files and directories in a directory. It is one of the most basic and commonly...

4 minutes read.