#!/usr/bin/ksh # # Directory File analysis. # Ben Mealey - June 2001 # # Format field widths. typeset -R15 allFiles typeset -R15 allDirs typeset -R15 allLinks typeset -R15 Mtotal typeset -R15 Ktotal typeset -R15 total typeset -R15 count typeset -R15 AFcount typeset -L5 pfileType if (( $# != 1 )); then print "Usage: $0 " exit fi if [[ ! -d $1 ]] then print "Eh? - $1 not a directory" exit fi parmDir=$1 (( allFiles = 0 )) (( allDirs = 0 )) (( allLinks = 0 )) (( count = 0 )) (( AFcount = 0 )) print "$(uname -n) - Files under ${parmDir} - $(date)" # # File type analysis # # The first find/grep generates a list of files that end in the # familiar filetype format, for example "sample.gif". # XXXX # # The awk -F | sort | uniq | sort -rn produces a sorted list of all # filetypes. For example: # # 15 gif # 12 tif # # and so on. # # Each filetype is fed into a second find to calculate the size, in # bytes of matching files. # # Compare the "Total analyzed files" with the subsequent "Total Files' # in order to get a count of files which didn't match the classic # filename.fileExtension pattern. # print " " print "File type count:" print " " print " Type Count Size" find ${parmDir} -type "f" -print | grep "\.[^/]*" | awk -F"." '{print $NF}' | sort | uniq -c | sort -rn | while read line; do (( count = 0 )) (( total = 0 )) fileType="*$(print ${line} | awk '{print $2}')" find ${parmDir} -name "${fileType}" -type "f" -print -xdev |\ xargs -I{} ls -l {} |\ awk '{print $5 " " $NF}' | while read type; do size="$(print ${type} | awk '{print $1}')" fn1="$(print ${type} | awk '{print $2}')" fn="${fn1##*/}" (( count = count + 1 )) (( total = total + size )) done # # Scale raw byte counts into either MB or K. # pfileType=${fileType} # padded version if (( total > 1000000 )); then (( Mtotal = total / 1000000 )) print "${pfileType} ${count} ${Mtotal} MB" elif (( total > 1000 )); then (( Ktotal = total / 1000 )) print "${pfileType} ${count} ${Ktotal} K Bytes" else print "${pfileType} ${count} ${total} Bytes" fi (( AFcount = AFcount + count )) done print " " print "Total analyzed files: ${AFcount}" print " " # # Get total count of files, directories, and links. # # Size discrepancies are due to rounding errors, and the different # ways that the ls -l and du different commands report space. # (( allFiles = $(find ${parmDir} -type "f" -print | wc -l) )) (( allDirs = $(find ${parmDir} -type "d" -print | wc -l) )) (( allLinks = $(find ${parmDir} -type "l" -print | wc -l) )) print "Total Files ${allFiles}" print "Total Directories ${allDirs}" print "Total Links ${allLinks}" # # Get space used. # print " " (( KspaceUsed = $(du -ks ${parmDir} | awk '{print $1}') )) # Report in MB if space is more than 10,000 K or 10 MB. if (( KspaceUsed > 10000 )); then (( MspaceUsed = KspaceUsed / 1000 )) print "Space used: ${MspaceUsed} MB" else print "Space used: ${KspaceUsed} K" fi print " " print "Done - $(date)"