findgrep version 6.


SUBMITTED BY: Guest

DATE: Dec. 4, 2013, 1:06 p.m.

FORMAT: Text only

SIZE: 4.3 kB

HITS: 818

  1. #!/bin/bash
  2. # FINDGREP INFO ########################
  3. PROGRAMINFO="findgrep version 6.
  4. This Script simplifies the recursive search of any given string in a given directory.
  5. It combines find and grep. Results are logged for later viewing and analyzing.
  6. findgrep remembers directories and search strings for following searches."
  7. PROGRAMINFO2="To change the path for logging of queries, please edit \$SAVEFILE in this script. Searches are case insensitive by default. Written by LinuxNetzer. www.netz10.de. contact: linuxnetzer-*-aet-*-netz10.de. findgrep v6 is released under GPL v3."
  8. # CHANGELOG #############################
  9. # Script now remembers directories and search strings.
  10. # Logfile now moved to /tmp.
  11. # Changed to more userfriendly interface.
  12. # Added many comments for better script reading.
  13. # DEBUG MODE ############################
  14. # set -x # uncomment for debugging
  15. # DEFINING SESSION SETTINGS #############
  16. ROOTDIR="$HOME/.findgrep" # place to store files
  17. SAVEDIR="/$ROOTDIR/remember.dir" # last search directory
  18. SAVESTRING="/$ROOTDIR/remember.string" # last search string
  19. #########################################
  20. if [ ! -f $ROOTDIR ] # file storage directory exists?
  21. then
  22. mkdir $ROOTDIR # if not: create directory
  23. fi
  24. #########################################
  25. if [ ! -f $SAVEDIR ] # last search directory defined?
  26. then
  27. echo $HOME > $SAVEDIR # if not, set to: home directory
  28. fi
  29. #########################################
  30. if [ ! -f $SAVESTRING ] # last search string defined?
  31. then
  32. echo $USER > $SAVESTRING # if not, set to: user name
  33. fi
  34. # VARIABLES #############################
  35. PROGNAME="findgrep v6" # name of program
  36. SAVEFILE="$ROOTDIR/.findgrep.log" # log file location
  37. SEARCHDIR="`cat $SAVEDIR`" # load search directory
  38. SEARCHSTRING="`cat $SAVESTRING`" # load search string
  39. DIVIDER="--------------------------------------------------" # better viewing
  40. # FUNKTION HEADER #######################
  41. HEADER () {
  42. clear
  43. echo " __ _ _
  44. / _(_)_ __ __| | __ _ _ __ ___ _ __
  45. | |_| | '_ \ / _. -- _. | '__/ _ \ '_ \
  46. | _| | | | | (_| | (_| | | | __/ |_) |
  47. |_| |_|_| |_|\__,_|\__, |_| \___| .__/
  48. |___/ |_| " | grep --color .
  49. echo " Recursive Text String Search Tool" | grep --color .
  50. echo
  51. }
  52. # CHOOSE DIRECTORY TO SEARCH ############
  53. HEADER
  54. echo "Search in Directory: $SEARCHDIR ?" | grep --color ?
  55. echo "If OK, press ENTER. To change please enter new path."
  56. read -e INPUT
  57. if [ "$INPUT" != "" ]
  58. then echo $INPUT > $SAVEDIR
  59. SEARCHDIR="$INPUT"
  60. fi
  61. HEADER
  62. echo "Searching in: $SEARCHDIR " | grep --color $SEARCHDIR
  63. # CHOOSE TEXTSTRING TO SEARCH ####################
  64. echo "Search for: $SEARCHSTRING ?" | grep --color ?
  65. echo "if OK, press ENTER. To change please enter new searchstring."
  66. read -e INPUT2
  67. if [ "$INPUT2" != "" ]
  68. then echo $INPUT2 > $SAVESTRING
  69. SEARCHSTRING="$INPUT2"
  70. fi
  71. echo "$SEARCHSTRING" | grep --color .
  72. # SUMMARY ########################################
  73. HEADER
  74. echo "SUMMARY:"
  75. echo "Searching in: $SEARCHDIR" | grep --color $SEARCHDIR
  76. echo "Searching for: $SEARCHSTRING" | grep --color $SEARCHSTRING
  77. echo "Logging to: $SAVEFILE" | grep --color $SAVEFILE
  78. echo $DIVIDER
  79. echo "Now starting recursive search for $SEARCHSTRING in $SEARCHDIR..."
  80. echo $DIVIDER
  81. echo "SEARCH RESULTS:" | grep --color "SEARCH RESULTS"
  82. export GREP_COLOR="31"
  83. # EXECUTE #########################################
  84. find $SEARCHDIR -type f -exec grep -i $SEARCHSTRING --exclude=$SAVEFILE /dev/null {} \; 2> /dev/null | tee $SAVEFILE | grep -i --color $SEARCHSTRING
  85. # DISPLAY RESULTS ################################
  86. echo $DIVIDER
  87. export GREP_COLOR="01;31" # fett, rot
  88. echo "Search in $SEARCHDIR: finished" | grep --color .
  89. echo "Found \"$SEARCHSTRING\": `cat $SAVEFILE | wc -l`" | grep --color .
  90. echo "Results saved to: $SAVEFILE" | grep --color .
  91. echo $DIVIDER
  92. echo $PROGRAMINFO2
  93. echo $DIVIDER
  94. exit

comments powered by Disqus