wc command in UNIX is a command line utility for printing newline, word and byte counts for files. It can return the number of lines in a file, the number of characters in a file and the number of words in a file. It can also be combine with pipes for general counting operations.
To get count information on a file use the
wc command with no options.
wc /usr/share/dict/words 235886 235886 2493109 /usr/share/dict/words
The output is number of lines, number of words, number of bytes, filename.
To print the number of lines in a file (or more specifically newline counts) use the
wc -l /usr/share/dict/words 235886 /usr/share/dict/words
To print the number of characters in a file use the
wc -m /usr/share/dict/words 2493109 /usr/share/dict/words
To print the number of bytes in a file use the
wc -c /usr/share/dict/words 2493109 /usr/share/dict/words
To print the number of words in a file use the
wc -w /usr/share/dict/words 235886 /usr/share/dict/words
To count the number of records (or rows) in several CSV files the
wc can used in conjunction with pipes.
In the following example there are five CSV files. The requirement is to find out the sum of records in all five files. This can be achieved by piping the output of the cat command to wc.
cat *.csv | wc -l 1866
Done. There are 1866 records across the 5 files.
To count the number of folders and files in a directory
wc can be combined with the
ls command. By passing the
-1 options to
ls it will list one file per line. This can be piped to
wc to give a count.
ls -1 | wc -l 21
Have an update or suggestion for this article? You can edit it here and send me a pull request.