#42 Improving the Readability of df Output

While Script #41 summarized df command output, the most important change we can make to df is simply to improve the readability of its output.

The Code

 #!/bin/sh # newdf - A friendlier version of df. awkscript="/tmp/newdf.$$" trap "rm -f $awkscript" EXIT cat << 'EOF' > $awkscript function showunit(size) { mb = size / 1024; prettymb=(int(mb * 100)) / 100; gb = mb / 1024; prettygb=(int(gb * 100)) / 100; if ( substr(size,1,1) !~ "[0–9]" || substr(size,2,1) !~ "[0–9]" ) { return size } else if ( mb < 1) { return size "K" } else if ( gb < 1) { return prettymb "M" } else { return prettygb "G" } } BEGIN { printf "%-27s %7s %7s %7s %8s %-s\n", "Filesystem", "Size", "Used", "Avail", "Capacity", "Mounted" } ...

Get Wicked Cool Shell Scripts now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.