本站内容有下面分类知识,欢迎您的到来^_^

shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言

设soyo123为首页 收藏本站
当前位置:|主页>shell相关E文资料>

#41 Figuring Out Available Disk Space

百度收藏 QQ搜藏

Related to disk quota management is the simpler question of how much disk space is available on the system. The df command reports disk usage on a per-disk basis, but the output can be a bit baffling:

$ df
Filesystem          1K-blocks      Used Available Use% Mounted on
/dev/hdb2            25695892   1871048  22519564   8% /
/dev/hdb1              101089      6218     89652   7% /boot
none                   127744         0    127744   0% /dev/shm

What would be much more useful is a version of df that summarizes the available capacity values in column four and then presents the summary in a way that is easily understood. It's a task easily accomplished in a script.

The Code
#!/bin/sh

# diskspace - Summarizes available disk space and presents it in a logical
#    and readable fashion.

tempfile="/tmp/available.$$"

trap "rm -f $tempfile" EXIT

cat << 'EOF' > $tempfile
    { sum += $4 }
END { mb = sum / 1024
      gb = mb / 1024
      printf "%.0f MB (%.2fGB) of available disk space\n", mb, gb
    }
EOF

df -k | awk -f $tempfile

exit 0


Running the Script
This script can be run as any user and produces a succinct one-line summary of available disk space.

The Results
On the same system on which the df output shown earlier was generated, the script reports the following:

$ diskspace
96199 MB (93.94GB) of available disk space

Hacking the Script
If your system has lots of disk space across many multigigabyte drives, you might even expand this script to automatically return values in terabytes as needed. If you're just out of space, it'll doubtless be discouraging to see 0.03GB of available disk space, but that's a good incentive to use diskhogs and clean things up, right?

Another issue to consider is whether it's more useful to know about the available disk space on all devices, including those partitions that cannot grow (like /boot), or whether reporting on user volumes is sufficient. If the latter is the case, you can improve this script by making a call to grep immediately after the df call. Use grep with the desired device names to include only particular devices, or use grep -v followed by the unwanted device names to screen out devices you don't want included.


上一篇:#40 Reporting Disk Hogs 下一篇:#42 Improving the Readability of df Output
power by soyo123 2007-2008