[comp.unix.questions] Can't ls -l add file sizes properly??

glidden@morgoth.UUCP (Ken A. Glidden) (11/18/87)

# ls -l
total 40
-rwxr--r--  1 glidden      2117 Aug  6 12:17 acctrpt
-rwxr--r--  1 glidden       179 Mar  2  1987 age.csh
-rwxrwxr--  1 root         1308 Aug  4 16:39 arcjobs.sh
-rwxr-x---  1 glidden     29696 Mar  2  1987 deltime
-rwxr--r--  1 glidden       269 Sep  9  1986 mailbarry
-r-xr--r--  1 glidden       296 Aug 25  1986 mailjim
-rwxr--r--  1 glidden       728 Jan  7  1987 newsindex
-rw-r--r--  1 root         1132 Aug  4 14:11 tmp
                         ======
SUBTOTAL------>          35,725

****
Why does the ls -l "total" give me 40Kb and my manual addition of the file 
sizes give me 35.7Kb !?!?!?! (Note: This came from Ultrix 2.0 on a DEC uVAX II)
****

-- 
LIVE:   Ken A. Glidden, (617) 969-0050
ARPA:	adelie!morgoth!glidden@harvard.HARVARD.EDU
UUCP:	{harvard | ll-xn | mirror | axiom}!adelie!morgoth!glidden

nic@marque.mu.edu (Nic Bernstein) (11/19/87)

In article <456@morgoth.UUCP> glidden@morgoth.UUCP (Ken A. Glidden) writes:
># ls -l
>total 40
>-rwxr--r--  1 glidden      2117 Aug  6 12:17 acctrpt
>-rwxr--r--  1 glidden       179 Mar  2  1987 age.csh
>-rwxrwxr--  1 root         1308 Aug  4 16:39 arcjobs.sh
>-rwxr-x---  1 glidden     29696 Mar  2  1987 deltime
>-rwxr--r--  1 glidden       269 Sep  9  1986 mailbarry
>-r-xr--r--  1 glidden       296 Aug 25  1986 mailjim
>-rwxr--r--  1 glidden       728 Jan  7  1987 newsindex
>-rw-r--r--  1 root         1132 Aug  4 14:11 tmp
>                         ======
>SUBTOTAL------>          35,725
>
>****
>Why does the ls -l "total" give me 40Kb and my manual addition of the file 
>sizes give me 35.7Kb !?!?!?! (Note: This came from Ultrix 2.0 on a DEC uVAX II)
>****
>
>-- 
>LIVE:   Ken A. Glidden, (617) 969-0050
>ARPA:	adelie!morgoth!glidden@harvard.HARVARD.EDU
>UUCP:	{harvard | ll-xn | mirror | axiom}!adelie!morgoth!glidden

	First of all, there might be some hidden files involved ( how
about something like .cshrc, .profile, etc. ).  This isn't the case
here however,  you should have run "ls -ls" which would also report
the block sizes of the files (should look like this):

 2 -rwxr--r--  1 glidden      2117 Aug  6 12:17 acctrpt
 1 -rwxr--r--  1 glidden       179 Mar  2  1987 age.csh
 2 -rwxrwxr--  1 root         1308 Aug  4 16:39 arcjobs.sh
29 -rwxr-x---  1 glidden     29696 Mar  2  1987 deltime
 1 -rwxr--r--  1 glidden       269 Sep  9  1986 mailbarry
 1 -r-xr--r--  1 glidden       296 Aug 25  1986 mailjim
 1 -rwxr--r--  1 glidden       728 Jan  7  1987 newsindex
 2 -rw-r--r--  1 root         1132 Aug  4 14:11 tmp

This first number is the number of blocks allocated to the file and will
always be at a 1024 byte boundry (at least on your machine).  This means
that the file "age.csh" which is only 179 bytes, takes up 1024 bytes of
disk space.  The number reported by "ls" and also by "du" is useful to
determine the amount of space you would have to allocate to back up the
files with cpio, tar etc.  You also then would have to add hidden files
("ls -als") and usually add a block for the directory itself ("."),
unless it is very large, then it would be more, but "ls -als" will
report the size of ".".
						- Nic

tony@artecon.UUCP (11/19/87)

In article <456@morgoth.UUCP> glidden@morgoth.UUCP (Ken A. Glidden) writes:
# ls -l
total 40
 3 -rwxr--r--  1 glidden      2117 Aug  6 12:17 acctrpt
 1 -rwxr--r--  1 glidden       179 Mar  2  1987 age.csh
 2 -rwxrwxr--  1 root         1308 Aug  4 16:39 arcjobs.sh
