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
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