Using the wc Command in Linux
The wc command in Ubuntu stands for “word count”. It is a simple command-line utility used to display the number of lines, words, and bytes (or characters) in files. The basic syntax of the wc command is:
wc [options] [file...]
Here are the common options you can use with the wc command:
- wc without any options: Displays the number of lines, words, and bytes in each specified file, and a total line if more than one file is specified.
wc file.txt
- -l: Prints the number of lines in the file.
wc -l file.txt
- -w: Prints the number of words in the file.
wc -w file.txt
- -c: Prints the number of bytes in the file.
wc -c file.txt
- -m: Prints the number of characters in the file. This option takes into account multi-byte characters, whereas -c counts bytes.
wc -m file.txt
- -L: Prints the length of the longest line in the file.
wc -L file.txt
Examples
- Count only lines in a single file:
wc -l file.txt
- Count lines, words, and bytes in multiple files:
wc file1.txt file2.txt
Combining Options
You can combine different options to get specific counts in the same command:
# This command will output the number of lines and words in file.txt
wc -lw file.txt
Practical Use Cases
- Count lines in log files:
wc -l /var/log/syslog
- Count the number of files in a directory:
ls -1 | wc -l
- Count words in a text file:
wc -w document.txt