sh script

I need to capture directory names with a count of how many files mite the search criteria

  I have script that looks for files with no owners because of some issues
I had to modify it to give me a count of the files that meet the criteria.

 But what I would like is to split the number in numbers per Dir

Something like this  
/oracle = 10
/home/user1 = 20
/ossec/tmp = 100
This is the script
#!/bin/sh
#######################################################################
#<doc> NAME
#<doc>         unowned_files_dirs
#<doc>
#<doc> SYNTAX
#<doc>         unowned_files_dirs
#<doc>         where:
#<doc>               default is to find all files & dirs
#<doc>
#<doc> DIAGNOSTICS
#<doc>         Exit code 0 if successful
#<doc>                   0 on failure
#<doc>
#<doc>
#<doc> DESCRIPTION
#<doc>          Scan filesystem for unowned files & directories. This
#<doc>          can be done by scanning a path or multiple paths
#<doc>          separated by commas ("SCAN_FOLDER,SCAN_FOLDER2...").
#######################################################################
#  MODIFY DATE   MODIFIED BY   REASON FOR & DESCRIPTION OF MODIFICATION
#  -----------  -------------  ----------------------------------------
#  07/29/05     Maria Cabral   Written
#  10/20/06     Maria Cabral   Modified.
#######################################################################
LC_ALL="C"; export LC_ALL
umask 066
#######################################################################
trap 'rm -f "${Tmp0}" exit 1' 1 2 3 15
rm -f "${Tmp0}"
#######################################################################
if test -x /bin/nawk
then
  AWk="/bin/nawk"
else
  AWk="awk"
fi
#######################################################################
#
# Clean-up temporary (working files)files left from the previous run....
#
#
for FILES in `find /tmp -type f -name cisuf-* -print 2>/dev/null`
do
        if [ -f "${FILES}" ]
        then
                PIDD=`echo "${FILES}" | cut -d '.' -f2`
                CHECK_PID=`ps -ef | grep "${PIDD}" | $AWk '{print $2}' | wc -l`
                if [ "${CHECK_PID}" -eq "0" ]; then
                        rm -f "${FILES}"
                fi
        fi
done
#######################################################################
#
# Get the required input
#
# Define directories that need to be fExcluded (multiple folder must be
# separated by commas) or the entire filesystem will be scanned.
# Default value for fExcludeS is N (no dir to be fExcluded).
#
# # 1: Function: parse_args ()
#
parse_args ()
{
  if [ $# -eq 1  ]
  then
    USER_DIRs=`echo $1 | sed 's/,/ /g'`
    GREP_EXCLUDE_DIRs=`echo "^$1" | sed 's/,/\\|^/g'`
        GREP_EXCLUDE_DIRs=`echo "${GREP_EXCLUDE_DIRs}\$|^$1/" | sed 's/,\//\\\|^/g'`
  else
        echo "Usage: scriptutil -h [HOST] -s unowned_files_dirs [directory to exclude]..."
    exit 0
  fi
}

#######################################################################
# 2: Function: exclude_find_dirs()
#
# Rule:      fExclude
# Search:    Don't look in the following directories.
# Criteria:  must be specified by the user as a parameter.
# Action:    Don't display the file
#
exclude_find_dirs()
{
  for USER_DIR in $USER_DIRs
  do
    if [ -n "$fExclude" ]
    then
      fExclude="$fExclude -o"
    fi
        SEARCH_PATH=`echo $USER_DIR | sed 's/\// /g' | awk '{print $NF}'`
        fExclude="${fExclude} -name $SEARCH_PATH -prune"
  done

  if [ -n "$fExclude" ]
  then
    fExclude="( $fExclude )"
  fi
}
######################################################################
# Make call - Function #1, #2
#
parse_args $@
exclude_find_dirs

######################################################################
# Rule:      fUnowned
# Search:    Search for any object that has an owner whose UID is not
#            registered in /etc/passwd.  Search for any object that
#            has a group-owner whose GID is not registered in /etc/group.
# Criteria:  * must either have no valid owner or no valid group
# Action:    Display directory listing
#
fUnowned='( -nouser -o -nogroup )'

######################################################################
#
# Output unowned files and directories.
######################################################################
GREP_EXCLUDE_DIRs="${GREP_EXCLUDE_DIRs}tcb$|dev$|devices$|proc$|vol$|xfn$|cdrom$|mnt$|fd$"
DIRs=`ls -1ALl / | grep "^d" | egrep -v "$GREP_EXCLUDE_DIRs" | awk '{print "/"$9}'`

if [ -n "${fExclude}" ]
then
        fExclude=`echo "${fExclude} -o"`
fi

#find ${DIRs} ${fExclude} \( -fstype ufs \) \( ! -local -prune \) -o \( -local \( \
#                ${fUnowned} \) \) -print 2>/dev/null

#find / ${DIRs} ${fExclude} -mount -local -type f \( -perm -4000 -o -perm -2000 \) -exec ls -ld '{}' \;
COUNT= find / ${DIRs} ${fExclude} -mount -local -type f \( -perm -4000 -o -perm -2000 \) -ls | wc -l
echo $COUNT
#######################################################################
# Exit.
exit 0
#######################################################################
# End of unowned_files_dirs.SunOS.sh



d_asselinAsked:
Who is Participating?
 
sentnerCommented:
Also, if you really want the format to be "dir = count", you can always pipe it through somethink like awk:

 for i in `find foo -nouser`; do dirname $i; done | sort | uniq -c | awk '{print $2, " = ", $1}'
0
 
xtermCommented:
Here's some sample directories where I created some junk files with some phony (non-existent) uids to own some of them:

colinb@laptop:~$ ls -lRt foo
foo:
total 8
drwxr-xr-x 2 colinb colinb 4096 2011-10-20 14:49 baz
drwxr-xr-x 2 colinb colinb 4096 2011-10-20 14:49 bar

foo/baz:
total 0
-rw-r--r-- 1 colinb colinb 0 2011-10-20 14:49 room
-rw-r--r-- 1    645 colinb 0 2011-10-20 14:49 world
-rw-r--r-- 1    644 colinb 0 2011-10-20 14:49 hello

foo/bar:
total 0
-rw-r--r-- 1 623 colinb 0 2011-10-20 14:49 world
-rw-r--r-- 1 622 colinb 0 2011-10-20 14:49 hello


Here's how I found them and sorted them as you asked:

colinb@laptop:~$ for i in `find foo -nouser`; do dirname $i; done | uniq -c
      2 foo/baz
      2 foo/bar

You can replace "foo" with whatever path you want to search, including "/", but I wouldn't recommend it.
0
 
sentnerCommented:
Nicely done, xterm.  One thing I might suggest is adding a sort after the for loop before the uniq call, as if you have subdirectories that also contain unowned files that fall in between 2 others, it will skew your output.  So:

 for i in `find foo -nouser`; do dirname $i; done | sort | uniq -c
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
d_asselinAuthor Commented:
Hi All Thank you for your input into this script This is the line I inserted in the script I added the find option and the final count which is Needed in this script  for i in `find / $DIR -mount -local -nouser`; do dirname $i; done | sort | uniq -c | awk '{print $2","$1}'' {SUM +=$1}END {print " Total Count =" SUM}'
0
 
xtermCommented:
Nice job adding the total count to the end, well done!
0
 
d_asselinAuthor Commented:
Like I said b4 tank you all  for the help

Regards
Dan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.