×

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

sudo Command in Linux/Unix with Examples

The sudo command in Linux is used to run a command or set of commands with the privileges of another user, typically the root user. This allows users to perform...

3 minutes read.

Linux Admin Tutorial

In publishing and graphic design, lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document without relying on meaningful content....

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.

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.

grep Command in Linux/Unix with Examples

The grep command in Linux is a command-line utility that searches for patterns in text files. It stands for "global regular expression print" and it is used to search for...

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

ps Command in Linux/Unix with Examples

The 'ps' command in Linux is used to display information about the running processes on a system. The command stands for "process status" and it allows you to view details...

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

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.

Difference Between Unix and Linux

Unix vs Linux Unix and Linux both have been of great use in changing the world of operating systems. Unix was among the very few operating systems which were programmed for...

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.

addgroup Command in Linux with Examples

What is the addgroup command? It is a command in Linux to create a new group using specified command lined and the system’s default values. How to add groups Adding a user group:...

4 minutes read.

Linux pwd

The pwd command is used to print or display the name of the working or current directory. Some shells have their own version of the pwd with several features, in...

1 minute read.

pwd Command in Linux/Unix with Examples

The "pwd" command in Linux (and other Unix-like operating systems) stands for "print working directory." When executed, it simply displays the current directory (or folder) that the user is currently...

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

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.

Linux SCP Command

SCP stands for secure copy protocol. It is a command-line application for copying files and directories safely between two places. Command-line utilities are programs that operate on a computer's command...

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.

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.