×

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

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.

cp Command in Linux/Unix with Examples

The cp (copy) command in Linux is a command-line utility that is used to copy files and directories. It is one of the most basic and commonly used commands in...

5 minutes read.

chown Command in Linux/Unix with Examples

The 'chown' command in Linux is used to change the ownership of a file or directory. The command stands for "change owner" and it allows you to change the user...

3 minutes read.

curl Command in Linux/Unix with Examples

The 'curl' command in Linux is a command-line tool that allows you to transfer data from or to a server using various protocols, including HTTP, HTTPS, FTP, and many more....

3 minutes read.

cat Command in Linux/Unix with Examples

The cat command in Linux is a command-line utility that is used to display the contents of a text file. It stands for "concatenate" and it is used to view...

4 minutes read.

mv Command in Linux/Unix with Examples

The 'mv' command in Linux is used to move or rename files and directories. The command stands for "move" and it allows you to move files and directories from one...

3 minutes read.

Linux mkdir | Linux Create Directory

The Linux mkdir command is very much similar to Windows’s "new folder" feature. This command is used in Linux os to create new directories. The command mkdir stands for "make...

1 minute read.

tar Command in Linux/Unix with Examples

The 'tar' command in Linux is used to create, maintain, and extract files from archive files called tarballs. The command stands for "tape archive" and it allows you to combine...

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

Linux Absolute and Relative Path

The concept of the absolute and relative path is very simple. The names “relative path” and “absolute path” give us little understanding of the concept we are learning here. The...

1 minute read.

gzip Command in Linux/Unix with Examples

The gzip command in Linux is used for compressing and decompressing files. It uses the Lempel-Ziv coding (LZ77) algorithm to compress files, which is known for its high compression ratio...

6 minutes read.

Linux Path Completion

Every operating system has some features which make them special. The Linux operating system primarily works on the command line interface, and we do have many features for this CLI....

1 minute read.

addr2line Command in Linux with Examples

Use of addr2line Command The addr2line command is used to convert addresses to line numbers and file names. This command uses debugging info to check which line number and file name...

4 minutes read.

du Command in Linux/Unix with Examples

The "du" command in Linux (and other Unix-like operating systems) is used to estimate the space used by a file or directory. The du command stands for "disk usage," and...

4 minutes read.

df Command in Linux/Unix with Examples

The 'df' command in Linux is used to display the amount of disk space available on the file system. The command reports the amount of disk space used and available...

3 minutes read.

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.

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.

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.

Linux ls

The primary use case of the ls command is to list the content available in the directory. The default directory is the current directory of the user. The below screenshot...

5 minutes read.

apt-get Command in Linux with Examples

The apt-get command is a command-line tool that is used to handle packages in Linux. This command handles the processes like retrieving the packages and information from authentic sources for...

5 minutes read.