#!/bin/ksh ## ## SCRIPT: diruse_files_onelevel_agesort_bygui ## ## Where: in $FEDIR/scripts where $FEDIR=/apps/nns_com/fea ## ############################################################################## ## PURPOSE: Shows the disk usage for each of the files, at ONE ## directory level, in a user-specified directory ## -- SORTED BY AGE. ## ## A Tcl-Tk script presents a GUI that prompts for hostid ## and a directory name. ## ############################################################################## ## CALLED BY: 'spacetools' in $FEDIR/scripts, ## actually, by 'spacetools.chestdef' in $FEDIR/scripts. ## ############################################################################## ## MAINTENANCE HISTORY: ## Written by: B. Montandon O06 16Mar2000 Based on 'duseagedirfils' and ## 'diruse_files_onelevel_sizesort_bygui' ## in $FEDIR/scripts ## Updated by: B. Montandon O06 17Mar2000 Added calc of $TOTMEG from ## total 512-byte blocks in 'ls -l' ## output. ## Updated by: B. Montandon O06 6Sep2000 Add 'Unknown host' check. ## Updated by: B. Montandon O06 7Sep2000 Add local/remote DIRCHECK routine. ## Updated by: B. Montandon O06 21Sep2000 Chg Bytes to KiloBytes and add ## decimal point for readability. ## ## Updated by: Blaise Montandon 29apr2003 Replace Tk-GUI script ## 'enter_hostid_and_dir.tk' by ## 'enter_hostid_and_dir_toghelp-scroll.tk'. ## ## Updated by: Blaise Montandon 03sep2003 Separate directories from other files ## in the output listing. ## ## Updated by: Blaise Montandon 04sep2003 Load HOST_ID & DIRNAME vars from ## script args $1 & $2. If not loaded ## (i.e. = "") then use the previous ## defaults. ## ## Updated by: Blaise Montandon 04sep2003 Use better 'awk' technique to handle ## file & link names at end of lines ## and any number of embedded blanks ## --- from script ## diruse_files_onelevel_sizesort_bygui ############################################################################## ############################################################################# ## SET A UTILITY SCRIPTS PATHNAME in case this toolchest is not started from ## the HandyTools toolchest or the nnsFEAmenu toolchest --- and a ## $FEDIR utility script (or help file) is needed. ############################################################################# if test "$FEDIR" = "" then FEDIR=/apps/nns_com/fea export FEDIR fi ############################################################################## ## SET query hostname and directory from script arguments 1 & 2 ## --- if not already set. ############################################################################## if test "$HOST_ID" = "" then HOST_ID="$1" fi if test "$HOST_ID" = "" then DIRNAME="$2" fi ############################################################################## ## SET LOCAL HOST NAME FOR MESSAGES & REPORTS & LOCAL-HOST-CHECKS. ############################################################################## THISHOST=`hostname` SERVER="engprd00" FEA_SERVER="engfea00" VIZ_SERVER="engvis00" ######################################################################## ## SET 'CONSTANT' PARMS of the GUI that prompts for hostname & directory. ## Also set a default HOSTID and DIRECTORY NAME for the GUI. ######################################################################## WINTITLE="FileSizes_for_given_Host:Dirname (one dir level) -- AGE SORTED" export WINTITLE WIN_INFO="\ Enter 1) the name of an SGI (Unix) host on the site network and 2) a directory name. This utility shows all the 'relative' file names (not 'full' names), at *ONE* level under the specified directory --- in *AGE* sort order. This report is typically generated in 3 to 10 seconds. This utility uses the 'ls -altr' and 'grep' and 'awk' commands in the form of a 'pipeline' of commands: ls -altr | grep -v '^d' | awk '{ ... print ... }' and ls -altr | grep '^d' | awk '{ ... print ... }' at the specified host. ------------ PERFORMANCE: Since files at only ONE directory level are listed, with 'simply-retrieved' data, performance is not usually an issue, but ... For FAST PERFORMANCE, on a really large list of files in a directory, it is BEST TO SPECIFY THE HOST AT WHICH THE DIRECTORY'S DISK IS LOCALLY ATTACHED --- so that the 'ls' command is executed on the host where the directory 'resides', rather than accessing a directory 'over the network'. Examples: $SERVER and $HOME $SERVER and /data//cae/$USER $THISHOST and /local/scratch/$USER $THISHOST and /local/scratch $FEA_SERVER and /local/scratch/$USER $VIZ_SERVER and /division/... Local/remote directories references: The 'ManyDirs' button --- or the 'df -m' command on the host --- or the 'Show FILE-SYSTEM-SIZES @AnyHost (%-USED SORT)' drawer in the HandyTools-SpaceTools toolchest. ---------- THE OUTPUT (and its usage): This utility shows the DISK USAGE of EACH OF THE FILES in the specified directory (but NOT files in SUB-directories of that directory). This utility also shows the ownership & permissions of EACH OF THE FILES in the specified directory. This *AGE-SORTED* (oldest first) file-size information is especially helpful, when a file system is near-full, to locate files (and their 'owners') that will yield the most pay-back if the files could be REMOVED or COMPRESSED or ARCHIVED 'off-line'. AGE-SORTED lists are especially helpful to people NOT FAMILIAR WITH the history/source/nature of the files in the specified directory. -------- THE GUI: Note that you can expand this window to enter-and-see the full directory name. You can do queries on multiple hosts/directories and bring the report windows side-by-side for comparison. --- This GUI is presented by the 'wrapper' script $0 This 'wrapper' script also performs the report generation --- with 'ls -altr' & 'awk' commands (also 'tail', 'grep', 'head', 'bc', 'cut') --- and then shows the report." export WIN_INFO ## WIN_GEOM="+40+40" ## export WIN_GEOM ## WIN_COLOR="#AAAAAA" ## export WIN_COLOR ################################ ## SET DEFAULT HOST & DIR NAMES. ################################ ## HOSTS_LIST="`hostname` engfea00 engvis00 engprd00 ews###" ## export HOSTS_LIST if test "$HOST_ID" = "" then # HOST_ID="$SERVER" HOST_ID="`hostname`" fi export HOST_ID if test "$DIRNAME" = "" then # DIRNAME="$HOME" # DIRNAME="/local/scratch" DIRNAME="/local/scratch/$USER" fi export DIRNAME ############################################################################## ## START OF PROMPTING LOOP -- for directory name. (Not implemented.) ############################################################################## ## # while true ## while : ## do ################################ ## PROMPT FOR HOST & DIR NAMES. ################################ # TEMP=`$FEDIR/tkGUIs/enter_hostid_and_dir.tk` TEMP=`$FEDIR/tkGUIs/enter_hostid_and_dir_toghelp-scroll.tk` eval "$TEMP" ## FOR TESTING: # echo $TEMP if test "$HOST_ID" = "" then exit fi if test "$DIRNAME" = "" then exit fi ##################################################################### ## CHECK THAT THE HOST_ID EXISTS, if not THISHOST. ##################################################################### ## FOR TESTING: # set -x if test ! "$HOST_ID" = "$THISHOST" then HOSTCHECK=`rsh $HOST_ID cd 2>&1 | grep 'Unknown host'` if test ! "$HOSTCHECK" = "" then CONFIRM_TEXT="\ Specified Host: $HOST_ID is Unknown. " export CONFIRM_TEXT CONFIRM_GEOM="+080+080" CONFIRM_MINSIZE="350 150" CONFIRM_TITLE="$WINTITLE" export CONFIRM_TITLE CONFIRM_MINSIZE CONFIRM_GEOM CONFIRM_COLOR="#3DADFF" export CONFIRM_COLOR CONFIRM_FONT="-adobe-helvetica-bold-r-normal--*-100-*-*-p-*-*-*" export CONFIRM_FONT JUNK=`$FEDIR/tkGUIs/confirm.tk DISMISS` & exit ## continue fi ## END OF if test ! "$HOSTCHECK" = "" fi ## END OF if test ! "$HOST_ID" = "$THISHOST" ##################################################################### ## DIRECTORY CHECKs. ##################################################################### ## CHECK THAT THE DIRNAME IS ACCESSIBLE/EXISTS ## -- on $HOST_ID (do local or remote check). ##################################################################### ## For a slightly different technique of handling 'stdout & stderr' ## (with 2>&1), see $FEDIR/scripts/find_big_or_old_files4dir_bygui ##################################################################### ## FOR TESTING: # set -x if test "$HOST_ID" = "$THISHOST" then DIRCHECK=`ls -d $DIRNAME 2> /dev/null` else . $FEDIR/scripts/mak_rhosts DIRCHECK=`rsh $HOST_ID ls -d $DIRNAME 2> /dev/null` fi if test "$DIRCHECK" = "" then CONFIRM_TEXT="\ Specified Directory: $DIRNAME Not found or does not exist, according to $HOST_ID. Exiting. " export CONFIRM_TEXT CONFIRM_GEOM="+080+080" CONFIRM_MINSIZE="350 150" CONFIRM_TITLE="$WINTITLE" export CONFIRM_TITLE CONFIRM_MINSIZE CONFIRM_GEOM CONFIRM_COLOR="#3DADFF" export CONFIRM_COLOR CONFIRM_FONT="-adobe-helvetica-bold-r-normal--*-100-*-*-p-*-*-*" export CONFIRM_FONT JUNK=`$FEDIR/tkGUIs/confirm.tk DISMISS` & exit ## continue fi ## END OF if test "$DIRCHECK" = "" ########################################################### ## EXIT IF USER SPECIFIES '/' for the directory. ########################################################### ## if test "$DIRNAME" = "/" ## then ## echo " ## Try a lower level directory than the root directory (/). ## ## *** Press Enter to continue." ## read NNS_JUNK ## ## continue ## # exit ## fi ########################################################### ########################################################################## ## PUT THE REPORT INFO (from ls -altr) INTO AN IN-MEMORY VARIABLE, $OUT. ########################################################################## ## ISSUE THE 'ls -altr' COMMAND -- LOCALLY OR REMOTELY. ########################################################################## ## The first line ('total ###') will be used to convert the number ### ## from 512-byte blocks to Megabtyes and display in the heading of the ## report. ## The remaining lines will be sorted and reformated for the report body. ########################################################################## ## THISHOST=`hostname` ## WAS SET ABOVE. if test "$HOST_ID" = "$THISHOST" then ## FOR TESTING: # set -x OUT=`ls -altr $DIRNAME` ## FOR TESTING: # set - else ############################################ ## MAKE .rhosts FILE FOR THE USER, if needed. ############################################ . $FEDIR/scripts/mak_rhosts ######################################################################## ## 'rsh' to $HOST_ID to do 'ls -al'. ######################################################################## ## FOR TESTING: # set -x OUT=`rsh $HOST_ID ls -altr $DIRNAME` ## FOR TESTING: # set - ## For security purposes, could (re)move the .rhosts file. ## . $FEDIR/scripts/mv_rhosts fi ## END OF if test "$HOST_ID" = "$THISHOST" ########################################################################## ## CALCULATE TOTAL SIZE OF THE DIRECTORY FROM ## the first line ('total ###'). ########################################################################## ## Convert the number ### from 512-byte blocks to ## Megabtyes and display in the heading of the report. ## (1048576 = 1024 * 1024) ########################################################################## BLOCKS=`echo "$OUT" | head -1 | cut -d" " -f2` TOTMEG=`echo "scale=3; $BLOCKS * 512 / 1048576" | bc` ########################################################################## ## SET OUTPUT FILENAME, local if possible. ########################################################################## . $FEDIR/scripts/set_localoutlist ################################################################## ## SET REPORT HEADING for size of files and other file info. ################################################################## echo "\ ..................... `date '+%Y %b %d %a %T%p'` ............................ DISK USAGE OF FILES (& directory 'indexes') IN DIRECTORY: ${HOST_ID}:$DIRNAME at *ONE* level under this directory, NOT all levels. SORTED BY *AGE* --- OLDEST FILES (and links) AT THE TOP. DIRECTORIES ARE BELOW THE FILES (and links). This report was generated by running the 'ls -altr' command on $HOST_ID . TOTAL SPACE used by files at this directory level is $BLOCKS 512-byte blocks --- about $TOTMEG Megabytes. AGE-SORT ************* Disk usage File-type & Last-Modified Filename or link (MegaBytes) Permissions Owner Group Date-Time/Yr (with embedded blanks, if any) ------------- ----------- -------- -------- ------------ ---------------------- GigMeg.KilByt | | | | " > $OUTLIST ########################################################################## ## GENERATE REPORT BODY --- from contents saved in $OUT. ########################################################################## ## NOTE on 'ls -l' format: ## In 'ls -l' output, if the file is not more than a year old, ## $1=perms $3=userid $5=bytes $6=mmm, $7=dd $8=hrs:min $NF=filename ## ## For a file more than a year old, $8=yyyy, where yyyy denotes year. ########################################################################## ## NOTE on filenames with embedded spaces and 'awk': ## ## To handle filenames with embedded spaces, we use 'substr($0,COLmonth + 12)' ## in awk, instead of '$9' --- where ## we set COLmonth with the expression 'COLmonth = index($0,$6)'. ## ## The 'substr($0,COLmonth + 12)' gets the substring of the record, $0, ## starting at beginning of filename and going to the end of the record. ## ## This is not elegant, because when the contents of field $6 matches ## a subsring of one of the fields, $1 thru $5, some extra chars before ## the filename may be printed. But it is almost impossible that ## a 3-char month name, like 'Apr', will be in fields $1 thru $5. ## ## Have not been able to find an 'ideal' method to extract the filename ## with 'awk'. Would be nice if we could 'index' fields like $9 to get, ## with no exceptions, the column number of its starting character. ## ######################################################################### ## FOR TESTING: # set -x ######################################################### ## THIS FORM MIXES directories with regular-files and links. # # echo "$OUT" | tail +2 | \ # awk '{printf ("%13.6f %-10s %-8s %-8s %-3s %2s %5s %s\n", $5/1000000, $1, $3, $4, $6, $7, $8, $9 )}' \ # >> $OUTLIST ######################################################### ######################################################### ## THIS FORM DOES NOT SHOW links (the arrow and object of the link). # # echo "$OUT" | tail +2 | grep -v '^d' | \ # awk '{printf ("%13.6f %-10s %-8s %-8s %-3s %2s %5s %s\n", $5/1000000, $1, $3, $4, $6, $7, $8, $9 )}' \ # >> $OUTLIST ######################################################### ############################################ ## Add NON-directories --- i.e. ## FILES (and LINKS, etc.) --- to the report. ############################################ echo "$OUT" | tail +2 | grep -v '^d' | \ awk '{ COLmonth = index($0,$6) ; \ printf ("%13.6f %-10s %-8s %-8s %-3s %2s %5s %s\n", $5/1000000, $1, $3, $4, $6, $7, $8, substr($0,COLmonth + 12) )}' \ >> $OUTLIST ############################################ ## Add a separator line --- between ## FILES and DIRECTORIES. ############################################ echo " Directories:" >> $OUTLIST ######################################### ## Add DIRECTORIES to the report. ######################################### echo "$OUT" | tail +2 | grep '^d' | \ awk '{ COLmonth = index($0,$6) ; \ printf ("%13.6f %-10s %-8s %-8s %-3s %2s %5s %s\n", $5/1000000, $1, $3, $4, $6, $7, $8, substr($0,COLmonth + 12) )}' \ >> $OUTLIST ## FOR TESTING: # set - ######################################################################## ## Add TRAILER to report. ######################################################################## echo " | | | | GigMeg.KilByt ------------- ----------- -------- -------- ------------ ---------------------- (MegaBytes) File-type & Owner Group Date-Time/Yr Filenamee or link Disk usage Permissions Last-Modified (with embedded blanks, if any) ************* AGE-SORT ..................... `date '+%Y %b %d %a %T%p'` ............................ The output above was generated by the script $0 which ran the 'ls -altr' command on host $HOST_ID . ----------------- PROCESSING METHOD: The script uses a 'pipe' of commands (ls, tail, grep, awk) like: ls -altr | tail +2 | grep -v '^d' | awk '{ ... print ... }' and ls -altr | tail +2 | grep '^d' | awk '{ ... print ... }' ------------ FEATURE NOTE: This utility provides formatting - bytes-to-MegaBytes conversion, - directories separated from other files, - heading/trailer info, - fixed-width columns that is not available with only the 'ls' command. -------------------- SUB-DIRECTORIES NOTE: A line that starts with 'd' in the permissions string shows the size of the 'index' (i.e. table of contents) of a directory -- NOT the size of all the files and subdirectories under that directory. ---------------------- IMPLEMENTATION METHODS: This utility script can be run via a drawer in a 'spacetools' toolchest command-utility --- via a drawer with a name like 'Show FILE-SIZES 4aDir@AnyHost (ONE level, AGE-SORT). In particuar, this script is available via the site SGI toolchest-drawers hierarchy: SGI Toolchest -> HandyTools -> AnyHost - SpaceTools (Files) -> Show FILE-SIZES 4aDir@AnyHost (ONE level,AGE-SORT) OR you can implement the script (or 'spacetools') as - a command alias, via your $HOME/.profile file; - a desktop icon, via the 'Find, File QuickFind' tool drawers; - a drawer in the SGI toolchest, via your $HOME/.auxchestrc file. ........................................................................ " >> $OUTLIST ######################################################################## ## SHOW REPORT, with option to print. ######################################################################## . $FEDIR/scripts/shofil $OUTLIST ## done ######################################################################## ## END OF PROMPTING LOOP, FOR DIRECTORY NAME. (Not implemented.) ######################################################################## ## RETURN TO DIRECTORY PROMPT. ########################################################################