Friday, March 20, 2015

findFile.sh

#!/bin/bash
#========================================================
# Date: 3/19/2015
# Author: Deyong Xu @ SGT
# Purpose: Search files by patterns: one or multiple pattern.
# Limitation: Display will be werid if filename including
#             space.
# eg: $ findFile.sh  sgt      # search one pattern
#     $ findFile.sh  sgt  nco         # search two patterns
#     $ findFile.sh  -i  /home/user1  sgt  nco    # search in specified directory
#
#========================================================


# Default searching directory is the current one.
srhDir=`pwd`

# Set searching directory if specified via option "-i"
while  getopts "i:" var
do
   case "$var" in
      i)
        srhDir="$OPTARG"
        echo "$OPTARG"
        ;;    # equal to "break"
   esac
done

# Move parameter pointer to the right to skip
# option parameters along with their arguments to update $@
shift $((OPTIND-1))

# Initialize a counter
let cnt=0

# Search files in the serching directory
for pattern in $@
do
   # Case insensitive : -iname
   files=`find  ${srhDir}  -type f -iname "*${pattern}*" `
   for file in ${files}
   do
      ((cnt++))
      echo "File $cnt :    $file  "
   done
done

No comments:

Post a Comment