The wc command in Linux counts lines, words, characters, and bytes in files or standard input. It is most useful in shell pipelines where you need a quick count of output from another command.
The basic syntax is:
bashwc [OPTIONS] [FILE]...
Without options, wc prints three columns in a fixed order — line count, word count, and byte count — followed by the filename:
bashwc /proc/cpuinfo
448 3632 22226 /proc/cpuinfo
When multiple files are passed, wc shows a count for each file and a total line at the end:
bashwc /proc/cpuinfo /proc/meminfo
448 3632 22226 /proc/cpuinfo 49 143 1363 /proc/meminfo497 3775 23589 total
wc right-aligns numbers in columns when processing multiple files, which adds leading spaces. To get a clean number with no filename and no padding, redirect the file as stdin instead of passing it as an argument:
bashwc -l < /proc/cpuinfo
This form is easier to parse in scripts because the output is just the number.
Each option prints only the requested count:
-l — count lines (counts \n newline characters)-w — count words (any non-empty string delimited by whitespace)-c — count bytes-m — count characters (UTF-8 aware)-L — print the length of the longest linebashwc -l /etc/passwd # line count onlywc -w ~/Documents/notes.txt # word count onlywc -lL /proc/cpuinfo # line count and longest line length
-c and -m return the same value for ASCII text, because each character is one byte. For files with multibyte UTF-8 characters — accented letters, emoji, or non-Latin scripts — -c returns a higher number. Use -m when you need to count characters in international text.
wc -l only counts \n newline characters. A file with content on a single line and no trailing newline returns 0, which surprises most people the first time. Use grep -c '' as a workaround that counts all lines regardless of a trailing newline:
bashgrep -c '' file.txt
wc is commonly used to count the output of other commands:
bashfind . -type f | wc -l # count files in the current directorygrep "error" /var/log/syslog | wc -l # count matching log linesgetent passwd | wc -l # count user accounts on the system
For counting matches inside a file, grep -c "pattern" file is a direct alternative to piping through wc. It avoids spawning a second process and runs slightly faster. Use the pipe form (grep | wc -l) when the input is coming from another command rather than a file.
Extract only the total from a multi-file count:
bashwc -w file1.txt file2.txt | tail -1
The last line of wc output for multiple files is always the grand total.
Use -l for line counts, -m when working with UTF-8 text, and redirect input (< file) when you need a clean number for a script or pipeline. Leave a comment below if you run into any issues.
The groupdel command in Linux removes a group from the system. It deletes the group's entry from /etc/group and /etc/gshadow,…
The top command in Linux provides a real-time view of running processes and system resource usage. From…
The usermod command in Linux modifies existing user account attributes. You can use it to manage group…
The sort command in Linux reads lines from files or standard input and writes them to standard…
The wall command in Linux sends a message to the terminals of all currently logged-in users. The…
journalctl queries logs collected by systemd-journald, the systemd logging daemon. It gives you structured access to kernel…