#!/bin/ksh ## ## SCRIPT NAME: find_files4filetype ## ## Where: in $FEDIR/scripts where $FEDIR=/apps/nns_com/fea ## ############################################################################## ## ## PURPOSE: For a specified directory residing-on (or known/mounted-to) the ## host on which this script runs, this script creates a list of ## files whose type contains a specified keyword, like 'executable' ## or 'text' or 'data'. ## ## The list has the 'basename' of the selected files separated into ## column 1 --- so that the list can be sorted by 'basename'. ## ## One example of use of this utility (and sort method) is QUICKLY ## finding copies of the same-named or similarly-named executables ## in scattered sub-directories of a given directory. ## ## See the 'Call Format' section below. ############################################################################## ## MAIN/ORIGINAL INTENT: ## The main intent is to list duplicate executables ## in user-department/project directories (like /data/e46) ## on a 'File Server' like 'engprd00'. ## ############################################################################## ## ## CALLED BY: 'find_files4filetype_bygui' ## in $FEDIR/scripts ## which is called by an option in the SpaceTools toolchest ## invoked by 'spacetools' in $FEDIR/scripts. ## ## May be called by nnsFEAmenu option 'u f ??', i.e. ## feamain -> feautils.menu -> fileman.menu ## ## Also can be called manually, for example --- ## via login to 'engprd00'. ## ## Could be put in a 'ideasadm' cron job (in /apps/ideas/cron) ## or in a 'caeadm' cron job (in /usr/people/caeadm/cron). ## ############################################################################## ## CALL FORMAT (examples): See 'find_files4filetype_bygui'. ## ## $FEDIR/scripts/set_localoutlist ## $FEDIR/scripts/find_files4filetype /data/e46 executable > $OUTLIST ## xpg $OUTLIST ## #### where the six positional arguments are used to set vars: #### TOPdir fileTYPE ## ## OR ('rsh' form) ## $FEDIR/scripts/set_localoutlist ## rsh engprd00 $FEDIR/scripts/find_files4filetype \ ## /data/e46 executable > $OUTLIST ## xpg $OUTLIST ## ############################################################################## ## POSSIBLE ENHANCEMENTS FOR MORE AUTOMATION: ## ## 1a) With some userid selecting logic (and mail-address-finding/checking ## logic), this script could automatically generate e-mail ## to users who have many duplicate files in scattered sub-directories ## under a given directory. ## ## 1b) Or the e-mail could be sent to 'ideasadm' or 'caeadm' or 'root' userids, ## or specific Administrator ids (like 'bmo01' or 'tkj00') ## for manual notification of the appropriate users. The notification ## of 'userids' could be done with a userid/to/mail-address lookup aid, ## like a lookup aid in the 'mailtools' utility-cmd in $FEDIR/scripts. ## ########################################################################### ## 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 24Jul2000 Based on 'find_big_or_old_files4dir' ## in $FEDIR/scripts ## ## Updated by: Blaise Montandon 08apr2004 To avoid a bug in new SGI 'sort' ## in IRIX 6.5.22, changed several cases ## of 'sort -k5nr' to 'sort +4 -5nr' ## and 'sort -k3nr' to 'sort +2 -3nr'. ############################################################################## if test "$FEDIR" = "" then FEDIR="/apps/nns_com/fea" fi HOST_ID=`hostname` ############################################################################## ## SET PARAMETERS TO BE USED BELOW. ############################################################################## ## NOTE: Defaults could be set for $1, $2 so that the script ## can be tested quickly with no input parms. ############################################################################## ## FOR TESTING: # set -x TOPdir="$1" if test "$TOPdir" = "" then # TOPdir="/data/e46" # TOPdir="/data/e46/Lessard" TOPdir="/data/e46/minerw/alt_ins3d" # exit fi ######################################################################## ## If the directory $DIRNAME is NSF-mounted (rather than local), ## show a message on possible missing data (due to local-parm on 'find' ## command) --- and probable slow performance --- i.e. do the ## query on a remote host-server. ######################################################################## NFSCHECK=`stat $TOPdir | grep nfs` if test ! "$NFSCHECK" = "" then CONFIRM_TEXT="\ Specified Directory: $TOPdir is not local to this machine, $HOST_ID. Try the query on a host/server for which the directory is local. " export CONFIRM_TEXT CONFIRM_GEOM="+080+080" CONFIRM_MINSIZE="450 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 fi ############################################# ## Figure out the num of dir-levels in $TOPdir. ############################################# # TOPdirlevs=`echo "$TOPdir" | sed 's|/| |g' | wc -w` ############################################# ## SET THE 'fileTYPE' var. ############################################# fileTYPE="$2" if test "$fileTYPE" = "" then fileTYPE="executable" # fileTYPE="text" # fileTYPE="data" # exit fi ############################################################################ ## NOTE ON 'ls -l' OUTPUT: ############################################################################ ## In 'ls -l' output, if the file is NOT more than a year old, ## $1=perms $3=userid $5=bytes $6,$7=mmdd $8=hrs:min $NF=filename ############################################################################ ## In 'ls -l' output, if the file IS more than a year old, ## $1=perms $3=userid $5=bytes $6,$7=mmdd $8=year $NF=filename ############################################################################ ############################################################################ # REFMT_LS_CMD="eval awk '{printf (\"%10s %8s %12s %3s %2s %5s %s\n\", \$1, \$3, \$5, \$6, \$7, \$8, \$NF)}'" ## WORKS, but blanks in quoted format are compressed: ## eval awk '{printf ("%10s %8s %12s %3s %2s %5s %s ## ", $1, $3, $5, $6, $7, $8, $NF)}' ############################################################################ ## REFMT_LS_CMD="awk '{printf (\"%10s %8s %12s %3s %2s %5s %s\n\", \$1, \$3, \$5, \$6, \$7, \$8, \$NF)}'" ## DOES NOT WORK: ## awk '{printf ("%10s %8s %12s %3s %2s %5s %s ## ", $1, $3, $5, $6, $7, $8, $NF)}' ############################################################################ ## REFMT_LS_CMD='awk ''{printf ("%10s %8s %12s %3s %2s %5s %s\n", $1, $3, $5, $6, $7, $8, $NF)}''' ## DOES NOT WORK: ## awk {printf ("%10s %8s %12s %3s %2s %5s %s ## ", $1, $3, $5, $6, $7, $8, $NF)} ############################################################################ # REFMT_LS_CMD="cut -c1-10,16-24,31-" # REFMT_LS_CMD="eval awk '{print \$1 \" \" \$3 \" \" \$5 \" \" \$6 \" \" \$7 \" \" \$8 \" \" \$NF}'" # REFMT_LS_CMD="eval awk '{printf (\"%-13s %-8s %12s %3s %2s %5s %s\n\", \$1, \$3, \$5, \$6, \$7, \$8, \$NF)}'" # REFMT_LS_CMD="eval awk '{printf (\"%-13s %-8s %13.6f %3s %2s %5s %s\n\", \$1, \$3, \$5/1000000, \$6, \$7, \$8, \$NF)}'" REFMT_LS_CMD="eval awk '{printf (\"%-13s %-8s %13.6f %3s %2s %5s\n\", \$1, \$3, \$5/1000000, \$6, \$7, \$8)}'" LS_SORT_PREAWK="sort +4 -5nr" # LS_SORT_PREAWK="sort -k5nr" LS_SORT_POSTAWK="sort +2 -3nr" # LS_SORT_POSTAWK="sort -k3nr" ## FOR TESTING: # echo $REFMT_LS_CMD # REFMT_LS_HEADING=" # Owner File-Size # Permissions Userid GigMegKilByt Date-Time Filename # ---------- -------- | | | | ------------ ------------------------------ # " REFMT_LS_HEADING=" Owner FileSize(Meg) File Basename Permissions Userid GigMeg.KilByt Date-Time Directory ---------------- ---------- -------- | | | | ------------ ------------------------------ " ######################################################################### ## SET REPORT FILENAME IN $OUTLIST. ######################################################################### . $FEDIR/scripts/set_localoutlist UNSORTEDLIST=${OUTLIST}_unsorted ######################################################################### ## PREP REPORT HEADING. ######################################################################### echo " ******************************** `date '+%Y %b %d %a %T%p %Z'` ****************** FILES WHICH ARE OF FILETYPE ' $fileTYPE ', IN THE VARIOUS SUB-DIRECTORIES UNDER ${HOST_ID}:$TOPdir : (FILES BASENAME-SORTED) $REFMT_LS_HEADING" > $OUTLIST ######################################################################### ## COLLECT FULLY-QUALIFIED FILENAMES FOR FILES WHICH MATCH $fileTYPE, ## AT ANY LEVEL UNDER THE UNDER GIVEN $TOPdir: ######################################################################### ## FOR TESTING: set -x # find $TOPdir -local -type f -exec file {} \; | grep "$fileTYPE" >> $OUTLIST FILELIST=`find $TOPdir -local -type f -exec file {} \; | grep "$fileTYPE" | \ cut -d: -f1` ######################################################################### ## COLLECT 'ls -l' INFO FOR THE COLLECTED FULLY-QUALIFIED FILENAMES. ######################################################################### ## FOR TESTING: # set -x for FILE in $FILELIST do BASENAME=`basename $FILE` DIRNAME=`dirname $FILE` FILEINFO=`ls -l $FILE | awk '{printf ("%-13s %-8s %13.6f %3s %2s %5s\n", $1, $3, $5/1000000, $6, $7, $8)}'"` # echo "$BASENAME $DIRNAME `ls -l $FILE | $REFMT_LS_CMD`" BASENAME=`echo "$BASENAME " | cut -c1-16` echo "$BASENAME $FILEINFO $DIRNAME" >> $UNSORTEDLIST done sort -k1 $UNSORTEDLIST >> $OUTLIST ## FOR TESTING: # echo "FindRetCode: $?" ## FOR TESTING: # set - echo " ******************************** `date '+%Y %b %d %a %T%p %Z'` ****************** " >> $OUTLIST xpg $OUTLIST