#!/bin/ksh ## ## SCRIPT NAME: findfils4type_bygui ## ## Where: in $FEDIR/scripts where $FEDIR=/apps/nns_com/fea ## ############################################################################## ## PURPOSE: ## Finds files for a given FILE-TYPE and for a HOST:DIRECTORY ## 'start point'. Looks through all the sub-directories --- for ## files that are LOCAL to the specified host. ## ## More specifically, this script creates a list of files whose ## type-string (given by the 'file' command) ## contains a specified keyword, ## like 'executable' or 'text' or ## 'data' (i.e. 'binary', not-text-only data). ## ## An example application is looking for duplicate executables ## scattered through directories. ## ------------------------------------------------------------------------- ## ## NOTE1: There is another utility, 'findfils4type_basename_bygui', in which ## the listing has the 'basename' of the selected files separated into ## column 1 --- so that the list can be sorted by 'basename'. ## ## For example, that can be used to look for duplicate executables ## (or similar executable names --- similar prefixes) ## scattered among sub-directories of a given directory. ## ## THIS UTILITY IS RELATIVELY QUICKER. IT SIMPLY LISTS THE ## FILE NAMES (without extracting 'basename' and without adding ## 'ls -l' and 'file' info). ## ## ------------------------------------------------------------------------- ## ## NOTE2: This listing could be unsorted for speedier query completion. ## But a sort has been added to sort the list by fully-qualified ## filename --- since the user will probably, in many cases, wish ## that the list were sorted, and would be willing to wait a little ## for the computer-sort to save on slow-human search time. ## ############################################################################## ## IMPLEMENTATION NOTE: This script is similar to 'findfils4mask_bygui'. ## --- a search by filename-mask instead of filetype. ############################################################################## ## ## CALLED BY: 'findtools' ## in $FEDIR/scripts ## ## May eventually be called by nnsFEAmenu option 'u f ??', i.e. ## feamain -> feautils.menu -> fileman.menu ## ## See implementation notes in 'findtools'. ########################################################################### ## NOTE: You can see only the non-commented, executable lines of this ## script by using ## egrep -v '^ *##|^ *# ' ## or ## grep -v '^ *##' | grep -v '^ *# ' ############################################################################## ## MAINTENANCE HISTORY: ## Written by: B.Montandon O06 24Aug2000 Based on preliminary script ## 'findfils4type_info_bygui' ## in $FEDIR/scripts ## which extracts file basename and sorts on that. ## Updated by: Blaise Montandon C61 24Aug2000 Add HOST_ID found check; ## Fix DIRCHECK to do on $HOST_ID, ## not THISHOST. ## Updated by: Blaise Montandon O06 15Sep2000 Allow /CDROM dir to be excepted ## from check for NFS-mounted dirs. ## Updated by: Blaise Montandon O06 6oct2000 Partially improve file format ## for variety of filename lengths. ## Improve help message. ## Updated by: Blaise Montandon O06 10oct2000 Touch up report info. ## Updated by: Blaise Montandon 26mar2002 Update 'IMPLEMENTATION' info at the ## bottom of this script. ## ## Updated by: Blaise Montandon 29apr2003 Replace Tk-GUI script ## 'enter_hostid_dir_string.tk' by ## 'enter_hostid_dir_string_toghelp-scroll.tk'. ## ## Updated by: Blaise Montandon 04mar2004 Added ':.*' in grep for $STRING --- ## to grep only on chars after ':' ## in the 'file {}' output. ############################################################################## ############################################################################## ## SET APPS DIRECTORY NAME --- for utility scripts, if any --- ## in case this script is not called from within nnsFEAmenu script system. ############################################################################## if test "$FEDIR" = "" then FEDIR="/apps/nns_com/fea" fi ############################################################################## ## SET MAIN SERVER NAME --- used in window messages only. ## (The user sets the host where the directory is actually queried.) ############################################################################## SERVER="engprd00" ############################################################################## ## SET LOCAL HOST NAME FOR MESSAGES & REPORTS & LOCAL-HOST-CHECKS. ############################################################################## THISHOST=`hostname` ############################################################################## ## SET LIMIT ON NUMBER OF FILES FOR WHICH FILE INFO IS FOUND, ## which can be slow processing, at one command issued per file. ############################################################################## ## FILELIM=10 ## FOR TESTING. FILELIM=2000 ######################################################################## ## INITIALIZE VARS FOR PROMPTING LOOP TO ## GET HOSTID and DIRECTORY NAME and STRING (FILE-TYPE [SUB]STRING). ######################################################################## WINTITLE=\ "Files_under_Host:Dirname (ALL dir levels) -- OF A GIVEN FILE-TYPE" export WINTITLE WIN_INFO="\ Enter (1) the name of an SGI HOST on the NNS network and (2) a DIRECTORY NAME. (3) At the STRING prompt, enter a file-type or portion thereof (explained below). -------- This utility shows the FILENAMES under the specified host:directory --- in ALL SUB-directories of that directory. It shows filenames that match the SPECIFIED FILE TYPE, as determined by the Unix 'file' command. BASICALLY, THERE ARE THREE FILE-TYPES: text, executable, and 'data'. THE LIST simply shows FILENAME (FULLY-QUALIFIED) and FILE-TYPE. The list is SORTED by fully-qualified filename. -------- For fast performance, it may be BEST TO SPECIFY THE HOST AT WHICH THE DIRECTORY RESIDES. Reference: The 'ManyDirs' button. IN FACT, THIS UTILITY RESTRICTS ITS ATTENTION TO FILES 'LOCAL TO' THE SPECIFIED HOST --- for better performance and to reduce likelihood of following (unintentionally) links to huge directories that are mounted from another (remote) host --- like ${SERVER}:/usr/people. -------- Some example file-type strings to use for matching follow. TEXT: 'text' OR 'ascii text' OR 'c program text' OR 'English text' OR 'commands text' OR 'c program text with garbage' EXE'S: 'executable' OR 'ELF N32 MSB mips-4 dynamic executable ...' DATA: 'data' OR 'image' OR 'SGI imagelib' OR 'GIF' OR 'JPEG' OR 'compressed' OR 'compressed data' OR 'tar' Note: When the 'file' command returns 'data', it means the file contains 'binary' (NOT text-only) data, like numbers in easily-machine-readable, not easily-human-readable, format. Directories: This utility could be used to search directories like your home directory --- ${SERVER}:$HOME OR a local scratch directory --- ${THISHOST}:/local/scratch/$USER OR a /data sub-directory --- ${SERVER}:/data/subs/cae/$USER OR ${SERVER}:/data/e46 OR ${SERVER}:/data/foundry OR ${SERVER}:/data/cvn##/ideas/team OR ${SERVER}:/data/cvn## . -------- 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. For moderate sized directories, this report is typically generated in 10 to 30 seconds. If there are MANY sub-directories & files, IT MAY TAKE MORE THAN A MINUTE OR TWO. USE THIS UTILITY SPARINGLY ON BIG DIRECTORIES, like ${SERVER}:/usr/people. You can use the 'top' processes utility (of the PerfTools toolchest) to see the 'find' process running, in the case of a very long search." export WIN_INFO ## WIN_GEOM="+40+40" ## export WIN_GEOM ## ## WIN_MINWIDTH1="620" ## export WIN_MINWIDTH1 ## ## WIN_MINHEIGHT1="130" ## export WIN_MINHEIGHT1 ## ## WIN_COLOR="#AAAAAA" ## export WIN_COLOR ################################ ## SET DEFAULT HOST & DIR NAMES. ################################ HOST_ID="`hostname`" # HOST_ID="engprd00" export HOST_ID # DIRNAME="$HOME" DIRNAME="/local/scratch" # DIRNAME="$HOME/nsmail" export DIRNAME STRING="text" export STRING ############################################################################## ## PROMPTING LOOP -- for host:directory name. ############################################################################## ## # while true while : do # TEMP=`$FEDIR/tkGUIs/enter_hostid_dir_string.tk` TEMP=`$FEDIR/tkGUIs/enter_hostid_dir_string_toghelp-scroll.tk` eval "$TEMP" ## FOR TESTING: # echo $TEMP if test "$HOST_ID" = "" then exit fi if test "$DIRNAME" = "" then exit fi if test "$STRING" = "" then exit fi ##################################################################### ## CHECK THAT THE HOST_ID EXISTS. ##################################################################### ## FOR TESTING: # set -x 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 ## break continue fi ##################################################################### ## CHECK THAT THE DIRNAME IS ACCESSIBLE/EXISTS. ##################################################################### ## 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. " 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 ########################################################### ## 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 ################################### ######################################################################## ## If the directory $DIRNAME is NSF-mounted on host $HOST_ID, ## show a message on possible performance improvement in doing the ## query on a remote host-server. ######################################################################## ## THISHOST=`hostname` ## WAS SET ABOVE. if test "$HOST_ID" = "$THISHOST" then NFSCHECK=`stat $DIRNAME | grep 'st_fstype: nfs'` else . $FEDIR/scripts/mak_rhosts NFSCHECK=`rsh $HOST_ID stat $DIRNAME | grep 'st_fstype: nfs'` fi if test \( "$NFSCHECK" != "" -a "$DIRNAME" != "/CDROM" \) then CONFIRM_TEXT="\ The directory name that you specified $DIRNAME is NFS-mounted to the host you specified, $HOST_ID, i.e. the directory is remote from the specified host, NOT 'local to' it. USE A HOST FOR WHICH THE DIRECTORY IS IN A 'LOCAL' FILE SYSTEM. I.E. USE A HOST THAT IS SERVING-OUT THE DIRECTORY. " export CONFIRM_TEXT CONFIRM_GEOM="+080+080" CONFIRM_MINSIZE="550 250" CONFIRM_TITLE="$WINTITLE" export CONFIRM_TITLE CONFIRM_MINSIZE CONFIRM_GEOM CONFIRM_COLOR="#3DADFF" export CONFIRM_COLOR ## GOOD FIXED-WIDTH CANDIDATES (for env var CONFIRM_FONT): # set TEXT_FONT "-adobe-courier-bold-r-normal--*-120-*-*-m-92-*-*" # set TEXT_FONT "-adobe-courier-bold-r-normal--*-100-*-*-m-90-*-*" # set TEXT_FONT "-adobe-courier-medium-r-normal--*-100-*-*-m-90-*-*" # set TEXT_FONT "-misc-fixed-bold-r-normal--*-120-*-*-c-70-*-*" # set TEXT_FONT "-misc-fixed-bold-r-normal--*-120-*-*-c-80-*-*" # set TEXT_FONT "-misc-fixed-bold-r-normal--*-120-*-*-c-90-*-*" # set TEXT_FONT "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-m-80-*-*" # set TEXT_FONT "-schumacher-clean-bold-r-normal--*-130-*-*-c-80-*-*" ## GOOD VARIABLE-WIDTH CANDIDATES (for env var CONFIRM_FONT): # set TEXT_FONT "-adobe-helvetica-bold-r-normal--*-100-*-*-p-82-*-*" # set TEXT_FONT "-adobe-times-bold-r-normal--*-100-*-*-p-76-*-*" # set TEXT_FONT "-adobe-palatino-bold-r-normal--*-100-*-*-p-81-*-*" # set TEXT_FONT "-adobe-new century schoolbook-bold-r-normal--*-100-*-*-p-87-*-*" # set TEXT_FONT "-adobe-utopia-regular-r-normal--*-100-*-*-p-75-*-*" # set TEXT_FONT "-adobe-utopia-medium-r-normal--*-100-*-*-p-75-*-*" CONFIRM_FONT="-adobe-helvetica-bold-r-normal--*-100-*-*-p-*-*-*" export CONFIRM_FONT JUNK=`$FEDIR/tkGUIs/confirm.tk DISMISS` & # exit continue fi ##################################################################### ## SET A FILENAME FOR REPORT OUTPUT, in $OUTLIST. ##################################################################### . $FEDIR/scripts/set_localoutlist rm -f $OUTLIST ##################################################################### ## PREPARE REPORT HEADING, in $OUTLIST. ##################################################################### echo "\ ............................. `date '+%Y %b %d %a %T%p %Z'` ................. LISTING OF FILES WHOSE 'file'-TYPE INCLUDES THE STRING: $STRING AT ALL LEVELS, UNDER THE DIRECTORY $DIRNAME - SORTED BY FULLY-QUALIFIED FILENAME - ( ONLY FILES ON DISK *LOCAL TO* HOST $HOST_ID ARE LISTED.) The 'find' processing was run on Host: $HOST_ID The query was initiated from Host: $THISHOST .............................................................................................................................. Filename : File-type ------------------------------- " > $OUTLIST ######################################################################### ## SET $CMD to 'grep' each LOCAL file under directory $DIRNAME ## for the string $STRING, in its type, given by the 'file' command. ## To be used with or without 'rsh' below. ######################################################################### ## WORKS, without sort. # CMD="find $DIRNAME -local -type f -exec file {} \; | grep \"$STRING\"" ## WORKS, with sort. # CMD="find $DIRNAME -local -type f -exec file {} \; | \ # grep \"$STRING\" | sort -k1" ## WORKS, with sort and ':.*' in grep (to grep only on chars after ':'). CMD="find $DIRNAME -local -type f -exec file {} \; | \ grep \":.*$STRING\" | sort -k1" ## WORKS, but needs better & more complex awk formatting logic --- ## to get the file-type info to line up nicely. ## ## The 'sed' replaces any tab-char with 4 spaces. ## # CMD="find $DIRNAME -local -type f -exec file {} \; | \ # grep \"$STRING\" | sort -k1 | sed 's/\ / /g' | \ # awk -F: '{printf (\"%-70s %s \n\", \$1, \$2)}'" # awk -F: '{printf (\"%s %s \n\", \$1, \$2)}'" ## The following is ## NOT A GOOD METHOD cause file-type field has multiple words. ## CMD="find $DIRNAME -local -type f -exec file {} \; | grep \"$STRING\" | \ ## awk '{printf (\"%-68s %s %s %s %s %s\n\", \$1, \$2, \$3, \$4, \$5)}'" ########################################################################### ## PERFORM THE QUERY -- on the specified host. ## ISSUE $CMD LOCALLY OR WITH 'rsh', DEPENDING ON WHETHER THE ## USER-SELECTED HOST, $HOST_ID, IS $THISHOST OR ANOTHER. ########################################################################### ## FOR TESTING: # set -x if test "$HOST_ID" = "$THISHOST" then eval "$CMD" >> $OUTLIST ######################################################################### ## AN ALTERNATIVE TO CONSIDER: ######################################################################### ## FILELIST=`find $DIRNAME -local -type f -exec file {} \; | \ ## grep "$STRING" | cut -d: -f1` ######################################################################### else . $FEDIR/scripts/mak_rhosts ######################################################################### ## eval "rsh $HOST_ID $CMD" >> $OUTLIST ######################################################################### ## DOES NOT WORK. Gives 'UX:find: ERROR: incomplete statement' msg. ######################################################################### rsh $HOST_ID "$CMD" >> $OUTLIST fi ## FOR TESTING: # set - ######################################################################## ## Add TRAILER to report. ######################################################################## echo " ............................. `date '+%Y %b %d %a %T%p %Z'` ................. The output above was generated by the script $0 which ran the 'find' command on host $HOST_ID . --------------------------------------- HOW THE FILES WERE SELECTED & FORMATTED: --------------------------------------- The Unix 'find' command was used to travel through the sub-directories of the specified directory $DIRNAME and to provide a list - with fully-qualified filenames rather than a list - with file 'basenames' and 'section' breaks providing the directory names (such as the list produced by 'ls -lR'). I.e. this list 'immediately' gives the full-filename of each file, for easy pasting into other windows. A sort is added to this utility to assure that the list is sorted by full-filename. This can be helpful with long lists. -------- Command used: $CMD Restricting the find to 'local' files on the specified host helps reduce the likelihood of following (unintentionally) links to huge directories that are mounted from another (remote) host --- like engprd00:/usr/people. ------------------------------ IMPLEMENTATION OF THIS UTILITY: ------------------------------ The 'findfils4type_bygui' script can be accessed via a drawer in a 'findtools' command-toolchest. The 'findtools' command (fullname $FEDIR/scripts/findtools) is available via the 'handytools' command-toolchest, which is available via the HandyTools drawer on the site SGI Toolchest on most SGI workstations. If you cannot access this utility by the SGI Toolchest or by simply typing 'findtools' or 'handytools' at a command prompt, you can implement the 'findfils4type_bygui' script (or 'findtools') as - a command alias, via your $HOME/.profile file. Example: alias findtools='$FEDIR/scripts/findtools' - a desktop icon, via the 'Find, File QuickFind' tool drawers; - a drawer in the SGI toolchest, via your $HOME/.auxchestrc file. ........................................................................ " >> $OUTLIST ##################################################################### ## SHOW THE REPORT, with option to print. ##################################################################### . $FEDIR/scripts/shofil $OUTLIST ######################################################################## ## RETURN TO DIRECTORY PROMPT. ######################################################################## done ######################################################################## ## END OF prompting loop, for directory name. ########################################################################