#!/bin/sh ## ## Nautilus ## SCRIPT: 06_anyfile4Dir_COUNT_FILESinSUBDIRS_allLEVS_find-d-ls-grep-wc-awk.sh ## ## PURPOSE: This utility will list the subdirectories, at ALL levels ## under the current directory --- with counts of the number of ## files in each subdirectory, next to the subdirectory name. ## ## METHOD: This script uses 'find' along with a 'hidden' Nautilus utility ## script : ## '.filecnt_4dir_1level_2stdout' ## in the scripts directory with this script. ## ## The utility script provides two counts --- ## the (sub)directory counts and the non-directory ('regular') ## file counts one-level 'under' a each directory. ## ## The utility script directs the counts data lines to stdout, ## and this script concatenates the output to a text file. ## ## This script shows the text file using a text-file viewer of ## the user's choice. ## ## HOW TO USE: ## 1) In the Nautilus file manager, navigate to the desired 'base' ## directory, and right-click on the name of ANY file (or directory) ## in that 'base' directory. ## 2) Then select this script to run (name above). (The 'base' ## directory becomes the 'current' directory in this script.) ## ## Created: 2011apr27 ## Changed: 2011may02 Added $USER to a temp filename. ## Changed: 2011may11 Get 'nautilus-scripts' directory via an include script. ## FOR TESTING: (show executed statements) # set -x ############################################################ ## Prep a temporary filename, to hold the list of subdirnames ## and file counts. ## If the user cannot write to the current directory, ## put the output file in /tmp. ############################################################ ## NOTE: ## We could put the report in the /tmp directory, rather than ## junking up the current directory with this report. ## ## BUT it may be useful to have the report available in ## the directory to which it applies. ############################################################# OUTFILE="${USER}_count_files4subdirs_alllevels_temp.lis" CURDIR="`pwd`" if test ! -w "$CURDIR" then OUTFILE="/tmp/$OUTFILE" fi if test -f "$OUTFILE" then rm -f "$OUTFILE" fi ########################################################################## ## PREPARE THE 'ALL-SUBDIRS-FILE-CNTS' REPORT HEADING. ########################################################################## HOST_ID="`hostname`" echo "\ ********************* `date '+%Y %b %d %a %T%p %Z'` ****************** FILE-COUNTS for ALL SUB-DIRECTORIES under the directory $CURDIR SORTED BY ** FULLY-QUALIFIED SUB-DIRECTORY NAME **. DIRECTORY NON-DIRECTORY ** SORT FIELD ** FILES COUNT FILES COUNT SUB-DIRECTORY FILENAME ------------ ------------- -------------------------------------------- " > "$OUTFILE" ######################################################################## ## Run the 'find' command, with a file-counting script --- on each ## subdirectory of the current directory --- ## to generate the guts of the report. ######################################################################## . $HOME/.freedomenv/feNautilusScripts/set_DIR_NautilusScripts.shi SCRIPT4FINDEXEC="$DIR_NautilusScripts/SPACElists/.filecnt_4dir_1level_2stdout.sh" ## FOR TESTING: # set -x find "$CURDIR" -type d \ -exec $SCRIPT4FINDEXEC {} \; | \ awk '{ COLfilnam = index($0,$3) ; \ printf ("%12s %13s %s\n", $1, $2, substr($0,COLfilnam) )}' \ | sort -k3 >> "$OUTFILE" ## FOR TESTING: # set - ########################################### ## ADD A TRAILER TO THE REPORT-FILE. ########################################### SCRIPT_BASENAME=`basename $0` SCRIPT_DIRNAME=`dirname $0` echo " ------------ ------------- -------------------------------------------- DIRECTORY NON-DIRECTORY SUB-DIRECTORY FILENAME FILES COUNT FILES COUNT ** SORT FIELD ** ..................... `date '+%Y %b %d %a %T%p'` ............................ The output above was generated by the script $SCRIPT_BASENAME in directory $SCRIPT_DIRNAME The report was generated by running a combination of commands, including the 'ls -Ap', grep', and 'wc -l' commands, on each subdirectory found using the 'find' command. ---------------- REPORT CONTENTS: For a given Directory, in this case the directory $CURDIR this script creates a FILE-COUNTS report -- for ALL sub-directories of the given Directory --- at ALL LEVELS under the directory (i.e. to the ends of the directory 'branches'). For each sub-directory, the report shows TWO separate file-counts: DIRECTORY-file-counts and NON-DIRECTORY-file-counts. ------------- REPORT USAGE: This utility is meant mainly as a comparison/difference utility to look for differences in two large/huge directory hierarchies. It is meant as a first step --- looking at summary file counts for sub-directories, rather than huge lists of filenames & file-sizes in those directories. If differences in sub-directory file-counts are found -- when comparing to a file-count report for another directory that should have the same files -- then one can 'zero in' on specific sub-directories where there are differences in counts --- to look at differences in two lists of file-names (& file-sizes), for the 2 differing sub-directories. If files that should be equal in size are different in size, one can 'zero in' further, if necessary, doing a 'diff' on the two files. Thus, proceed to 're-equalize' the directory structure. ----------------------------- SELECTING LINES OF THE REPORT -- via a string-pattern: When you browse the report with the 'xpg' utility, you may want to extract/see just those lines that contain a certain string. To do that, you can use the 'ShowAllMatches' button with the plus-or-minus N lines option, with N set to zero (0). Example: To see all files containing the string 'out', put the string 'out' (without the quotes) in the String entry field of the 'xpg' GUI. Set N to 0 and click the 'ShowAllMatches' button. ----------------- PROCESSING METHOD: The script uses a 'find' command, on the host of the specified directory, to find all the sub-directories of the directory. For each sub-directory, this utility uses a combination of commands --- 'ls -Ap', 'echo', 'grep', and 'wc -l' --- to generate a counts-line for each sub-directory. In addition, 'awk' and 'sort' are used to format the report data. ---------------------------------------------------------------------------- " >> "$OUTFILE" ################################################## ## Show the listing. ################################################## ## . $HOME/.gnome2/nautilus-scripts/.set_VIEWERvars.shi ## This is called above. # . $HOME/.freedomenv/feNautilusScripts/set_DIR_NautilusScripts.shi . $DIR_NautilusScripts/.set_VIEWERvars.shi $TXTVIEWER "$OUTFILE" &