(word count) command counts the number of lines, words, and
characters in the files you specify.
(Like most UNIX utilities (1.30
reads from its standard input
if you don't specify a filename.)
For example, the file letter
has 120 lines, 734 words, and 4297
120 734 4297 letter
You can restrict what is counted
by specifying the options -l
(count lines only), -w
(count words only), and -c
(count characters only).
For example, you can count the number of lines in a file:
wc -l letter
or you can count the number of files in a directory:
ls | wc -w
The first example uses a file as input; the second example pipes
the output of an ls
command to the input of wc
(Be aware that the
list dot files.
If your ls
to include -a
or other options that add words to the normal output - such as the
from ls -l
you may not get the results you want.)
The fact that you can pipe the output of a command through wc
to perform addition and subtraction. For example, I once wrote
a shell script that involved, among other things, splitting files into several
pieces, and I needed the script to keep track of how many files were
created. (The script ran
on each file, producing an arbitrary
number of new files named file.00
Here's the code I used to solve this problem:
before=`ls $file* | wc -l` # count the file
split the file by running it through csplit
after=`ls $file* | wc -l` # count file plus new splits
num_files=`expr $after - $before` # evaluate the difference
As another trick, the following command will tell you how many more words
are in new.file
than in old.file
expr `wc -w < new.file` - `wc -w < old.file`
[The C and Korn shells have built-in arithmetic commands and don't really need
works in all shells. -JP
Notice that you should have wc
read the input files
by using a
character. If instead you say:
expr `wc -w new.file` - `wc -w old.file`
the filenames will show up in the expressions and produce a syntax error.
||Taking this concept further, here's a simple shell script to calculate
the differences in word count between two files:
count_1=`wc -w < $1` # number of words in file 1
count_2=`wc -w < $2` # number of words in file 2
diff_12=`expr $count_1 - $count_2` # difference in word count
# if $diff_12 is negative, reverse order and don't show the minus sign:
case "$diff_12" in
-*) echo "$2 has `expr $diff_12 : '-\(.*\)'` more words than $1" ;;
*) echo "$1 has $diff_12 more words than $2" ;;
If this script were called count.it
, then you could invoke it like this:
count.it draft.2 draft.1
draft.1 has 23 more words than draft.2
You could modify this script to count lines or characters.
Unless the counts are very large, the output of wc
will have leading
spaces. This can cause trouble in scripts if you aren't careful.
For instance, in the script above, the command:
echo "$1 has $count_1 words"
draft.2 has 79 words
See the extra spaces?
Understanding how the shell handles
will help here.
If you can, let the shell read the wc
output and remove extra spaces.
For example, without quotes, the shell passes four separate words to
adds a single space between each word:
echo $1 has $count_1 words
that might print:
draft.2 has 79 words
That's especially important to understand when you use wc
with commands like test
which don't expect spaces
in their arguments.
If you can't use the shell to strip out the spaces, delete them by
piping the wc
tr -d ' '
Finally, two notes about file size:
isn't an efficient way to count the characters in large
numbers of files.
opens and reads each file, which takes time.
The fourth or fifth column of output from ls -l
your version) gives the character count without opening the file.
You can sum ls -l
counts for multiple files with the
| addup 4
Using character counts (as in the item above) doesn't give you the total
disk space used by files.
That's because, in general, each file takes at least one disk block
command gives accurate disk usage.