Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying long integers

Status
Not open for further replies.

nashcom

MIS
Apr 12, 2002
91
0
0
GB
Is it possible to print a larger number than a standard 32bit integer?

For example, I need to output the size of a directory in bytes, and the directory is 6GB.

I've tried a few things, but the simplest line is:

Code:
du -s -k | awk 'print $1*1000}'

The -k option lists the directory size in KB, but I need the (rough) size in bytes so multiply it by 1000, but awk prints it as 6.1444e+09.

I've lived with this problem for ages, but would love to know how to get around it!

Thanks.
 
Use the printf awk function.
Another way:
du -sk | awk '{print $1"1000"}'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi

Just force the formatting :
Code:
du -s -k | awk '{print[highlight]f[/highlight] [highlight]"%d\n",[/highlight]$1*1000}'
The actual success may depend on your Awk implementation.

Personally I would use [tt]bc[/tt] instead :
Code:
echo "$( du -s -k )*1000" | bc -q

Feherke.
[link feherke.github.com/][/url]
 
Thanks very much.

The first example concatenating the $1 and "000" works well enough - I just wasn't sure how to join the number and the string.

The printf %d returns a strange number (-2147483648). I'd been using printf %d in my code, and if I see that number being printed it means the directory is big!

My bc also says that -q is an illegal option.

Thanks again.
 
Yet another way:
du -sk | awk '{printf "%.0f\n",$1*1000}'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi

nashcom said:
My bc also says that -q is an illegal option.
I included the [tt]-q[/tt] ( [tt]--quiet[/tt] ) option as once I saw a [tt]bc[/tt] implementation which printed the version information even when input was piped. Just remove it if your [tt]bc[/tt] does not support it.

And a correction, as I forgot that [tt]du[/tt] outputs the path too :
Code:
echo "$( du -s -k | cut -f1 )*1000" | bc

Feherke.
[link feherke.github.com/][/url]
 
Thanks very much for all the help - those methods work great.

I don't really know what I'm doing with it, so it takes me a while to work things out, but Unix scripting is fabulous in its power and flexibility.
 
1kB = 1024B
1MB = 1024kB
1GB = 1024MB = 1024x1024x1024B

1024 = 2^10

You should multiply by 1024 instead of 1000 to be precise.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top