formatting output in human readable numbers

Hi,

The following command provides the usage in 1024-byte blocks

du -ks * | sort -n | echo "$1"
...
1588820 user10
2463140 user11
2464096 user12
5808484 user13
6387400 user14
.....

I am trying to produce an output of first coulmn by multiplying by 1024 so that the output should display in bytes instead of 1024 kb blocks.

du -ks * | sort -n | awk '{print ($1 * 10242, $2)}'

getting the following output

1.62727e+10 user10
2.52275e+10 user11
2.52373e+10 user12
5.94905e+10 user13
6.54198e+10 user14

I even tried the printf as under:

du -ks * | sort -n | awk '{printf("%-20s%-10s\n", $1 * 1024, $2)}'

3.17764e+06 user10
4.92628e+06 user12
4.92819e+06 user13
1.1617e+07 user14
1.27748e+07 user15

Can some help to fix this output.

Thanks,
Ghazi

This may not be the solution that you are looking for, but it comes to mind.... you might consider du with the -b flag. From the man page:

I am on HP.

-b For each name operand that is a directory for
which file system swap has been enabled, print the
number of blocks the swap system is currently using.

-k Gives the block count in 1024-byte blocks.

$> ll
total 96
-rwxr-xr-x 1 user test 23677 Jan 8 15:55 file1
-rwxr-xr-x 1 user test 23040 Jan 8 15:56 file2

$> du -bs * | sort -n
48 file1 #comment: result of 23677/512
48 file2 #comment: result of 23040/512
$> du -ks * | sort -n
24 file1 #comment: result of 23677/1024
24 file2 #comment: result of 23040/1024

thanks,
Ghazi

Hmm... this is largely untested as I don't really have a heap of large directories to test on - however, something like

#!/bin/sh

for i in `ls /my/dirs/are/here`
do
   if [ -d "$i" ]; then
      result=`ls -lR "$i" | awk 'BEGIN {c=0;} {c+=$5} END{print c}'`
      echo "$result * 1024" | bc | awk -vdir=$i '{print $1, dir }'
   fi
done

will yield *approximate* values (not a true byte count as we're just multiplying the kilobyte count by 1024) on systems without GNU du for directories only....

Cheers
ZB

Nooop... it does not work for me...

Is there other command which can produce the disk usage
in bytes instead of blocks.

thanks

Why doesn't it work for you? What is wrong with the output? Is there any output at all? What error messages are you getting? etc...

I only tested this under Linux and UNICOS as I don't have access to an HP-UX box until Monday when I'm back at work.

Cheers
ZB

No there was no output. I did not get any error. The syntax looks good but does not do any thing.

Thanks,
Ghazi

When I used to work on HP-UX systems, the first thing I did was download a gcc development environment and build GNU utilities (like du, for example).

If I were you (of course I am not) I would create a GNU development environment for HP-UX and then you an have access to a very large (and free!) world of wonderful GNU tools that run on HP-UX.

See this link: http://www.gnu.org/ You will need to install GCC:

http://gcc.gnu.org/ ....find your HP-UX platform here:

http://gcc.gnu.org/install/specific.html

As an editorial note, back in the 'good ole days' when I was a UNIX systems consultant/contractor for big firms, I installed a GNU development environment will my clients. In every case, we saved the client time and big $$$ using GNU software.

Neo

du -b does not give bytes on HP-UX, BTW:

See:

http://docs.hp.com/en/B2355-90128/du.1.html

PS: See earlier post. I recommend you install GCC build some GNU utlities for HP-UX :slight_smile:

For an example HP-UX 10 build environment see:

http://supportweb.cs.bham.ac.uk/documentation/php/install.hpux.html

For an HP-UX 10 example environmental setup (from above):

du -sk * | sort -n | awk '{print $1*1024 " " $2}'

If using awk, either change the value of OFMT (Output ForMaT) or just do something like...

du -ks * | sort -n | awk '{printf("%20d %-10s\n", $1 * 1024, $2)}'

Thanks to Ygor and others as well for helping me out.

With best regards,
Ghazi:)