#!/bin/ksh ## ## SCRIPT: files4dir_all_levs_namesortPERsubdir ## ## Where: in $FEDIR/scripts where $FEDIR=/apps/nns_com/fea ## ########################################################################## ## PURPOSE: For a given directory, this utility ## shows the filenames (and 'ls -l' file info) for files sorted ## by name --- within each of ALL subdirectories under the ## specified directory --- where the subdirectories are ## sorted by full-name. ## ## This avoids a list of files within a directory being interrupted ## by subdirectory names that list among the file names --- when ## the list of files is full-filenames at multiple directory levels, ## name-sorted by the full filenames. Example from utility script ## 'diruse_files_all_levs_fullnamesort_bygui': ## ## 0.000963 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/addblack ## 0.000960 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/cattojt ## 0.000975 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/check_config ## 0.000969 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/dspyserver ## 0.000969 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/dxfconvert ## 0.005576 -rwxrwxrwx root sys Aug 7 2000 /apps/eai/bin/eai_environment ## 0.000960 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/flytojt ## 0.000963 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/geotofly ## 0.000960 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/geotoiv ## 0.000992 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/help_launch_browser ## 0.000966 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/hyperhelp ## 0.000963 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/igestojt <<** NOTE break in list ## 0.250644 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/irix/addblack <<** of 'bin' subdir. ## 0.480264 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/irix/cattojt ## 0.000237 -rw-r--r-- root sys Oct 30 1998 /apps/eai/bin/irix/com/libAnnotationTree.rgs ## 0.127204 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/irix/com/libAnnotationTree.so ## 0.000234 -rw-r--r-- root sys Oct 30 1998 /apps/eai/bin/irix/com/libCADAssembly.rgs ## .... ## 0.000028 lrwxr-xr-x root sys Jan 3 00:00 /apps/eai/bin/irix/com_vv2/libeaiobjectbroker.so -> ../com/libeaiobjectbroker.so ## 0.000046 -rw-r--r-- root sys Oct 30 1998 /apps/eai/bin/irix/com_vv2/readme ## 0.451448 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/irix/dspyserver ## 2.999108 -rwxr-xr-x root sys Aug 13 1998 /apps/eai/bin/irix/dxfconvert ## 0.062472 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/irix/flytojt ## ... ## 0.304136 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/irix/wrltojt <<** NOTE continuation ## 0.000960 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/ivtofly <<** of 'bin' subdir list. ## 0.000960 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/ivtogeo ## 0.000963 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/jtappend ## 0.000978 -rwxr-xr-x root sys Oct 30 1998 /apps/eai/bin/jtappendfiles ## ########################################################################## ## CALLED BY: script 'diruse_files_all_levs_namesortINsubdirs_bygui' ## in $FEDIR/scripts, ## which is called by 'spacetools' in $FEDIR/scripts, ## actually by 'spacetools.chestdef' in $FEDIR/scripts. ## ## which is called by ## 'handytools' script in $FEDIR/scripts, ## actually by 'handytools.chestdef' in $FEDIR/scripts. ## ## See more info in 'diruse_files_all_levs_fullnamesortPERsubdir_bygui'. ########################################################################## ## MAINTENANCE HISTORY: ## Written by: Blaise Montandon 26apr2004 Based on ## 'dirnames_all_levs_groupBYlev_bygui' ## in $FEDIR/scripts ## Updated by: Blaise Montandon 26apr2004 ########################################################################### # TEST_MODE="TEST_YES" TEST_MODE="TEST_NO" DIRNAME="$1" ## FOR TESTING: # DIRNAME="/tmp" # DIRNAME="/var/tmp" if test "$DIRNAME" = "" then echo " SCRIPT $0: NO DIRECTORY NAME SPECIFIED. Exiting ... " exit fi #################################################################### ## SUBDIRS_LIST in /tmp, for testing: (note embedded spaces in last one) ## (also, soft-links followed) ## /tmp ## /tmp/.X11-unix ## /tmp/.ps_data ## /tmp/LINK_TEST.dir ## /tmp/LINK_TEST.dir/SUBDIRofLINK_TEST.dir ## /tmp/LINK_TEST2.linkdir ## /tmp/LINK_TEST2.linkdir/SUBDIRofLINK_TEST.dir ## /tmp/TEST DIR embedded spaces ## ## $ ls -l /tmp ## drwxr-xr-x 3 bmo01 user 39 Apr 26 14:38 LINK_TEST.dir ## lrwxr-xr-x 1 bmo01 user 13 Apr 26 14:11 LINK_TEST2.linkdir -> LINK_TEST.dir ## drwxr-xr-x 2 bmo01 user 9 Apr 26 13:33 TEST DIR embedded spaces ## -rw-r--r-- 1 root sys 57380 Mar 29 07:38 daemonstat.any.2077 ## Srwx------ 1 root sys 0 Mar 29 07:39 espdb.sock ## -rw-r--r-- 1 bmo01 user 10 Apr 26 08:02 ugs_bmo01.dat ## ## $ ls -l /tmp/LINK_TEST.dir ## drwxr-xr-x 2 bmo01 user 9 Apr 26 14:38 SUBDIRofLINK_TEST.dir #################################################################### #################################################################### ## IF we do not use 'sort', the dirnames are in 'random' order, from ## ## find $DIRNAME -local -type d -print #################################################################### #################################################################### ## ISSUE-1: ## The 'find' WITHOUT '-follow' does not detect links (-type l) to directories. ## On the other hand, ## WITH '-follow', 'find' lists subdirs of linked-to-dir, which could be ## many, many --- and/or may be confusing. ## ## COULD MAKE '-follow' an option, i.e. prompt user in calling ## utility, and pass a follow-parm to this script. #################################################################### #################################################################### ## ISSUE-2: ## SHOULD WE REVEAL 'Permission denied' NOTICES --- ## SOMEHOW WITH '2>&1' ? # # find $DIRNAME -local -follow -type d -print 2>&1 #################################################################### ########################################################################## ## FOR TESTING: ## SET REPORT HEADING on size of sub-directories. ## (A heading like this should be in the calling script.) ########################################################################## if test "$TEST_MODE" = "TEST_YES" then THISHOST=`hostname` SUBDIRS_LIST=`find $DIRNAME -local -follow -type d -print | sort` echo " ................... `date '+%Y %b %d %a %T%p'` ............................ DISK USAGE OF FILES IN *ALL* THE SUB-DIRECTORIES UNDER ${THISHOST}:$DIRNAME Actually the files listed are any kind of file EXCEPT directories. So links, 'sockets', 'named-pipes', 'block-special', and 'character-special' files may be shown. ************************ SORTED BY *FULLY-QUALIFIED FILENAME* within each sub-directory. ************************ And the sub-directories are listed in FULLY-QUALIFIED DIRECTORY NAME sort order. " ## FOR TESTING: echo " SUBDIRS_LIST: $SUBDIRS_LIST NAME-SORT Disk usage Last-Modified ********************** (MegaBytes) Permissions Owner Group Date-Time/Yr Filename -------------- ----------- -------- -------- ------------ ---------------------- GigMeg.KilByt | | | |" fi ## END OF if test "$TEST_MODE" = "TEST_YES" #################################################################### ## FOR EACH SUBDIR, DO 'ls -al' to give name-sorted file-info. ## (Filter out directory names.) ##################################################################### ## A 'for' loop does NOT seem to handle embedded spaces in dirnames. ## Also, it can fail on a list with too many names. 'Arguments too long' ## So we do NOT use ## for SUBDIR in `echo "$SUBDIRS_LIST"` ## But we use a 'while read' construct. Reference: Unix Power Tools, ## section 10.18. Also see end-of-47.16, end-of-47.18, 47.22, 47.23. ##################################################################### find "$DIRNAME" -local -follow -type d -print | sort | \ while read SUBDIR do # echo "" # echo " # $SUBDIR" echo " SUBDIR: $SUBDIR" # echo " # ****** # SUBDIR: $SUBDIR # " ## FOR TESTING: # set -x ###################################################################### ## List any regular file or link. MAY WANT TO SEE *ANY* NON-DIRECTORY FILE. ## GIVES RELATIVE FILENAMES. WANT FULL. # ls -al "$SUBDIR" | egrep '^-|^l' # ls -alL "$SUBDIR" | egrep '^-|^l' ###################################################################### ## List any non-directory file. GIVES RELATIVE FILENAMES. WANT FULL. # ls -al "$SUBDIR" | tail +2 | grep -v '^d' ###################################################################### ###################################################################### ## Use 'find' with 'prune', to get full filenames, at one dir-level. ###################################################################### ## BASENAME=`basename "$SUBDIR"` ## find "$SUBDIR" \( -type d ! -name "$BASENAME" -prune \) -o \ ## does not work properly on a directory like /apps/eai when it has ## subdirectory /apps/eai/eai. It goes down into /apps/eai/eai. ##################################################################### ## But we use a '-exec test' construct. Reference: Unix Power Tools, ## section 18.25. ##################################################################### BASENAME=`basename "$SUBDIR"` find "$SUBDIR" \( -type d ! -exec test "{}" = "$SUBDIR" \; -prune \) -o \ \( -type l -exec ls -ld {} \; \) -o \ \( ! -type d -exec ls -l {} \; \) | \ sort +8 | \ awk ' { COLfilnam = index($0,$9) ; \ printf ("%13.6f %-10s %-8s %-8s %-3s %2s %5s %s\n", $5/1000000, $1, $3, $4, $6, $7, $8, substr($0,COLfilnam) )}' ## ALTERNATIVE sort formulations, on filename --- with ## an aim to handle filenames with embedded blanks: # sort -k9 # sort +8 ## ALTERNATIVE 'ls' pair: # \( ! -type d -exec ls -l {} \; \) -o \ # \( -follow -type d -exec ls -ld {} \; \) | \ ## ALTERNATIVE 'ls' pair: # \( -follow -type l -exec ls -ld {} \; \) -o \ # \( ! -type d -exec ls -l {} \; \) | \ ## ALTERNATIVE 'ls' statements: # # ! -type d -exec ls -l {} \; # # -follow ! -type d -exec ls -l {} \; | \ # # -type f -exec ls -l {} \; | \ # # -exec ls -l {} \; | \ ## FOR TESTING (alternative to 'ls' statement): # -type f -print ## FOR TESTING: # set - done ##################################################################### ## NOTE on filenames with embedded spaces and 'awk': ## ## To handle filenames with embedded spaces, we use 'substr($0,COLfilnam)' ## in awk, instead of '$9' --- where ## we set COLfilnam with the expression 'COLfilnam = index($0,$9)'. ## ## The 'substr($0,COLfilnam)' gets the substring of the record, $0, ## starting at column COLfilnam and going to the end of the record. ## ## This is not elegant, because when the contents of field $9 matches one ## of the fields, $1 thru $8, some extra chars before the filename may be ## printed. ## ## 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. ## ## See script 'diruse_files_onelevel_sizesort_bygui' for a more foolproof ## method using 'substr($0,COLmonth + 12)'. #########################################################################