Operating System - HP-UX
1756020 Members
2790 Online
108839 Solutions
New Discussion юеВ

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