Can ls show total Kbytes of "foo*"?

Lars P. Fischer fischer at iesd.auc.dk
Fri Oct 27 14:58:54 AEST 1989


In article <2453 at umbc3.UMBC.EDU> rostamia at umbc3.UMBC.EDU (Rouben Rostamian) writes:
>I wonder if there is an obvious way to compute the total size of all
>"foo*" files in a directory.  The only way I know how is the ridiculously 
>complicated construction:
>
>ls -1s foo* | awk 'BEGIN{size=0} {size += $1} END{print "total: " size}'

You don't need the BEGIN part, e.g.

  ls -1s foo* | awk '{s += $1} END{print "total: " s}'

would do the job. What's so ridiculous about that? Instead of making
'ls' support some silly feature, you use general unix tools. As you
say, you can easily create a script/alias. If you need the feature
often, you could also create a "total" script, taking the col. to sum
as an argument, eg.

  ls -s foo* | total -1

would do the job (compare with "ls foo* | wc -l"). Creating "total"
using awk would be very simple.

>It stands to reason to expect that the command "ls -s foo*" would provide
>the corresponding information for all files "foo*" in the directory.
>Alas, it does not work that way;  although the size of each "foo*"
>is displayed, the total kilobytes for the "foo*" files is not.

Yeah, one could imagine the BSD guys making 'ls' do all kind of
things, but that's not the way to go. Making 'ls' display a total was
a mistake in the first place. You're on a unix system. Use it.
--
Lars Fischer,  fischer at iesd.auc.dk   | Seek error on /dev/brain (core dumped).
CS Dept., Univ. of Aalborg, DENMARK. |                  -- (null)



More information about the Comp.unix.questions mailing list