INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Displaying long integers

Displaying long integers

(OP)
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.

RE: Displaying long integers

Hi

Just force the formatting :

CODE

du -s -k | awk '{printf "%d\n",$1*1000}' 
The actual success may depend on your Awk implementation.

Personally I would use bc instead :

CODE

echo "$( du -s -k )*1000" | bc -q 

Feherke.
http://feherke.github.com/

RE: Displaying long integers

(OP)
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.

RE: Displaying long integers

Hi

Quote (nashcom)

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

And a correction, as I forgot that du outputs the path too :

CODE

echo "$( du -s -k | cut -f1 )*1000" | bc 

Feherke.
http://feherke.github.com/

RE: Displaying long integers

(OP)
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.

RE: Displaying long integers

1kB = 1024B
1MB = 1024kB
1GB = 1024MB = 1024x1024x1024B

1024 = 2^10

You should multiply by 1024 instead of 1000 to be precise.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close