For context, I'm trying to compute the total size in bytes taken by docker images on my machine. I know of docker system df
, but I want to understand how I can do this in general.
If I run docker image ls -a
, I get something like this:
REPOSITORY TAG IMAGE ID CREATED SIZE
debian 8 00b72214a37e 3 days ago 129MB
debian latest 971452c94376 3 days ago 114MB
Now I'd like to sum the SIZE
column, so I can remove the first row with tail +2
, and then use awk
to sum the 7th column (using this):
docker image ls -a | tail +2 | awk '{s+=$7}END{print s}'
This command will correctly give me the total size in MB (243MB).
However, if an image has its size in GB, awk
will add it to the sum but will ignore the unit, so for instance, the same command would return 244MB instead of 1.243GB on the following images:
REPOSITORY TAG IMAGE ID CREATED SIZE
debian 8 00b72214a37e 3 days ago 129MB
debian latest 971452c94376 3 days ago 114MB
debian latest 971452c94376 3 days ago 1GB
How can I tweak my command to have it support sizes (or values in general) with different metric prefixes? I don't necessarily want the output to be formatted in any way, for instance an output in bytes would be fine.