Glam Prestige Journal

Bright entertainment trends with youth appeal.

Here is what I have in my shell file:

FILENAME=/var/lib/backups/html-backup/backup-15-04-2016.tar.gz;
FILESIZE= $(ls -lah $FILENAME | awk '{ $FILESIZEINGB = $5}')
echo "Size is $FILESIZEINGB ."

Can you please tell me why I can not see the file size in readable form ?

It seems $FILESIZEINGB is not set and I can not see the result in the echo output.

3 Answers

You cannot set a shell variable from a child process - so setting FILESIZEINGB wouldn't work anyway (and it wouldn't work because that means something else in awk syntax).

Just use du -h:

FILESIZEINGB=$(du -h "$FILENAME" | awk '{print $1}')

Also:

  • You shouldn't have a space after =.
1

Or if you don't want to use du:

FILESIZE=$(ls -lah $FILENAME | awk '{ print $5}')
echo "Filesize is $FILESIZE"
1

I found someone made an AWK one-liner, and it had a bug but I fixed it. I also added in petabytes after terabytes.

FILE_SIZE=234234 # FILESIZE IN BYTES
FILE_SIZE=$(echo "${FILE_SIZE}" | awk '{ split( "B KB MB GB TB PB" , v ); s=1; while( $1>1024 ){ $1/=1024; s++ } printf "%.2f %s", $1, v[s] }')

Considering stat is not on every single system, I would use the AWK solution. Example; the Raspberry Pi does not have stat but it does have awk.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy