#!/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 SUB-DIRECTORIES, 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. ## ## This script is available via the site SGI toolchest-drawers hierarchy: ## ## SGI Toolchest -> ## HandyTools -> ## AnyHost - SpaceTools (Files) -> ## Show SUBDIR-SIZES 4aDir@AnyHost (ONE level,AGE-SORT) ############################################################################## ## MAINTENANCE HISTORY: ## Written by: Blaise Montandon 03sep2003 Based on script ## 'diruse_files_onelevel_agesort_bygui' ## in $FEDIR/scripts ## ## 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 " egrep '^d|^l' " in place of ## " grep '^d' " ## ## Updated by: Blaise Montandon 04sep2003 Improve the awk to show the entire ## string in the filename position, ## even when there are embedded blanks ## or a link indicated by ' -> '. ############################################################################## ############################################################################# ## 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="SUBDIRs_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' sub-directory names (not 'full' names), at *ONE* level under the specified directory --- in AGE sort order. This is a fast query. This report is typically generated within 3 seconds. --- This utility uses an 'ls' command-pipe in the form ls -altr | egrep '^d|^l' | awk '{print ... }' For completeness, the listing shows directories --- AND 'links', if any. Links are pointers ('shortcuts'). Links may point to either directories or to regular-files. ------------ PERFORMANCE: Since sub-directories 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 use): This utility shows the ownership & permissions of EACH OF THE SUB-DIRECTORIES in the specified directory --- but does NOT show sub-directories at lower levels under the specified directory. This *AGE-SORTED* (oldest first) sub-directory information can be helpful, when a file system is near-full, to locate sub-directories (or files in those sub-directories) which will yield the most pay-back if they 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' & 'grep' & 'awk' commands --- 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 ############################################################################ ## PROMPTING for host and directory name. ############################################################################ # 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 | grep '^d'` OUT=`ls -altr $DIRNAME | egrep '^d|^l'` ## FOR TESTING: # set - else ############################################ ## MAKE .rhosts FILE FOR THE USER, if needed. ############################################ . $FEDIR/scripts/mak_rhosts ######################################################################## ## 'rsh' to $HOST_ID to do 'ls -l'. ######################################################################## ## FOR TESTING: # set -x # OUT=`rsh $HOST_ID ls -altr $DIRNAME | grep '^d'` OUT=`rsh $HOST_ID ls -altr $DIRNAME | egrep '^d|^l'` ## FOR TESTING: # set - ## For security purposes, could (re)move the .rhosts file. ## . $FEDIR/scripts/mv_rhosts fi ## END OF if test "$HOST_ID" = "$THISHOST" ########################################################################## ## (Not used.) ## CALCULATE TOTAL SIZE OF THE DIRECTORY FROM ## the first line ('total ###') of the command 'ls -altr $DIRNAME'. ########################################################################## ## 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. ################################################################## echo "\ ..................... `date '+%Y %b %d %a %T%p'` ............................ SUB-DIRECTORIES AT ONE-LEVEL UNDER DIRECTORY: ${HOST_ID}:$DIRNAME SORTED BY *AGE* --- OLDEST SUB-DIRECTORIES AT THE TOP. This report was generated by running the command pipe ls -altr | egrep '^d|^l' on $HOST_ID . For completeness, this list shows directories --- AND 'links', if any. Links are pointers ('shortcuts'). Links may point to either directories or to regular-files. The disk usage of the 'index' (i.e. table of contents) of each sub-directory is shown. This is generally much, much smaller than the space occupied by all the files under each sub-directory. AGE-SORT Disk usage of ************* each Directory INDEX File-type & Last-Modified (KiloBytes) Permissions Owner Group Date-Time/Yr Filename ------------- ----------- -------- -------- ------------ ---------------------- GigMegKil.Byt | | | | " > $OUTLIST # TOTAL SPACE used by files at this directory level is # $BLOCKS 512-byte blocks --- about $TOTMEG Megabytes. ########################################################################## ## GENERATE REPORT BODY --- from contents saved in $OUT. ########################################################################## ## Source of the following info: script 'diruse_files_onelevel_sizesort_bygui' ## ## 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 awk FORM does NOT handle embedded blanks and links ## in the ending filename position. # # echo "$OUT" | tail +2 | \ # awk '{printf ("%13.3f %-10s %-8s %-8s %-3s %2s %5s %s\n", $5/1000, $1, $3, $4, $6, $7, $8, $9 )}' \ # >> $OUTLIST ######################################################### echo "$OUT" | tail +2 | \ awk '{ COLmonth = index($0,$6) ; \ printf ("%13.3f %-10s %-8s %-8s %-3s %2s %5s %s\n", $5/1000, $1, $3, $4, $6, $7, $8, substr($0,COLmonth + 12) )}' \ >> $OUTLIST ## FOR TESTING: # set - ######################################################################## ## Add TRAILER to report. ######################################################################## echo " | | | | GigMegKil.Byt ------------- ----------- -------- -------- ------------ ---------------------- (KiloBytes) File-type & Owner Group Date-Time/Yr Filename Disk usage of Permissions Last-Modified each Directory INDEX ************* AGE-SORT ..................... `date '+%Y %b %d %a %T%p'` ............................ The output above was generated by the script $0 which ran the command 'ls -altr' on host $HOST_ID . ----------------- PROCESSING METHOD: The script uses a 'pipe' of commands (ls, grep, and awk) like: ls -altr | egrep '^d|^l' | awk '{printf ( ... )}' ------------ FEATURE NOTE: This utility EXTRACTS the lines --- from the 'ls -altr' command --- that start with 'd' --- or 'l' --- in the permissions string. That is, this utility provides an extract capability (and nice columnar formatting) that is not available with only the 'ls' command. ------------------------ SUB-DIRECTORY SIZES NOTE: The sizes in this list are the size of the 'index' (table of contents) of each sub-directory -- NOT the size of all the files (and sub-directories) under that sub-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 SUBDIRs 4aDir@AnyHost (ONE level, AGE-SORT)' In particular, this script is available via the site SGI toolchest-drawers hierarchy: SGI Toolchest -> HandyTools -> AnyHost - SpaceTools (Files) -> Show SUBDIRs 4aDir@AnyHost (ONE level,AGE-SORT) OR you can implement this script (or the 'spacetools' script) 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. ########################################################################