#!/bin/bash # formatdir--Outputs a directory listing in a friendly and useful format. # Note that you need to ensure "scriptbc" (script #9) is in your current path # because it's invoked within more than once. # Function to format sizes in Kb to Kb, Mb, or Gb for more readable output. proc readablesize { if test $1 -ge 1048576 { echo "$[scriptbc -p 2 $1 / 1048576]Gb" } elif test $1 -ge 1024 { echo "$[scriptbc -p 2 $1 / 1024]Mb" } else { echo "$(1)Kb" } } ################# ## MAIN CODE if test $Argc -gt 1 { echo "Usage: $0 [dirname]" > !2; exit 1 } elif test $Argc -eq 1 { # Specified a directory other than the current one? cd @Argv # Then let’s change to that one. if test $Status -ne 0 { # Or quit if the directory doesn't exist exit 1 } } for file in [*] { if test -d $file { setglobal size = $[ls $file | wc -l | sed 's/[^[:digit:]]//g] if test $size -eq 1 { echo "$file ($size entry)|" } else { echo "$file ($size entries)|" } } else { setglobal size = $[ls -sk $file | awk '{print $1}] echo "$file ($[readablesize $size])|" } } | \ sed 's/ /^^^/g' | \ xargs -n 2 | \ sed 's/\^\^\^/ /g' | \ awk -F'|' '{ printf "%-39s %-39s\n", $1, $2 }' exit 0