简体   繁体   中英

display used disk space including reserve disk space in bash

I need show used disk space as (used+reserved),I have created below script and planning to add used and reserved,Is there a better way to do this?

I need to display "disk total used available" in this format in GB.

#!/bin/sh

output=`df -h --output=source,size,used,avail /dev/vd* /dev/disk/* -x devtmpfs | grep -v 'Filesystem' | awk '{printf $1 "\t" $2 "\t" $3 "\t" $4 "\n" }'`
while read -r line; do
    diskname=`echo $line|awk -F " " '{print $1}'`
    reserved=`tune2fs -l $diskname|grep -i "Reserved block count"|awk -F ":" '{print $2}'`
    reservedInGB=`echo "$((((( $reserved * 4096 ) / 1024 ) / 1024 )))"|bc -l`
    total=`echo $line|awk -F " " '{print $2}'`
    used=`echo $line|awk -F " " '{print $3}'`
    free=`echo $line|awk -F " " '{print $4}'`
    echo $diskname $total $used $free $reservedInGB
done <<< "$output"

My local emulation doesn't do --output , but try something like this - tweak to spec.

df -PB 1GB -x devtmpfs /tmp | grep -v ^Filesystem |
   while read mnt size used avail cap disk
   do printf "%-10s %4d %4d %4d\n" $disk $size $used $avail
   done

Note that embedded spaces in the mount break this, but it handles converting to GB right in the data generation with df . Since I can't do --output I saw no reason not to use -P to make sure the mount point and its data came out on the same line. Doing a read, so reordering is easy as well, as long as the fields land correctly.

Try something like that

df -h --output=source,size,used,avail | tail -n +2 | \
while read line; \
do printf "%s\t%s\n" "$line" \
"your calc with tune2fs and ${line%%[[:space:]]*}";done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM