Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

findit - a menu-base kshell script to find files and text within files

Status
Not open for further replies.

RobJordan

MIS
Apr 3, 2001
296
US
Enjoy!

Robert

#!/usr/bin/ksh
#
# Date: 2/08/2002
# Name: findit
# Ver: 1.1
# Author: Robert G. Jordan Robert@JORDAN2000.com
# Purpose: find files by name, size, date, time or text withinin a file
# To Run: findit (an interactive menu will launch)
#
################################################################################

################################################################################
# Variables
################################################################################

TMP_LIST=/tmp/TMP_LIST.$$ # generic tmp file for data
COLUMNS=1 # Number of columns for menus
MAX_ROWS=50 # Max rows per page for menus
MAX_LINE=80 # Max line length
LOG_FILE=/tmp/findit.log.$$.tmp # results are written this log file
HOST_NAME=`/usr/bin/hostname` # capture hostname for log file
CUR_DATE=`/usr/bin/date` # capture date for log file

################################################################################
# Functions
################################################################################

Write () # Display a message on screen and log to a file
# usage: Write "[Your message here]"
{
MESSAGE="$1"
eval "echo \"$MESSAGE\""|tee -a $LOG_FILE
}

Create_Menu () # display menu from a list and get user input
# requires one to three arguments: LIST_NAME and MENU_TITLE_ONE(optional) and MENU_TITLE_TWO(optional)
# ALL_OPTION can be set to true or false i.e. let user select all options on the menu

# returns MENU_VALUE
{
# clear
LIST=$1
MENU_TITLE_ONE=$2
MENU_TITLE_TWO=$3
IM_DONE="false"
ALL="false"
LIST_COUNT=`cat $LIST|wc -l`
until [ "$IM_DONE" = "true" ] # loop until valid input is received
do

MENU_COUNTER=1 # initialize counter
echo ""
echo " $MENU_TITLE_ONE"
echo ""
echo " $MENU_TITLE_TWO"
echo "--------------------------------------------------------------------------------"

ROWS=0
LINE_LENGTH=0
FORMAT_COUNTER=0
LIST_COUNT=`expr $LIST_COUNT + 1`

cat $LIST|while read LINE
do
FORMAT_COUNTER=`expr $FORMAT_COUNTER + 1`

if [ $MENU_COUNTER -lt 1000 ] # add no extra spaces for triple-digit menu number
then
SPACES=""
fi

if [ $MENU_COUNTER -lt 100 ] # add one extra space for double-digit menu number
then
SPACES=" "
fi

if [ $MENU_COUNTER -lt 10 ] # add two extra spaces for single-digit menu number
then
SPACES=" "
fi

STRING=`echo "${MENU_COUNTER}) ${SPACES}${LINE}"`
Check_Line

if [ $FORMAT_COUNTER -eq $COLUMNS ] # formats based on value of COLUMNS
then
echo "$STRING"
LINE_LENGTH=0
FORMAT_COUNTER=0
ROWS=`expr $ROWS + 1`
else
case ${#LINE} in # determine number of tabs to add based on item length
[0-9]|[1][0-5])
echo "$STRING\t\t\c"
;;
[1][6-9]|[2][0-9])
echo "$STRING\t\c"
;;
*)
echo "$STRING\c"
;;
esac
fi

MENU_COUNTER=`expr $MENU_COUNTER + 1`

if [ $ROWS -eq $MAX_ROWS ]
then
echo ""
echo "Press [RETURN/ENTER KEY] to see more choices."
ROWS=0
read PAUSE
fi
done

if [ "$ALL_OPTION" = "true" ]
then
echo "$MENU_COUNTER) -- ALL --"
ALL=$MENU_COUNTER
MENU_COUNTER=`expr $MENU_COUNTER + 1`
fi

echo "$MENU_COUNTER) -- Exit --"
EXIT=$MENU_COUNTER

# get user's menu choice

echo "#? \c"
read MENU_CHOICE
if [ "$MENU_CHOICE" = "$EXIT" ]
then
Exit
fi

if [ "$MENU_CHOICE" = "$ALL" ]
then
IM_DONE="true"
ALL="true"
break
fi

MENU_COUNTER=1

cat $LIST|while read LINE
do
if [ "$MENU_CHOICE" = "$MENU_COUNTER" ]
then
MENU_VALUE=$LINE
IM_DONE="true"
break
fi

MENU_COUNTER=`expr $MENU_COUNTER + 1`
done
done
}

Check_Line () # add line wrapping for menu items
{
LINE_LENGTH=`expr $LINE_LENGTH + ${#STRING}`

if [ $LINE_LENGTH -gt $MAX_LINE ] # add extra carriage return if line gets too long
then
echo ""
ROWS=`expr $ROWS + 1`
FORMAT_COUNTER=0
LINE_LENGTH=0
fi
}

Exit () # user seleted exit
{
exit 0
}


Check_ID ()
{
ID=`whoami`
if [ $ID != "root" ]
then
echo "WARNING: you are not root user."
echo "findit may not be able to search certain directories."
echo
echo "Please press [RETURN/ENTER] to continue."
read PAUSE
echo
fi
}

Main_Menu ()
{
> $TMP_LIST.Main
echo "Find files by name" >> $TMP_LIST.Main
echo "Find files by size" >> $TMP_LIST.Main
echo "Find files by user" >> $TMP_LIST.Main
echo "Find files by age" >> $TMP_LIST.Main
echo "Custom Search (mix & match criteria)" >> $TMP_LIST.Main
echo "Find files by text within file" >> $TMP_LIST.Main
ALL_OPTION="false"
Create_Menu "$TMP_LIST.Main" "" "Main Menu"

case $MENU_CHOICE in
1)
Name_Search
;;
2)
Size_Search
;;
3)
User_Search
;;
4)
Age_Search
;;
5)
Custom_Search
;;
6)
Text_Search
;;
*)
Main_Menu
;;
esac
}

File_Name ()
{
echo
echo "Please enter file name to search for"
echo "* can be used as a wild card ex: *.log"
echo "enter * alone to search for all files"
echo "note: all searches are case sensative"
echo
echo "File Name--> \c"
read FILE_NAME
if [ ${#FILE_NAME} -lt 1 ] #loop until valid input is received
then
File_Name
fi
}


Search_Path ()
{
echo "Search Path--> \c"
read SEARCH_PATH
if test -a $SEARCH_PATH
then
case $SEARCH_PATH in
/)
SEARCH_PATH="$SEARCH_PATH*"
;;
*)
SEARCH_PATH="$SEARCH_PATH/*"
;;
esac
else
echo "Invalid path!"
Search_Path
fi
}

Sub_Directories ()
{
echo "Include sub-directories in this search? (y/n) [y] \c"
read ANSWER
case $ANSWER in
[nN])
SUB_DIRS="-only -prune"
;;
*)
SUB_DIRS=""
;;
esac
}

Symbolic_Links ()
{
FOLLOW=""
echo "Follow symbolic links? (y/n) [n] \c"
read ANSWER
case $ANSWER in
[yY])
FOLLOW="-follow"
;;
*)
FOLLOW=""
;;
esac
}

Name_Search ()
{
File_Name
}

Size_Search ()
{
PLUS_SIZE=""
MINUS_SIZE=""
> $TMP_LIST.Size
echo "Find files greater than X..." >> $TMP_LIST.Size
echo "Find files less than Y..." >> $TMP_LIST.Size
echo "Find files greater than X and less than Y..." >> $TMP_LIST.Size
ALL_OPTION="false"
Create_Menu "$TMP_LIST.Size" "" "Search by Size Menu"

case $MENU_CHOICE in
1)
echo "Please enter greater than size in bytes"
echo "Note: 1MB = 1000000 bytes"
echo "--> \c"
read SIZE
PLUS_SIZE="-size +${SIZE}c"
;;
2)
echo "Please enter less than size in bytes"
echo "Note: 1MB = 1000000 bytes"
echo "--> \c"
read SIZE
MINUS_SIZE="-size -${SIZE}c"
;;
3)
echo "Please enter greater than size in bytes"
echo "Note: 1MB = 1000000 bytes"
echo "--> \c"
read SIZE
PLUS_SIZE="-size +${SIZE}c"
echo "Please enter greater than size in bytes"
echo "--> \c"
read SIZE
MINUS_SIZE="-size -${SIZE}c"
;;
*)
Size_Search
;;
esac
}

Age_Search ()
{
PLUS_DAYS=""
MINUS_DAYS=""
> $TMP_LIST.Age
echo "Find files more than X days old..." >> $TMP_LIST.Age
echo "Find files less than Y days old..." >> $TMP_LIST.Age
echo "Find files more than X days old and less than Y days old..." >> $TMP_LIST.Age
ALL_OPTION="false"
Create_Menu "$TMP_LIST.Age" "" "Search by Age Menu"

case $MENU_CHOICE in
1)
echo "Please enter amount for more than X days old"
echo "--> \c"
read DAYS
PLUS_DAYS="-mtime +${DAYS}"
;;
2)
echo "Please enter amount for less than Y days old"
echo "--> \c"
read DAYS
MINUS_DAYS="-mtime -${DAYS}"
;;
3)
echo "Please enter amount for more than X days old"
echo "--> \c"
read DAYS
PLUS_DAYS="-mtime +${DAYS}"
echo "Please enter amount for less than Y days old"
echo "--> \c"
read DAYS
MINUS_DAYS="-mtime -${DAYS}"
;;
*)
Age_Search
;;
esac
}


