Can ls show total Kbytes of "foo*"?

Can ls show total Kbytes of "foo*"?

Post by Rouben Rostami » Tue, 02 Oct 1990 23:26:00



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}'

(which I alias to something.)  I run ULTRIX.  The command "ls -s" in ULTRIX
lists the files in the current directory, gives the size of each file in
kilobytes, and  also gives the total kilobytes for the files in that
directory.

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.

Am I missing something obvious?

--
Rouben Rostamian                               Phone: 301 455-2458
Department of Mathematics                      e-mail:


 
 
 

Can ls show total Kbytes of "foo*"?

Post by Lars P. Fisch » Tue, 02 Oct 1990 05:58:00



>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.

Quote:>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.
--

CS Dept., Univ. of Aalborg, DENMARK. |                  -- (null)

 
 
 

Can ls show total Kbytes of "foo*"?

Post by jim fro » Tue, 02 Oct 1990 14:09:00


|
|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}'
[...]
|Am I missing something obvious?

Yes.  You could just do "wc -c foo* | tail -1".  If you don't do the
tail it will display the size of each followed by a summary line; with
it you get just the summary line or -- if there's only one file --
just the size.

jim frost
software tool & die     "The World" Public Access Unix for the '90s

 
 
 

Can ls show total Kbytes of "foo*"?

Post by Brian Mill » Tue, 02 Oct 1990 21:51:00


<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}'

Assuming everything equal (what am I saying...this is Unix), this
might work (at least it does for me)...

cat foo* | wc -c

Any typos are the responsibility of my cat.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Has anyone seen my old friend brain?
University of Merryland, Center for Automation Research
       -A nice place to live, but I wouldn't want to work there.

 
 
 

Can ls show total Kbytes of "foo*"?

Post by Brian V. Smi » Tue, 02 Oct 1990 22:28:00



Quote:(jim frost) writes:


Quote: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}'
< [...]
< |Am I missing something obvious?
<
< Yes.  You could just do "wc -c foo* | tail -1".  If you don't do the
< tail it will display the size of each followed by a summary line; with
< it you get just the summary line or -- if there's only one file --
< just the size.

Try that on some LARGE files and you will quickly (slowly) see that
"wc" is much slower than the "ls."
_____________________________________

Lawrence Berkeley Laboratory
I don't speak for LBL, these non-opinions are all mine.

 
 
 

Can ls show total Kbytes of "foo*"?

Post by jim fro » Tue, 02 Oct 1990 19:14:00




>(jim frost) writes:
>< You could just do "wc -c foo* | tail -1".
>Try that on some LARGE files and you will quickly (slowly) see that
>"wc" is much slower than the "ls."

Ok, the following alias ought to work:

alias tsize "(cd $HOME ; echo '#include <sys/stat.h>\
main(argc,argv) int argc;char **argv;{int t=0;struct stat s;\
while(--argc){stat(*(++argv),&s);t+=s.st_size;}\
printf("%d",t);}' >foo.c ; cc -o foo foo.c) ; $HOME/foo"

A bit slow for those small jobs but if you're talking Gb files it'll
work great :-).

jim frost
software tool & die     "The World" Public Access Unix for the '90s

 
 
 

Can ls show total Kbytes of "foo*"?

Post by Rahul Dhe » Tue, 02 Oct 1990 01:59:00


I use shell scripts called "bytes" and "blocks".  They are:

bytes:
   /bin/ls -l $* | awk '{sum = sum + $4} END {print sum}'

blocks:
   du -s $* | awk '{total += $1} END {print total}'

(System V users may need to adjust the field number in the first one.)

The "blocks" script gives me total blocks used by specified files and
directory subtrees.


UUCP:  oliveb!cirrusl!dhesi
Use above addresses--email sent here via Sun.com will probably bounce.

 
 
 

Can ls show total Kbytes of "foo*"?

Post by Randal Schwar » Tue, 02 Oct 1990 18:58:00


[you *knew* it was coming... :-]

| alias tsize "(cd $HOME ; echo '#include <sys/stat.h>\
| main(argc,argv) int argc;char **argv;{int t=0;struct stat s;\
| while(--argc){stat(*(++argv),&s);t+=s.st_size;}\
| printf("%d",t);}' >foo.c ; cc -o foo foo.c) ; $HOME/foo"
|
| A bit slow for those small jobs but if you're talking Gb files it'll
| work great :-).

Here it is in *one* process (a Perl one, of course):


Just another Perl hacker (waiting for comp.lang.perl),
--
/== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\
| on contract to Intel's iWarp project, Hillsboro, Oregon, USA, Sol III  |

\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/