findfiles Script
#!/bin/sh
# findfiles.sh: "find" with less typing
#
# Usage: findfiles.sh [-h | -help | help] [-d <directory>] [-n "<pattern>"] [-g[i] <pattern>] -o <file>]
#
# -h | -help | help = Print help message
# -d <directory> = Search within <directory> (default is current directory)
# -n "<pattern>" = Search for filenames matching <pattern> (Using * wildcards)
# -g[i] <pattern> = "grep" files for <pattern> (Use -gi for grep -i)
# -o <file> = Send output to <file> (OVERWRITES EXISTING FILE!)
#
# Note: stderr is suppressed.
PRINT_USAGE=FALSE
SEARCH_DIR="."
NAME_PATTERN=""
GREP_PATTERN=""
GREP_OPTION="-l"
OUTPUT_FILE=""
# Parse arguments
while [ $# -ge 1 ]; do
case "$1" in
-h | -help | help )
# = Print help message
PRINT_USAGE=TRUE
;;
-d )
# = Search within <directory> (default is current directory)
if [ $# -ge 2 ]; then
if [ -d $2 ]; then
SEARCH_DIR=$2
shift
else
echo "$0: Not a directory: $2"
exit 1
fi
else
echo "$0: Warning: No directory found for -d option"
echo " Using current directory..."
fi
;;
-n )
# = Search for filenames matching <pattern> (Using * wildcards)
if [ $# -ge 2 ]; then
NAME_PATTERN="$2"
shift
else
echo "$0: Warning: No pattern found for -n option"
echo " Searching all files..."
fi
-gi )
# = "grep" files for <pattern>
if [ $# -ge 2 ]; then
GREP_PATTERN="$2"
GREP_OPTION="-li"
shift
else
echo "$0: Warning: No pattern found for -g option"
echo " Returning all found files..."
fi
;;
-g )
# = "grep" files for <pattern>
if [ $# -ge 2 ]; then
GREP_PATTERN="$2"
shift
else
echo "$0: Warning: No pattern found for -g option"
echo " Returning all found files..."
fi
;;
-o )
# = Send output to <file>
if [ $# -ge 2 ]; then
OUTPUT_FILE=$2
shift
else
echo "$0: Warning: No file found for -o option"
echo " Output sent to stdout..."
fi
;;
* )
echo "$0: Unknown argument: $1"
exit 1
;;
esac
# Next argument
shift
done # while [ $# -ge 1 ]; do
# Print help message?
if [ $PRINT_USAGE = TRUE ]; then
echo "Usage: $0 [-h | -help | help] [-d <directory>] [-n \"<pattern>\"]"
echo " [-g[i] <pattern>] -o <file>]"
echo ""
echo " -h | -help | help = Print help message"
echo " -d <directory> = Search within <directory> (default is current directory)"
echo " -n \"<pattern>\" = Search for filenames matching <pattern> (Using * wildcards)"
echo " -g[i] <pattern> = \"grep\" files for <pattern> (Use -gi for grep -i)"
echo " -o <file> = Send output to <file> (OVERWRITES EXISTING FILE!)"
echo ""
echo "Note: $0 suppresses stderr stderr from \"find\"!"
exit 0
fi
if [ "$NAME_PATTERN" != "" ]; then
# -n
if [ "$GREP_PATTERN" != "" ]; then
# -n -g
if [ "$OUTPUT_FILE" != "" ]; then
# -n -g -o
find $SEARCH_DIR -name "$NAME_PATTERN" -exec grep $GREP_OPTION "$GREP_PATTERN" {} \; 2>/dev/null >$OUTPUT_FILE
else
# -n -g
find $SEARCH_DIR -name "$NAME_PATTERN" -exec grep $GREP_OPTION "$GREP_PATTERN" {} \; 2>/dev/null
fi # END -o SWITCH
else
# -n
if [ "$OUTPUT_FILE" != "" ]; then
# -n -o
find $SEARCH_DIR -name "$NAME_PATTERN" -print 2>/dev/null >$OUTPUT_FILE
else
# -n
find $SEARCH_DIR -name "$NAME_PATTERN" -print 2>/dev/null
fi # END -o SWITCH
fi # END -g SWITCH
else
if [ "$GREP_PATTERN" != "" ]; then
# -g
if [ "$OUTPUT_FILE" != "" ]; then
# -g -o
find $SEARCH_DIR -exec grep $GREP_OPTION "$GREP_PATTERN" {} \; 2>/dev/null >$OUTPUT_FILE
else
# -g
find $SEARCH_DIR -exec grep $GREP_OPTION "$GREP_PATTERN" {} \; 2>/dev/null
fi # END -o SWITCH
else
if [ "$OUTPUT_FILE" != "" ]; then
# -o
find $SEARCH_DIR -print 2>/dev/null >$OUTPUT_FILE
else
# NO ARGUMENTS
find $SEARCH_DIR -print 2>/dev/null
fi # END -o SWITCH
fi # END -g SWITCH
fi # END -n SWITCH
page revision: 4, last edited: 25 Aug 2011 06:31