User_Search ()
{
echo "Please enter user ID to search for"
echo "--> \c"
read USER_ID
USER="-user $USER_ID"
}

Custom_Search ()
{
echo "Custom Search"
echo "Enter file name criteria? (y/n) [n] \c"
read ANSWER
if [ "$ANSWER" = "y" ] || [ "$ANSWER" = "Y" ]
then
Name_Search
fi

echo "Enter file size criteria? (y/n) [n] \c"
read ANSWER
if [ "$ANSWER" = "y" ] || [ "$ANSWER" = "Y" ]
then
Size_Search
fi

echo "Enter file user id criteria? (y/n) [n] \c"
read ANSWER
if [ "$ANSWER" = "y" ] || [ "$ANSWER" = "Y" ]
then
User_Search
fi

echo "Enter file age criteria? (y/n) [n] \c"
read ANSWER
if [ "$ANSWER" = "y" ] || [ "$ANSWER" = "Y" ]
then
Age_Search
fi
}

Text_Search ()
{
case $SEARCH_COMPLETE in
true)
echo "Search for a text string within these files? (y/n) [n] \c"
read ANSWER
case $ANSWER in
[yY])
String_Search
;;
*)
exit 0
;;
esac
;;
*)
echo
echo "First, run a search (1-5)"
echo "You will then be able to search"
echo "the results for a text string"
Main_Menu
;;
esac
}

String_Search ()
{
echo "Please enter text to search for"
echo "--> \c"
read TEXT
echo
echo "Case-sensative search (y/n) [n] \c"
read ANSWER
case $ANSWER in
[yY])
GREP_STATEMENT="grep"
;;
*)
GREP_STATEMENT="grep -i"
;;
esac
TEXT_FILE_COUNT=0
FILE_HITS=0
TOTAL_HITS=0
echo
echo "Scanning files...\c"
echo "$CUR_DATE" >> $TMP_LIST.Text_Hits
echo "$FIND_COMMAND" >> $TMP_LIST.Text_Hits
echo "Text Search: $TEXT" >> $TMP_LIST.Text_Hits

cat $TMP_LIST.Results|while read LINE
do
echo "...\c"
FILE_TYPE=`file $LINE|awk -F":" '{print $2}`
FILE_TEST=`echo $FILE_TYPE|grep -i text`
if [ ${#FILE_TEST} -gt 0 ]
then
TEXT_FILE_COUNT=`expr $TEXT_FILE_COUNT + 1`
HIT_COUNT=`$GREP_STATEMENT "$TEXT" $LINE|wc -l`
if [ $HIT_COUNT -gt 0 ]
then
echo
echo "$HIT_COUNT occurences of [$TEXT] found in $LINE"
echo "$HIT_COUNT occurences of [$TEXT] found in $LINE" >> $TMP_LIST.Text_Hits
FILE_HITS=`expr $FILE_HITS + 1`
TOTAL_HITS=`expr $TOTAL_HITS + $HIT_COUNT`
fi
fi
done
echo
echo "$TEXT_FILE_COUNT text files found"
echo "$TEXT_FILE_COUNT text files found" >> $TMP_LIST.Text_Hits
echo "[$TEXT] was found in $FILE_HITS files"
echo "[$TEXT] was found in $FILE_HITS files" >> $TMP_LIST.Text_Hits
echo "Total hits for [$TEXT]: $TOTAL_HITS"
echo "Total hits for [$TEXT]: $TOTAL_HITS" >> $TMP_LIST.Text_Hits
echo
echo "Basic results saved in: $TMP_LIST.Results"
case $DETAILED_RESULTS in
true)
echo "Detailed results saved in: $TMP_LIST.Detail"
;;
esac
echo "Text search results saved in: $TMP_LIST.Text_Hits"
echo
}


Begin_Search ()
{
echo
FIND_COMMAND=`echo "find $SEARCH_PATH $SUB_DIRS $FOLLOW $PLUS_SIZE $MINUS_SIZE $USER $PLUS_DAYS $MINUS_DAYS -name $FILE_NAME -print"`
echo "Executing: $FIND_COMMAND"
echo "Searching...\c"
# find $SEARCH_PATH $FOLLOW $PLUS_SIZE $MINUS_SIZE $USER -type f -name "$FILE_NAME" -exec ls -l {} \;
find $SEARCH_PATH $SUB_DIRS $FOLLOW $PLUS_SIZE $MINUS_SIZE $USER $PLUS_DAYS $MINUS_DAYS -name "$FILE_NAME" -print >> $TMP_LIST.Results &

SEARCHING="true"
until [ "$SEARCHING" = "false" ]
do
TEST=`ps -ef|grep $$|grep "find "|grep -v grep`
if [ ${#TEST} -gt 0 ]
then
# tail -1 $TMP_LIST.Results
echo "...\c"
else
echo "Search Complete! \c"
break
fi
done
echo
echo
echo "Provide detailed results? (y/n) [y] \c"
read ANSWER
case $ANSWER in
[Nn])
DETAILED_RESULTS="false"
;;
*)
DETAILED_RESULTS="true"
echo "Formatting detailed results...\c"
echo "$CUR_DATE" >> $TMP_LIST.Detail
echo "$FIND_COMMAND" >> $TMP_LIST.Detail
echo "" >>$TMP_LIST.Detail
cat $TMP_LIST.Results|sort|while read LINE
do
echo "...\c"
FILE_DETAIL=`ls -ld $LINE`
echo $FILE_DETAIL >> $TMP_LIST.Detail
done
echo "Done!"
;;
esac
echo
}

Display_Results ()
{
MATCHES=`cat $TMP_LIST.Results|wc -l`
echo
echo "$MATCHES matches found."
echo
if [ ${MATCHES} -gt 0 ]
then
echo "Basic results saved in: $TMP_LIST.Results"
case $DETAILED_RESULTS in
true)
echo "Detailed results saved in: $TMP_LIST.Detail"
;;
esac
echo
echo "Display results on screen? (y/n) [y] \c "
read ANSWER
case $ANSWER in
[nN])
ON_SCREEN="false"
;;
*)
case $DETAILED_RESULTS in
true)
echo
cat $TMP_LIST.Detail
echo
echo "Basic results saved in: $TMP_LIST.Results"
echo "Detailed results saved in: $TMP_LIST.Detail"
;;
*)
echo
cat $TMP_LIST.Results|sort
echo
echo "Basic results saved in: $TMP_LIST.Results"
esac
esac
echo
SEARCH_COMPLETE="true"
fi
}

################################################################################
# Main
################################################################################
# clear

VERSION=`head -5 findit|grep Ver:|awk '{print $3}'`
echo
echo "Thank you for using findit version $VERSION"
echo "Please send any questions/comments to Robert@JORDAN2000.com"
echo

SEARCH_COMPLETE="false"
DETAILED_RESULTS="true"
FILE_NAME="*"

Check_ID
Main_Menu
Search_Path
Sub_Directories
Symbolic_Links
Begin_Search
Display_Results
Text_Search
 
updated version!

Main Menu
--------------------------------------------------------------------------------
1) Find files by name
2) Find files by size
3) Find files by user
4) Find files by age
5) Find files by access date
6) Find files by file permissions
7) Custom Search (mix & match criteria)
8) Find files by text within file
9) -- Exit --

code is below this line
--------------------------------------------
#!/usr/bin/ksh
#
# Date: 2/08/2002
# Name: findit
# Ver: 1.3
# Author: Robert G. Jordan Robert.Jordan@JORDAN2000.com
# Purpose: find files by name, size, date, time or text withinin a file
# To Run: findit (an interactive menu will launch)
#
################################################################################

################################################################################
# Variables
################################################################################

TMP_LIST=/tmp/TMP_LIST.$$ # generic tmp file for data
COLUMNS=1 # Number of columns for menus
MAX_ROWS=50 # Max rows per page for menus
MAX_LINE=80 # Max line length
LOG_FILE=/tmp/findit.log.$$.tmp # results are written this log file
HOST_NAME=`/usr/bin/hostname` # capture hostname for log file
CUR_DATE=`/usr/bin/date` # capture date for log file

################################################################################
# Functions
################################################################################

Write () # Display a message on screen and log to a file
# usage: Write "[Your message here]"
{
MESSAGE="$1"
eval "echo \"$MESSAGE\""|tee -a $LOG_FILE
}

Create_Menu () # display menu from a list and get user input
# requires one to three arguments: LIST_NAME and MENU_TITLE_ONE(optional) and MENU_TITLE_TWO(optional)
# ALL_OPTION can be set to true or false i.e. let user select all options on the menu

# returns MENU_VALUE
{
# clear
LIST=$1
MENU_TITLE_ONE=$2
MENU_TITLE_TWO=$3
IM_DONE="false"
ALL="false"
LIST_COUNT=`cat $LIST|wc -l`
until [ "$IM_DONE" = "true" ] # loop until valid input is received
do

MENU_COUNTER=1 # initialize counter
echo ""
echo " $MENU_TITLE_ONE"
echo ""
echo " $MENU_TITLE_TWO"
echo "--------------------------------------------------------------------------------"

ROWS=0
LINE_LENGTH=0
FORMAT_COUNTER=0
LIST_COUNT=`expr $LIST_COUNT + 1`

cat $LIST|while read LINE
do
FORMAT_COUNTER=`expr $FORMAT_COUNTER + 1`

if [ $MENU_COUNTER -lt 1000 ] # add no extra spaces for triple-digit menu number
then
SPACES=""
fi

if [ $MENU_COUNTER -lt 100 ] # add one extra space for double-digit menu number
then
SPACES=" "
fi

if [ $MENU_COUNTER -lt 10 ] # add two extra spaces for single-digit menu number
then
SPACES=" "
fi

STRING=`echo "${MENU_COUNTER}) ${SPACES}${LINE}"`
Check_Line

if [ $FORMAT_COUNTER -eq $COLUMNS ] # formats based on value of COLUMNS
then
echo "$STRING"
LINE_LENGTH=0
FORMAT_COUNTER=0
ROWS=`expr $ROWS + 1`
else
case ${#LINE} in # determine number of tabs to add based on item length
[0-9]|[1][0-5])
echo "$STRING\t\t\c"
;;
[1][6-9]|[2][0-9])
echo "$STRING\t\c"
;;
*)
echo "$STRING\c"
;;
esac
fi

MENU_COUNTER=`expr $MENU_COUNTER + 1`

if [ $ROWS -eq $MAX_ROWS ]
then
echo ""
echo "Press [RETURN/ENTER KEY] to see more choices."
ROWS=0
read PAUSE
fi
done

if [ "$ALL_OPTION" = "true" ]
then
echo "$MENU_COUNTER) -- ALL --"
ALL=$MENU_COUNTER
MENU_COUNTER=`expr $MENU_COUNTER + 1`
fi

echo "$MENU_COUNTER) -- Exit --"
EXIT=$MENU_COUNTER

# get user's menu choice

echo "#? \c"
read MENU_CHOICE
if [ "$MENU_CHOICE" = "$EXIT" ]
then
Exit
fi

if [ "$MENU_CHOICE" = "$ALL" ]
then
IM_DONE="true"
ALL="true"
break
fi

MENU_COUNTER=1

cat $LIST|while read LINE
do
if [ "$MENU_CHOICE" = "$MENU_COUNTER" ]
then
MENU_VALUE=$LINE
IM_DONE="true"
break
fi

MENU_COUNTER=`expr $MENU_COUNTER + 1`
done
done
}

Check_Line () # add line wrapping for menu items
{
LINE_LENGTH=`expr $LINE_LENGTH + ${#STRING}`

if [ $LINE_LENGTH -gt $MAX_LINE ] # add extra carriage return if line gets too long
then
echo ""
ROWS=`expr $ROWS + 1`
FORMAT_COUNTER=0
LINE_LENGTH=0
fi
}

Exit () # user seleted exit
{
exit 0
}


Check_ID ()
{
ID=`whoami`
if [ $ID != "root" ]
then
echo "WARNING: you are not root user."
echo "findit may not be able to search certain directories."
echo
echo "Please press [RETURN/ENTER] to continue."
read PAUSE
echo
fi
}

Main_Menu ()
{
> $TMP_LIST.Main
echo "Find files by name" >> $TMP_LIST.Main
echo "Find files by size" >> $TMP_LIST.Main
echo "Find files by user" >> $TMP_LIST.Main
echo "Find files by age" >> $TMP_LIST.Main
echo "Find files by access date" >> $TMP_LIST.Main
echo "Find files by file permissions" >> $TMP_LIST.Main
echo "Custom Search (mix & match criteria)" >> $TMP_LIST.Main
echo "Find files by text within file" >> $TMP_LIST.Main
ALL_OPTION="false"
Create_Menu "$TMP_LIST.Main" "" "Main Menu"

case $MENU_CHOICE in
1)
Name_Search
;;
2)
Size_Search
;;
3)
User_Search
;;
4)
Age_Search
;;
5)
Access_Search
;;
6)
Permissions_Search
;;
7)
Custom_Search
;;
8)
Text_Search
;;
*)
Main_Menu
;;
esac
}

File_Name ()
{
echo
echo "Please enter file name to search for"
echo "* can be used as a wild card ex: *.log"
echo "enter * alone to search for all files"
echo "note: all searches are case sensative"
echo
echo "File Name--> \c"
read FILE_NAME
if [ ${#FILE_NAME} -lt 1 ] #loop until valid input is received
then
File_Name
fi
}


Search_Path ()
{
echo "Search Path--> \c"
read SEARCH_PATH
if test -a $SEARCH_PATH
then
case $SEARCH_PATH in
/)
SEARCH_PATH="$SEARCH_PATH*"
;;
*)
SEARCH_PATH="$SEARCH_PATH/*"
;;
esac
else
echo "Invalid path!"
Search_Path
fi
}

Sub_Directories ()
{
echo "Include sub-directories in this search? (y/n) [y] \c"
read ANSWER
case $ANSWER in
[nN])
SUB_DIRS="-only -prune"
;;
*)
SUB_DIRS=""
;;
esac
}

Symbolic_Links ()
{
FOLLOW=""
echo "Follow symbolic links? (y/n) [n] \c"
read ANSWER
case $ANSWER in
[yY])
FOLLOW="-follow"
;;
*)
FOLLOW=""
;;
esac
}

Name_Search ()
{
File_Name
}

Size_Search ()
{
PLUS_SIZE=""
MINUS_SIZE=""
> $TMP_LIST.Size
echo "Find files greater than X..." >> $TMP_LIST.Size
echo "Find files less than Y..." >> $TMP_LIST.Size
echo "Find files greater than X and less than Y..." >> $TMP_LIST.Size
ALL_OPTION="false"
Create_Menu "$TMP_LIST.Size" "" "Search by Size Menu"

case $MENU_CHOICE in
1)
echo "Please enter greater than size in bytes"
echo "Note: 1MB = 1000000 bytes"
echo "--> \c"
read SIZE
PLUS_SIZE="-size +${SIZE}c"
;;
2)
echo "Please enter less than size in bytes"
echo "Note: 1MB = 1000000 bytes"
echo "--> \c"
read SIZE
MINUS_SIZE="-size -${SIZE}c"
;;
3)
echo "Please enter greater than size in bytes"
echo "Note: 1MB = 1000000 bytes"
echo "--> \c"
read SIZE
PLUS_SIZE="-size +${SIZE}c"
echo "Please enter greater than size in bytes"
echo "--> \c"
read SIZE
MINUS_SIZE="-size -${SIZE}c"
;;
*)
Size_Search
;;
esac
}

Age_Search ()
{
AGE_PLUS_DAYS=""
AGE_MINUS_DAYS=""
> $TMP_LIST.Age
echo "Find files more than X days old..." >> $TMP_LIST.Age
echo "Find files less than Y days old..." >> $TMP_LIST.Age
echo "Find files more than X days old and less than Y days old..." >> $TMP_LIST.Age
ALL_OPTION="false"
Create_Menu "$TMP_LIST.Age" "" "Search by Age Menu"

case $MENU_CHOICE in
1)
echo "Please enter amount for more than X days old"
echo "--> \c"
read DAYS
AGE_PLUS_DAYS="-mtime +${DAYS}"
;;
2)
echo "Please enter amount for less than Y days old"
echo "--> \c"
read DAYS
AGE_MINUS_DAYS="-mtime -${DAYS}"
;;
3)
echo "Please enter amount for more than X days old"
echo "--> \c"
read DAYS
AGE_PLUS_DAYS="-mtime +${DAYS}"
echo "Please enter amount for less than Y days old"
echo "--> \c"
read DAYS
AGE_MINUS_DAYS="-mtime -${DAYS}"
;;
*)
Age_Search
;;
esac
}

Access_Search ()
{
ACC_PLUS_DAYS=""
ACC_MINUS_DAYS=""
> $TMP_LIST.Access
echo "Find files with an access date of more than X days old..." >> $TMP_LIST.Access
echo "Find files with an access date of less than Y days old..." >> $TMP_LIST.Access
echo "Find files with an access date of more than X days and less than Y days ..." >> $TMP_LIST.Access
ALL_OPTION="false"
Create_Menu "$TMP_LIST.Access" "" "Search by access date Menu"

case $MENU_CHOICE in
1)
echo "Please enter amount for more than X days old"
echo "--> \c"
read DAYS
ACC_PLUS_DAYS="-atime +${DAYS}"
;;
2)
echo "Please enter amount for less than Y days old"
echo "--> \c"
read DAYS
ACC_MINUS_DAYS="-atime -${DAYS}"
;;
3)
echo "Please enter amount for more than X days old"
echo "--> \c"
read DAYS
ACC_PLUS_DAYS="-atime +${DAYS}"
echo "Please enter amount for less than Y days old"
echo "--> \c"
read DAYS
ACC_MINUS_DAYS="-atime -${DAYS}"
;;
*)
Access_Search
;;
esac
}

Permissions_Search ()
{
echo
echo "Please enter file permissions to search for"
echo "Format is [owner][group][world] ex: 777"
echo "--> \c"
read PERMISSIONS
case $PERMISSIONS in
[0-7][0-7][0-7])
PERM="-perm $PERMISSIONS"
;;
*)
echo "Invalid entry!"
Permissions_Search
;;
esac
}


User_Search ()
{
echo "Please enter user ID to search for"
echo "--> \c"
read USER_ID
USER="-user $USER_ID"
}

Custom_Search ()
{
echo "Custom Search"
echo "Enter file name criteria? (y/n) [n] \c"
read ANSWER
if [ "$ANSWER" = "y" ] || [ "$ANSWER" = "Y" ]
then
Name_Search
fi

echo "Enter file size criteria? (y/n) [n] \c"
read ANSWER
if [ "$ANSWER" = "y" ] || [ "$ANSWER" = "Y" ]
then
Size_Search
fi

echo "Enter file user id criteria? (y/n) [n] \c"
read ANSWER
if [ "$ANSWER" = "y" ] || [ "$ANSWER" = "Y" ]
then
User_Search
fi

echo "Enter file age criteria? (y/n) [n] \c"
read ANSWER
if [ "$ANSWER" = "y" ] || [ "$ANSWER" = "Y" ]
then
Age_Search
fi

echo "Enter file access date criteria? (y/n) [n] \c"
read ANSWER
if [ "$ANSWER" = "y" ] || [ "$ANSWER" = "Y" ]
then
Access_Search
fi

echo "Enter file permissions criteria? (y/n) [n] \c"
read ANSWER
if [ "$ANSWER" = "y" ] || [ "$ANSWER" = "Y" ]
then
Permissions_Search
fi
}

Text_Search ()
{
case $SEARCH_COMPLETE in
true)
echo "Search for a text string within these files? (y/n) [n] \c"
read ANSWER
case $ANSWER in
[yY])
String_Search
;;
*)
exit 0
;;
esac
;;
*)
echo
echo "First, run a search (1-7)"
echo "You will then be able to search"
echo "the results for a text string"
Main_Menu
;;
esac
}

String_Search ()
{
echo "Please enter text to search for"
echo "--> \c"
read TEXT
echo
echo "Case-sensative search (y/n) [n] \c"
read ANSWER
case $ANSWER in
[yY])
GREP_STATEMENT="grep"
;;
*)
GREP_STATEMENT="grep -i"
;;
esac
CURRENT_FILE=0
TEXT_FILE_COUNT=0
FILE_HITS=0
TOTAL_HITS=0
echo
echo "$CUR_DATE" >> $TMP_LIST.Text_Hits
echo "$FIND_COMMAND" >> $TMP_LIST.Text_Hits
echo "Text Search: $TEXT" >> $TMP_LIST.Text_Hits

cat $TMP_LIST.Results|while read LINE
do
CURRENT_FILE=`expr $CURRENT_FILE + 1`
clear
echo "Scanning file $CURRENT_FILE of $MATCHES $LINE"
FILE_TYPE=`file $LINE|awk -F":" '{print $2}`
FILE_TEST=`echo $FILE_TYPE|grep -i text`
if [ ${#FILE_TEST} -gt 0 ]
then
TEXT_FILE_COUNT=`expr $TEXT_FILE_COUNT + 1`
HIT_COUNT=`$GREP_STATEMENT "$TEXT" $LINE|wc -l`
if [ $HIT_COUNT -gt 0 ]
then
echo
# echo "$HIT_COUNT occurences of [$TEXT] found in $LINE"
echo "$HIT_COUNT occurences of [$TEXT] found in $LINE" >> $TMP_LIST.Text_Hits
FILE_HITS=`expr $FILE_HITS + 1`
TOTAL_HITS=`expr $TOTAL_HITS + $HIT_COUNT`
fi
fi
done
echo
echo "$TEXT_FILE_COUNT text files found"
echo "$TEXT_FILE_COUNT text files found" >> $TMP_LIST.Text_Hits
echo "[$TEXT] was found in $FILE_HITS files"
echo "[$TEXT] was found in $FILE_HITS files" >> $TMP_LIST.Text_Hits
echo "Total hits for [$TEXT]: $TOTAL_HITS"
echo "Total hits for [$TEXT]: $TOTAL_HITS" >> $TMP_LIST.Text_Hits
echo
echo "Basic results saved in: $TMP_LIST.Results"
case $DETAILED_RESULTS in
true)
echo "Detailed results saved in: $TMP_LIST.Detail"
;;
esac
echo "Text search results saved in: $TMP_LIST.Text_Hits"
echo
}