29 -rwxr-x---  1 glidden     29696 Mar  2  1987 deltime
 1 -rwxr--r--  1 glidden       269 Sep  9  1986 mailbarry
 1 -r-xr--r--  1 glidden       296 Aug 25  1986 mailjim
 1 -rwxr--r--  1 glidden       728 Jan  7  1987 newsindex
 2 -rw-r--r--  1 root         1132 Aug  4 14:11 tmp
==                          ======
40   SUBTOTAL------>        35,725

I added another column for you (try ls -ls).  This column is the number of
1k blocks occupied by the files.  That is, even though a file may only be
179 bytes, it still occupies a full block on the disk which is 1024 in
this example.  So the number given at the total by ls -l is the number of
blocks occupied by those files, not a total of the byte counts.  It may
seem like a high amount of overhead, but for the tradeoff in speed, it
is worth it.

-- Tony
-- 
**************** Insert 'Standard' Disclaimer here:  OOP ACK! *****************
*  Tony Parkhurst  -->  hp-sdd!artecon!adp (or) hp-sdd!artecon!adp@nosc.ARPA  *
* "One lawyer can steal more than a hundred men with guns." -- The Godfather  *
*******************************************************************************

alen@cogen.UUCP (Alen Shapiro) (11/19/87)

In article <456@morgoth.UUCP> glidden@morgoth.UUCP (Ken A. Glidden) writes:
># ls -l
>total 40
>-rwxr--r--  1 glidden      2117 Aug  6 12:17 acctrpt
>-rwxr--r--  1 glidden       179 Mar  2  1987 age.csh
>-rwxrwxr--  1 root         1308 Aug  4 16:39 arcjobs.sh
>-rwxr-x---  1 glidden     29696 Mar  2  1987 deltime
>-rwxr--r--  1 glidden       269 Sep  9  1986 mailbarry
>-r-xr--r--  1 glidden       296 Aug 25  1986 mailjim
>-rwxr--r--  1 glidden       728 Jan  7  1987 newsindex
>-rw-r--r--  1 root         1132 Aug  4 14:11 tmp
>                         ======
>SUBTOTAL------>          35,725
>
>****
>Why does the ls -l "total" give me 40Kb and my manual addition of the file 
>sizes give me 35.7Kb !?!?!?! (Note: This came from Ultrix 2.0 on a DEC uVAX II)
>****

Try this for size (excuse the pun please). Our version of ls thinks it
knows that we have a minimum disk block size of 1k (the 1024 byte variety).
The number of 'k' that ls reports is the number of disk blocks used. Since
only 1 in 1024 files will fully populate disk blocks (except if they
are compiled images (darn there goes a perfectly reasonable generalisation))

Hence a discrepancy between the number of bytes used and the number of
1k blocks used. Try ls -ls (following) to see the effect.

total 40
 3 -rwxr--r--  1 glidden      2117 Aug  6 12:17 acctrpt
 1 -rwxr--r--  1 glidden       179 Mar  2  1987 age.csh
 2 -rwxrwxr--  1 root         1308 Aug  4 16:39 arcjobs.sh
29 -rwxr-x---  1 glidden     29696 Mar  2  1987 deltime
 1 -rwxr--r--  1 glidden       269 Sep  9  1986 mailbarry
 1 -r-xr--r--  1 glidden       296 Aug 25  1986 mailjim
 1 -rwxr--r--  1 glidden       728 Jan  7  1987 newsindex
 2 -rw-r--r--  1 root         1132 Aug  4 14:11 tmp
==                          ======
40                          35,725

--alen the Lisa slayer (it's a long story)

ps du's approach to the same problem is yet another story. It lives
in an age long gone (512 byte blocks & expensive disks)

mkhaw@teknowledge-vaxc.ARPA (Mike Khaw) (11/19/87)

On Ultrix 1.2, I did

ls -s | tail +2 | awk '{sum += $1} END { print sum }'

and got the same number that the ls "total" printed.  Apparently the
"total" just adds up the number of kilobytes that each file occupies
(rounded up to the nearest kilobyte for each file), so you get accumulated
rounding error.

Mike Khaw
-- 
internet:  mkhaw@teknowledge-vaxc.arpa
usenet:	   {uunet|sun|ucbvax|decwrl|uw-beaver}!mkhaw%teknowledge-vaxc.arpa
USnail:	   Teknowledge Inc, 1850 Embarcadero Rd, POB 10119, Palo Alto, CA 94303

john@frog.UUCP (11/19/87)

In article <456@morgoth.UUCP>, glidden@morgoth.UUCP (Ken A. Glidden) writes:
[ In effect, why does ls say "40Kb" when only 35,725 bytes are used. ]
> # ls -l
> total 40
Ah, but that means 40 BLOCKS, not 40,000 bytes.  Here's where it went:
> -rwxr--r--  1 glidden      2117 Aug  6 12:17 acctrpt
			3 (1 Kb blocks, note that the last is mostly empty)
> -rwxr--r--  1 glidden       179 Mar  2  1987 age.csh
		        1 (this whole block is also mostly empty)
> -rwxrwxr--  1 root         1308 Aug  4 16:39 arcjobs.sh
			2
> -rwxr-x---  1 glidden     29696 Mar  2  1987 deltime
			29 (exactly)
> -rwxr--r--  1 glidden       269 Sep  9  1986 mailbarry
			1
> -r-xr--r--  1 glidden       296 Aug 25  1986 mailjim
			1
> -rwxr--r--  1 glidden       728 Jan  7  1987 newsindex
			1
> -rw-r--r--  1 root         1132 Aug  4 14:11 tmp
			2
>                          ======
> SUBTOTAL------>          35,725
Real total------------->40 blocks

The technical term for this is "breakage," the amount of space wasted by
always allocating a fixed size chunk for the (varying length) tail of a
file.

4.[23]BSD ameliorates this (somewhat) by using two sizes of blocks, typically
8K and 1K (since they like large blocks, you can see why they'd be concerned
about losing half a block, on average, per file!).

--
John Woods, Charles River Data Systems, Framingham MA, (617) 626-1101
...!decvax!frog!john, ...!mit-eddie!jfw, jfw@eddie.mit.edu

"Cutting the space budget really restores my faith in humanity.  It
eliminates dreams, goals, and ideals and lets us get straight to the
business of hate, debauchery, and self-annihilation."
		-- Johnny Hart

roger@celtics.UUCP (Roger B.A. Klorese) (11/19/87)

In article <456@morgoth.UUCP> glidden@morgoth.UUCP (Ken A. Glidden) writes:
|# ls -l
|total 40
|-rwxr--r--  1 glidden      2117 Aug  6 12:17 acctrpt
|-rwxr--r--  1 glidden       179 Mar  2  1987 age.csh
|-rwxrwxr--  1 root         1308 Aug  4 16:39 arcjobs.sh
|-rwxr-x---  1 glidden     29696 Mar  2  1987 deltime
|-rwxr--r--  1 glidden       269 Sep  9  1986 mailbarry
|-r-xr--r--  1 glidden       296 Aug 25  1986 mailjim
|-rwxr--r--  1 glidden       728 Jan  7  1987 newsindex
|-rw-r--r--  1 root         1132 Aug  4 14:11 tmp
|                         ======
|SUBTOTAL------>          35,725
|
|****
|Why does the ls -l "total" give me 40Kb and my manual addition of the file 
|sizes give me 35.7Kb !?!?!?! (Note: This came from Ultrix 2.0 on a DEC uVAX II)

"ls -l" is counting allocated fragments.  Disk is allocated, in this case, in
1KB fragments, so you should add:

total 40
-rwxr--r--  1 glidden      2117 Aug  6 12:17 acctrpt		3096
-rwxr--r--  1 glidden       179 Mar  2  1987 age.csh		1024
-rwxrwxr--  1 root         1308 Aug  4 16:39 arcjobs.sh		2048
-rwxr-x---  1 glidden     29696 Mar  2  1987 deltime	       29696
-rwxr--r--  1 glidden       269 Sep  9  1986 mailbarry		1024
-r-xr--r--  1 glidden       296 Aug 25  1986 mailjim		1024
-rwxr--r--  1 glidden       728 Jan  7  1987 newsindex		1024
-rw-r--r--  1 root         1132 Aug  4 14:11 tmp		2048
                         ======				       =====
SUBTOTAL------>          35,725				       40960

-- 
 ///==\\   (Your message here...)
///        Roger B.A. Klorese - CELERITY (Northeast Area)
\\\        40 Speen St.  Framingham, MA 01701 USA  +1 617 872-1552
 \\\==//   celtics!roger@necntc.NEC.COM - necntc!celtics!roger

jfh@killer.UUCP (The Beach Bum) (11/19/87)

In article <456@morgoth.UUCP>, glidden@morgoth.UUCP (Ken A. Glidden) writes:
> # ls -l
> total 40
> -rwxr--r--  1 glidden      2117 Aug  6 12:17 acctrpt
> -rwxr--r--  1 glidden       179 Mar  2  1987 age.csh
> -rwxrwxr--  1 root         1308 Aug  4 16:39 arcjobs.sh
> -rwxr-x---  1 glidden     29696 Mar  2  1987 deltime
> -rwxr--r--  1 glidden       269 Sep  9  1986 mailbarry
> -r-xr--r--  1 glidden       296 Aug 25  1986 mailjim
> -rwxr--r--  1 glidden       728 Jan  7  1987 newsindex
> -rw-r--r--  1 root         1132 Aug  4 14:11 tmp
>                          ======
> SUBTOTAL------>          35,725
 
No, adding up the number of blocks in each file gives you

	acctrpt		3
	age.csh		1
	arcjobs.sh	2
	deltime	       29
	mailbarry	1
	mailjim		1
	newsindex	1
	tmp		2

	total	       40 blocks exactly.
> ****
> Why does the ls -l "total" give me 40Kb and my manual addition of the file 
> sizes give me 35.7Kb !?!?!?! (Note: This came from Ultrix 2.0 on a DEC uVAX II)

Because, Oh Great Dweeb, ls reports in blocks, and since the entire block
must be used, it tells you just how big the file is IN BLOCKS.  Try it on
an old system - BUT THEN - the entire world is running on a Vax!

For the next part, how you divided 35,725 bytes by 1024 and came up with
35.7 KB is beyond me.  I did the division and came up with 34.887 KB.

> LIVE:   Ken A. Glidden, (617) 969-0050

Is this guy a yuppy undergrad?  What is it with these undergrads anyway?

Oh yes, time for a yuppy joke.

What is the difference between a yuppy and a pigeon?

The pigeon can still make a deposit on a BMW.

- John.
-- 
John F. Haugh II                  SNAIL:  HECI Exploration Co. Inc.
UUCP: ...!ihnp4!killer!jfh                11910 Greenville Ave, Suite 600
      ...!ihnp4!killer!rpp386!jfh         Dallas, TX. 75243
"Don't Have an Oil Well?  Then Buy One!"  (214) 231-0993

mwm@eris.UUCP (11/20/87)

In article <1879@celtics.UUCP> roger@celtics.UUCP (Roger B.A. Klorese) writes:
<In article <456@morgoth.UUCP> glidden@morgoth.UUCP (Ken A. Glidden) writes:
<|Why does the ls -l "total" give me 40Kb and my manual addition of the file 
<|sizes give me 35.7Kb !?!?!?! (Note: This came from Ultrix 2.0 on a DEC uVAX II)
<
<"ls -l" is counting allocated fragments.  Disk is allocated, in this case, in
<1KB fragments, so you should add:

No - those who said it was counting "1k blocks" (as opposed to
allocated fragments - which could be anywhere from 512 bytes to 4K)
were right.

Along the same lines, the "total" figure is only for the files
actually listed. Any files starting with a "." (most importantly, "."
itself :-) aren't counted.

How did I glean these bits of information? By looking through the
source. I realize that not everyone has source, so this route isn't
available. But that still doesn't excuse empirical guesswork when
others have the source. Please: if you aren't *sure* of your answer
(i.e. - it's in the source or documentation in front of you) wait to
see if someone posts an answer before posting. If you disagree with
them (i.e. - thinking it's fragments intead of 1K blocks) check before
posting. If agree - or disagree and were wrong, don't post.

	<mike
--
My feet are set for dancing,				Mike Meyer
Won't you turn your music on.				mwm@berkeley.edu
My heart is like a loaded gun,				ucbvax!mwm
Won't you let the water run.				mwm@ucbjade.BITNET

gwyn@brl-smoke.ARPA (Doug Gwyn ) (11/20/87)

# ls -l
total 40
-rwxr--r--  1 glidden      2117 Aug  6 12:17 acctrpt	 3 blocks
-rwxr--r--  1 glidden       179 Mar  2  1987 age.csh	 1 block
-rwxrwxr--  1 root         1308 Aug  4 16:39 arcjobs.sh	 2 blocks
-rwxr-x---  1 glidden     29696 Mar  2  1987 deltime	29 blocks
-rwxr--r--  1 glidden       269 Sep  9  1986 mailbarry	 1 block
-r-xr--r--  1 glidden       296 Aug 25  1986 mailjim	 1 block
-rwxr--r--  1 glidden       728 Jan  7  1987 newsindex	 1 block
-rw-r--r--  1 root         1132 Aug  4 14:11 tmp	 2 blocks
                         ======				=========
SUBTOTAL------>          35,725				40 blocks

ron@topaz.rutgers.edu.UUCP (11/20/87)

The file size reported with each file with the -l command is the number
of bytes in the file.  The file size reported by the -s command is the
number of blocks consumed by the file (counts indirect blocks and the
partially filled one on the end).  The size in the total field reflects
the sum of the -s block count.

-Ron

still@usceast.UUCP (Bert Still) (11/21/87)

In article <456@morgoth.UUCP> glidden@morgoth.UUCP (Ken A. Glidden) writes:
># ls -l
>total 40
>-rwxr--r--  1 glidden      2117 Aug  6 12:17 acctrpt
>-rwxr--r--  1 glidden       179 Mar  2  1987 age.csh
>-rwxrwxr--  1 root         1308 Aug  4 16:39 arcjobs.sh

		[... other files left out ...]

>-rw-r--r--  1 root         1132 Aug  4 14:11 tmp
>                         ======
>SUBTOTAL------>          35,725
>
>****
>Why does the ls -l "total" give me 40Kb and my manual addition of the file 
>sizes give me 35.7Kb !?!?!?! (Note: This came from Ultrix 2.0 on a DEC uVAX II)
>****
>

	What you need to notice here, is that the "total 40" line indicates
how many disk BLOCKS are in use inside this directory. If a file occupies any
part of a disk block, the whole block will be allocated to that file. Thus, the
file age.csh occupies 1 block, while arcjobs.sh occupies 2. If you divide the
character-count of each file by 1024, then round each answer up and sum, you
will get the 40 listed. The "wasted space" is usually called overhead, and that
is why 1k blocks were chosen over 2k or 4k blocks.

					Bert

--------------------------------------------------------------------------------
		It's great to be the systems manager.... pull.
--------------------------------------------------------------------------------
UUCP:   ...seismo!ncr-sd!ncrcae      #  US SNAIL:  Bert Still
                 !usceast!still      #             Dept of Mathematics
CSNET:  still@cs.scarolina.edu       #             University of South Carolina
                                     #             Columbia, SC 29208
BITNET: T410119@UNIVSCVM             #
--------------------------------------------------------------------------------

ruiu@tic.UUCP (11/22/87)

This is a response to responses of easy questions.

It seems that whenever anyone asks a simple question, there is always a 
veritable flood about it. Some people flame the original poster for not
knowing, and daring to ask what is probably a silly question.

I will dare to conjecture that people who do this, post a question instead
of asking around, don't know that they are asking a bluntly obvious que-
stion:
       why don't ls's add up, how do you peek and poke in c... etc.

Please don't flame people for not knowing, even those who think themselves
gurus were probably struggling neophytes once. Everyone deserves to learn.

So when you see an obvious one, don't jump on the post bandwagon, just quietly
email a response. I mark simple questions to look at later, and if they have
not been answered in a couple of days, *then* I try to answer them. I think
if more people did this, there would be less crying about bandwith. (And
if we could get the amiga people a dedicated net to handle all their traffic :-)

So don't scream at uninformed people, tell them the answer, and maybe next
time they can help you in an area you are new to.


-- 
Dragos Ruiu          Disclaimer: My opinons are my employer's, I'm unemployed!
            UUCP:{ubc-vision,mnetor,vax135,ihnp4}!alberta!edson!tic!dragos!work
(403) 432-0090         #1705, 8515 112th Street, Edmonton, Alta. Canada T6G 1K7 
Never play leapfrog with Unicorns...

wnp@killer.UUCP (Wolf Paul) (11/23/87)

In article <456@morgoth.UUCP> glidden@morgoth.UUCP (Ken A. Glidden) writes:
># ls -l
>total 40
>-rwxr--r--  1 glidden      2117 Aug  6 12:17 acctrpt
> ... lines deleted ...
>                         ======
>SUBTOTAL------>          35,725
>
>****
>Why does the ls -l "total" give me 40Kb and my manual addition of the file 
>sizes give me 35.7Kb !?!?!?! ...

Because the ls command total gives the total diskspace occupied by the files,
and thus always gives a multiple of 1 block - usually 512 bytes or 1 KB.
The file sizes displayed for each entry however is the actual length of the
file, disreagarding unused partial disk blocks.

Each file, even if it contains only  1 character, occupies at least one
disk block, therefore ls would display a total of 1 block but a filesize of
1 character.

Wolf Paul
ihnp4!killer!dcs!wnp