How can I change the filesize unit that is output by the tar command?

tar tvf test.tar.gz | awk '{print $5,$9}'

I would like for it to print the filesize in Gigabytes.

  • A gigabyte is 1000000000 bytes. Division of the file size in bytes by that number should give you the size in gigabytes. – Kusalananda Feb 11 at 21:40

Very crudely, you could:

$ while read size filename; do echo "$filename: $(( size /1024 /1024 /1024))"; done < <(tar tvf test .tar.gz | awk '{print $4,$7}') test1.txt: 1 test2.txt: 5 test3.txt: 0 $

You may also like to add some decimal precision:

$ while read size filename; do echo "$filename: $(( (size * 100) /1024 /1024 /1024))"; done < <(tar tvf test.tar.gz | awk '{print $4,$7}') | sed 's/\([0-9][0-9]\)$/.\1/' test1.txt: 1.03 test2.txt: 5.93 test3.txt: 0.44 $

  • Why those cumbersome while loops? awk cand do all that work, and the work of sed as well. – Kusalananda Feb 11 at 21:55

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.