Begin_Search ()
{
echo
FIND_COMMAND=`echo "find $SEARCH_PATH $SUB_DIRS $FOLLOW $PLUS_SIZE $MINUS_SIZE $USER $AGE_PLUS_DAYS $AGE_MINUS_DAYS $ACC_PLUS_DAYS $ACC_MINUS_DAYS $PERM -name $FILE_NAME -print"`
echo "Executing: $FIND_COMMAND"
echo "Searching...\c"
find $SEARCH_PATH $SUB_DIRS $FOLLOW $PLUS_SIZE $MINUS_SIZE $USER $AGE_PLUS_DAYS $AGE_MINUS_DAYS $ACC_PLUS_DAYS $ACC_MINUS_DAYS $PERM -name "$FILE_NAME" -print >> $TMP_LIST.Results &

SEARCHING="true"
until [ "$SEARCHING" = "false" ]
do
TEST=`ps -ef|grep $$|grep "find "|grep -v grep`
if [ ${#TEST} -gt 0 ]
then
# tail -1 $TMP_LIST.Results
echo "...\c"
else
echo "Search Complete! \c"
break
fi
done
echo
MATCHES=`cat $TMP_LIST.Results|wc -l`
echo "$MATCHES matches found."
if [ $MATCHES -lt 1 ]
then
exit 0
fi
echo
echo "note: detailed results will take more time"
echo "by running an ls -ld command against each match."
echo "Provide detailed results? (y/n) [n] \c"
read ANSWER
case $ANSWER in
[yY])
DETAILED_RESULTS="true"
echo "Formatting detailed results...\c"
echo "$CUR_DATE" >> $TMP_LIST.Detail
echo "$FIND_COMMAND" >> $TMP_LIST.Detail
echo "" >>$TMP_LIST.Detail
cat $TMP_LIST.Results|sort|while read LINE
do
echo "...\c"
FILE_DETAIL=`ls -ld $LINE`
echo $FILE_DETAIL >> $TMP_LIST.Detail
done
echo "Done!"
;;
*)
DETAILED_RESULTS="false"
;;
esac
echo
}

Display_Results ()
{
echo
if [ ${MATCHES} -gt 0 ]
then
echo "Basic results saved in: $TMP_LIST.Results"
case $DETAILED_RESULTS in
true)
echo "Detailed results saved in: $TMP_LIST.Detail"
;;
esac
echo
echo "Display results on screen? (y/n) [y] \c "
read ANSWER
case $ANSWER in
[nN])
ON_SCREEN="false"
;;
*)
case $DETAILED_RESULTS in
true)
echo
cat $TMP_LIST.Detail
echo
echo "Basic results saved in: $TMP_LIST.Results"
echo "Detailed results saved in: $TMP_LIST.Detail"
;;
*)
echo
cat $TMP_LIST.Results|sort
echo
echo "Basic results saved in: $TMP_LIST.Results"
esac
esac
echo
SEARCH_COMPLETE="true"
fi
}

Trapit ()
{
echo "Script aborted!... \c"
echo "Cleaning up... \c"
kill %1
kill -9 $$
}

################################################################################
# Main
################################################################################
trap Trapit 1 2 15 # cleanup if script is aborted

VERSION=`head -5 findit|grep Ver:|awk '{print $3}'`
echo
echo "Thank you for using findit version $VERSION"
echo "The latest version can always be found on awhq6394 in /usr/local/bin"
echo "Please send any questions/comments to Robert@JORDAN2000.com"
echo

SEARCH_COMPLETE="false"
DETAILED_RESULTS="true"
FILE_NAME="*"

Check_ID
Main_Menu
Search_Path
Sub_Directories
Symbolic_Links
Begin_Search
Display_Results
Text_Search
exit 0
 
end of script got cut off

must be a limit to how many lines
can be submitted?
 
Great stuff - looks like it will be my default search tool.
happy.gif
 
Robert,

If you put this into a FAQ you could edit just the one version of the script. What about a new FAQ category named "Useful Scripts"? Mike
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Yes, I think it would be great if you had a section
called "shell scripts" or "shell programming"
in the faq section as well as a forum.

I would also be nice to have a section
for shell script functions since many functions
in scripts can be easily copied and pasted
from one script to another.

I use functions over and over in many of my scripts
so I don't have to re-invent the wheel every time.

Here's an example of a function which is useful in a script.
Usually, you have to do two seperate echo or print statements if you want to display something on screen
as well as in a log file. This function displays to
the screen and writes the same output to a log file.

Write () # Display a message on screen and log to a file
# usage: Write "[Your message here]"
{
MESSAGE="$1"
eval "echo \"$MESSAGE\""|tee -a $LOG_FILE
}

All you have do is place the function in your script
above your main program code.

To use this function:

Write "Hello There!"

:)

Robert Robert G. Jordan

Robert@JORDAN200.com
Unix Admin, United Airlines
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top