Operating System - HP-UX
1829103 Members
2227 Online
109986 Solutions
New Discussion

Re: ls (is the result in bytes)

 
SOLVED
Go to solution
Manuales
Super Advisor

ls (is the result in bytes)

Hi,
i would like to know what is the size of the result after i run the command "ls -l"

Example:
$ ls -lrt prueba
-rw-rw-r-- 1 ftphr sapsys2 21 Feb 26 12:58 prueba

is "21" in bytes? if so, how can i run ls to get the value in Mb?

please let me know.
Thanks.
Dma.

34 REPLIES 34
James R. Ferguson
Acclaimed Contributor
Solution

Re: ls (is the result in bytes)

Hi:

Yes that value "21" has the units of characters. If you want to convert this to MB, you could do:

# ls -l | awk '{$5=sprintf("%6.2f",$5/1024/1024);print}'

Regards!

...JRF...

Dennis Handly
Acclaimed Contributor

Re: ls (is the result in bytes)

>is "21" in bytes?

Of course.
If you want it in Mb, you will have to write a script:
ll -rt prueba | awk '
NF >= 9 {
print $1, $2, $3, $4, $5/(1024*1024), $6, $7, $8, $9, $10, $11
next
}
{
print $0
}'
Manuales
Super Advisor

Re: ls (is the result in bytes)

and , what about Kb? how can i do that?
only 1 time: 1024?
James R. Ferguson
Acclaimed Contributor

Re: ls (is the result in bytes)

Hi (again) Manuales:

> and , what about Kb? how can i do that?

Yes:

# ls -l | awk '{$5=sprintf("%6.2f",$5/1024);print}'

Regards!

...JRF...
Manuales
Super Advisor

Re: ls (is the result in bytes)

Hi,
when i run:
ls -l | awk '{$5=sprintf("%6.2f",$5);print}'

the result is:
how can i gent 27.00 ?

total 32 0.00
-rw-rw-r-- 1 ftphr sapsys2 20.00 Mar 5 16:33 file1
-rw-rw-r-- 1 ftphr sapsys2 7.00 Mar 5 16:33 file2
James R. Ferguson
Acclaimed Contributor

Re: ls (is the result in bytes)

Hi (again):

Yes, we should skip the "total" line at the head of the list:

# ls -l | awk '/^total/ {print;next};{$5=sprintf("%6.2f",$5/1024);print}'

...converts to KB...

Regards!

...JRF...
Manuales
Super Advisor

Re: ls (is the result in bytes)

thanks ....
I have a question, if i run:
$ ls -lR | awk '{ sum += $5 } END { print sum }'

I got:
1.26335e+08

if i run:
$ ls -l | awk '/^total/ {print;next};{$5=sprintf("%6.2f",$5/1024);print}'

I got: total 155152 (KB)

is the same both cases? i mean, the result (numbers) are different .... how can i explain that?

OldSchool
Honored Contributor

Re: ls (is the result in bytes)

example one had:

ls -lR | awk '{ sum += $5 } END { print sum }'

example two had:

ls -l | awk '/^total/ {print;next};{$5=sprintf("%6.2f",$5/1024);print}'

the "-R" in example one caused it to list the sizes of all the files in all the subdirectories, while the second didn't have that option.

also notice that the second example got rid of the "total" line, although this should not change the results.

Basically, you appear to have run the test with different sets of data!
OldSchool
Honored Contributor

Re: ls (is the result in bytes)

Note also the following:

example one totals the sizes of all the files in the list, as in:

ls -lR | awk '{ sum += $5 } END { print sum }'
5912633



example two converts / displays each file to KB and displays that in the list:

ls -l | awk '/^total/ {print;next};{$5=sprintf("%6.2f",$5/1024);print}'
total 668
-rwxr----- 1 root root 0.00 Feb 23 12:26 abc
-rw-r--r-- 1 root root 1.73 Dec 26 16:05 add.hosts
-rw-r--r-- 1 root root 1.72 Dec 26 19:50 anaconda-ks.cfg
drwxr-xr-x 2 root root 4.00 Feb 25 10:26 c2
drwx------ 3 root root 4.00 Dec 26 19:57 Desktop
-rw-r--r-- 1 root root 120.00 Dec 30 14:07 examples.tar
-rwxr-xr-x 1 root root 0.04 Feb 27 15:18 fsm
-rw-r--r-- 1 root root 0.10 Mar 2 15:54 fsm1
-rwxr-xr-x 1 root root 0.13 Mar 2 16:11 fsm1.sh
-rw-r--r-- 1 root root 0.10 Mar 2 14:45 fsm2
-rw-r--r-- 1 root root 0.03 Feb 5 15:27 fsma1
-rw-r--r-- 1 root root 0.52 Feb 5 15:25 fsmawk
-rwxr-xr-x 1 root root 0.16 Feb 19 16:27 fsm.sh
-rw-r--r-- 1 root root 46.62 Dec 26 19:50 install.log
-rw-r--r-- 1 root root 5.10 Dec 26 19:50 install.log.syslog
drwxr-xr-x 3 root root 4.00 Jan 9 10:34 sudo
-rw-r--r-- 1 root root 423.72 Dec 26 15:28 tzdata-2008i-1.el2_1.noarch.rpm
Manuales
Super Advisor

Re: ls (is the result in bytes)

OldSchool, you are completely RIGHT ...

So, why it is different:

$ ls -l | awk '{ sum += $5 } END { print sum }'
RESULT: 7.94204e+07

$ ls -l | awk '/^total/ {print;next};{$5=sprintf("%6.2f",$5/1024);print}'
RESULT: total 155152
James R. Ferguson
Acclaimed Contributor

Re: ls (is the result in bytes)

Hi Manuales:

You asked:

So, why it is different:

$ ls -l | awk '{ sum += $5 } END { print sum }'
RESULT: 7.94204e+07

$ ls -l | awk '/^total/ {print;next};{$5=sprintf("%6.2f",$5/1024);print}'
RESULT: total 155152

...

In the first case we summed *characters*.

In the second case we simply passed through without converion the "total" line in the list head. The UNITS of this value are 512-character BLOCKS. Hence, 155152*512 = 79437824 =~ 7.94204e+07

Regards!

...JRF...
OldSchool
Honored Contributor

Re: ls (is the result in bytes)

$ ls -l | awk '/^total/ {print;next};{$5=sprintf("%6.2f",$5/1024);print}'
RESULT: total 155152

I take it in the above you calculated a total by hand ('cause it sure didn't print it).

if so, lots of small files could be an issue i suppose, but w/o seeing the directory listing *before* it gets run thru the various awks, its hard to tell
Manuales
Super Advisor

Re: ls (is the result in bytes)

so, wha is the best option to get the space used?
i mean, i have to get a size report of different paths , my boss needs to know how much we are using in KB.

please let me know what option is the best to get above information.

thanks in advance.
James R. Ferguson
Acclaimed Contributor

Re: ls (is the result in bytes)

Hi Manuales:

> I mean, i have to get a size report of different paths , my boss needs to know how much we are using in KB.

The 'du' command is designed for this. You could summarize a path like this:

# du -ks /path

...and then to find the one-level deeper directory usage:

# ls -l /path | awk '/^d/ {print $NF}'|xargs du -ks

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: ls (is the result in bytes)

>I have to get a size report of different paths, my boss needs to know how much we are using in KB.

You can use "du -kx".
Manuales
Super Advisor

Re: ls (is the result in bytes)

thaks both ..
now, how can i get the result in:
bytes
kbytes
mbytes ?

thanks.
James R. Ferguson
Acclaimed Contributor

Re: ls (is the result in bytes)

Hi (again) Manuales:

> now, how can i get the result in: bytes kbytes mbytes ?

Using 'du -k' means Kilobytes, so you need to appropriately convert by factors of 1024. You have quite enough examples to deduce how to do that :-)

Regards!

...JRF...


Manuales
Super Advisor

Re: ls (is the result in bytes)

tell me please !!!!
Manuales
Super Advisor

Re: ls (is the result in bytes)

tell me please !!!! today i am very tired to think it .... pleaseeeeeeeeeeeee
Manuales
Super Advisor

Re: ls (is the result in bytes)

i deduced:
$ du -m
du: illegal option -- m
usage: du [-a|-s] [-kbrx] [-t type] [name ...]

here i found:
http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.cmds/doc/aixcmds2/du.htm

To summarize the disk usage of a directory tree and each of its subtrees in MB blocks, enter:
du -m /home/fran

but it did not work for me !!!

how can i get it?
James R. Ferguson
Acclaimed Contributor

Re: ls (is the result in bytes)

Hi:

Why would you look at AIX manpages for HP-UX options? To convert KB to MB, divide by 1024.

Using my suggestion of:

# ls -l /path|awk '/^d/ {print $NF}'|xargs du -ks

...which reports KB sizes, to show in MB, you could do:

# ls -l /path|awk '/^d/ {print $NF}'|xargs du -ks|awk '{printf "%6.2f %s\n",$1/1024,$2}'

For a simple 'du' [ which includes both files and directory sizes ]:

# du -k /path|awk '{printf "%6.2f %s\n",$1/1024,$2}'

...should you want the output sorted in desceding size order pipe to a sort like:

# du -k /path|awk '{printf "%6.2f %s\n",$1/1024,$2}'|sort -knr1,1

Regards!

...JRF...
Manuales
Super Advisor

Re: ls (is the result in bytes)

thanks a lot !!!!!
Really, it was very easy but i am a little tired, i have 2 nights without sleeping ... thanks a lot for your great support ... (i am not very familiar with this comands and concepts related to the size that ls command shows, i know it is a basic command but i need to be sure what exactly means ..) THANKS A LOT, have a nice evening.
Steven Schweda
Honored Contributor

Re: ls (is the result in bytes)

"man ls"?

[...]
-l

(ell) List in long format, giving mode,
number of links, owner, group, size in
bytes, and [...]


> [...] i need to be sure [...]

Read the documentation? Is that so
difficult?
Manuales
Super Advisor

Re: ls (is the result in bytes)

OK, Steven, i will read the documentation more carefully...

one question:

i have these files:




REPORT IN KS:
$ ls -l |awk '/^d/ {print $NF}'|xargs du -ks
101637 .

REPORT IN MB:
$ ls -l |awk '/^d/ {print $NF}'|xargs du -ks|awk '{printf "%6.2f %s\n",$1/1024,$2}'
99.25 .


if i see the first one, i could think:
"101637 kb = 101.637 MB"
but if see the other instrucction i see: 99.25

so 101.637 is not equal to 99.25
what is the correct value to know in MB?

Thanks in advance.