简体   繁体   English

Bash - 将变量转换为人类可读的格式(KB、MB、GB)

[英]Bash- Converting a variable to human readable format (KB, MB, GB)

In my bash script, I run through a list of directories and read in the size of each directory to a variable using the du command.在我的 bash 脚本中,我运行了一个目录列表,并使用 du 命令将每个目录的大小读入到一个变量中。 I also keep a running total of the total size of the directories.我还保留了目录总大小的运行总数。 The only problem is that after I get the total size, it's in an unreadable format (ex. 64827120).唯一的问题是,在我获得总大小后,它的格式无法读取(例如 64827120)。 How can I convert the variable containing this number into GBs, MBs, etc?如何将包含此数字的变量转换为 GB、MB 等?

您想使用du -h为您提供“人类可读”的输出,即 KB、MB、GB 等。

Try using du -sh for getting summarise size in human readable, also you can find the command related help in manual.尝试使用du -sh获取人类可读的汇总大小,您也可以在手册中找到与命令相关的帮助。
Try below command, it will give you the size in Human readable format试试下面的命令,它会给你人类可读格式的大小

 du | tail -1 | awk {'print $1'} | awk '{ total = $1 / 1024 ; print total "MB" }'

 du | tail -1 | awk {'print $1'} | awk '{ total = $1 / 1024/1024 ; print total "GB" }'

This is a combination of @Mahattam response and some others I combined which tallys the total amount in the standard format and then formats the output in human readable.这是@Mahattam 响应和我结合的其他一些响应的组合,它们以标准格式计算总量,然后以人类可读的格式输出输出。

for path in $(awk -F: '{if ($3 >= 1000) print $6}' < /etc/passwd); do disk_usage=0; disk_usage=$(du -s ${path} | grep -oE '[[:digit:]]+');  echo "$path: $(echo $disk_usage | tail -1 | awk {'print $1'} | awk '{ total = $1 / 1024/1024 ; printf("%.2fGB\n", total) }')"; myAssociativeArray[${path}]=${disk_usage}; done ; total=$(IFS=+; echo "$((${myAssociativeArray[*]}))"); echo "Total disk usage: $(echo $total | tail -1 | awk {'print $1'} | awk '{ total = $1 / 1024/1024 ; printf("%.2fGB\n", total) }')"; unset total; unset disk_usage ;

外观示例

How it works.这个怎么运作。

This could be anything you want to iterate through path list but in this example its just using the /etc/pass to loop over users paths source is here for path in $(awk -F: '{if ($3 >= 1000) print $6}' < /etc/passwd)这可能是您想要遍历路径列表的任何内容,但在此示例中,它仅使用 /etc/pass 循环遍历用户路径source is here for path in $(awk -F: '{if ($3 >= 1000) print $6}' < /etc/passwd)

It then calculates the usage per folder and extracts only digits from the output in the loop disk_usage=0; disk_usage=$(du -s ${path} | grep -oE '[[:digit:]]+')然后计算每个文件夹的使用量并仅从循环disk_usage=0; disk_usage=$(du -s ${path} | grep -oE '[[:digit:]]+')的输出中提取数字disk_usage=0; disk_usage=$(du -s ${path} | grep -oE '[[:digit:]]+') disk_usage=0; disk_usage=$(du -s ${path} | grep -oE '[[:digit:]]+')

It outputs the nice formatting rounded to 2 decimal points echo "$path: $(echo $disk_usage | tail -1 | awk {'print $1'} | awk '{ total = $1 / 1024/1024 ; printf("%.2fGB\\n", total) }')";它输出四舍五入到 2 个小数点的漂亮格式echo "$path: $(echo $disk_usage | tail -1 | awk {'print $1'} | awk '{ total = $1 / 1024/1024 ; printf("%.2fGB\\n", total) }')";

Adds this to the bash associative array myAssociativeArray[${path}]=${disk_usage}将此添加到 bash 关联数组myAssociativeArray[${path}]=${disk_usage}

then it sums the total value in the original amount from the array total=$(IFS=+; echo "$((${myAssociativeArray[*]}))")然后它对来自数组total=$(IFS=+; echo "$((${myAssociativeArray[*]}))")的原始金额求和

then we use the same fancy output formatting to show this nicely echo "Total disk usage: $(echo $total | tail -1 | awk {'print $1'} | awk '{ total = $1 / 1024/1024 ; printf("%.2fGB\\n", total) }')";然后我们使用相同的花哨输出格式来显示这个很好的echo "Total disk usage: $(echo $total | tail -1 | awk {'print $1'} | awk '{ total = $1 / 1024/1024 ; printf("%.2fGB\\n", total) }')";

I used a variation of this for calculating cPanel Resellers accounts disk usage in the below monster oneliner.我使用了它的一个变体来计算下面的怪物 oneliner 中的 cPanel 经销商帐户磁盘使用情况。

Reseller="CPUsernameInputField"; declare -A myAssociativeArray ; echo "==========================================" | tee -a ${Reseller}_disk_breakdown.txt ; echo "Reseller ${Reseller}'s Disk usage by account"| tee -a ${Reseller}_disk_breakdown.txt; for acct in $(sudo grep ${Reseller} /etc/trueuserowners | cut -d: -f1); do disk_usage=0; disk_usage=$(du -s /home/${acct} | grep -oE '[[:digit:]]+');  echo "$acct: $(echo $disk_usage | tail -1 | awk {'print $1'} | awk '{ total = $1 / 1024/1024 ; printf("%.2fGB\n", total) }')" | tee -a ${Reseller}_disk_breakdown.txt ; myAssociativeArray[${acct}]=${disk_usage}; done ; total=$(IFS=+; echo "$((${myAssociativeArray[*]}))"); echo "Total disk usage: $(echo $total | tail -1 | awk {'print $1'} | awk '{ total = $1 / 1024/1024 ; printf("%.2fGB\n", total) }')" | tee -a ${Reseller}_disk_breakdown.txt; unset total; unset disk_usage;echo "==========================================" | tee -a ${Reseller}_disk_breakdown.txt ; echo "Sorted by top users" | tee -a ${Reseller}_disk_breakdown.txt; for key in "${!myAssociativeArray[@]}"; do printf '%s:%s\n' "$key" "${myAssociativeArray[$key]}"; done | sort -t : -k 2rn | tee -a ${Reseller}_disk_breakdown.txt;echo "==========================================" | tee -a ${Reseller}_disk_breakdown.txt ;for key in "${!myAssociativeArray[@]}"; do USER_HOME=$(eval echo ~${key}); echo "Disk breakdown for $key" | tee -a ${Reseller}_disk_breakdown.txt ; sudo du -h ${USER_HOME} --exclude=/app --exclude=/home/virtfs| grep ^[0-9.]*[G,M]  | sort -rh|head -n20 | tee -a ${Reseller}_disk_breakdown.txt;echo "=======================================" | tee -a ${Reseller}_disk_breakdown.txt; done

cPanel 经销商磁盘使用示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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