Skip to content

Instantly share code, notes, and snippets.

@shashank-ssriva
Created August 22, 2017 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shashank-ssriva/6595bfe80fc75346aeed64acf7b0802f to your computer and use it in GitHub Desktop.
Save shashank-ssriva/6595bfe80fc75346aeed64acf7b0802f to your computer and use it in GitHub Desktop.
#TextInFile! A cute, little utility that allows you to search for the text inside directories from the command-line.
#Author : - Shashank Srivastava
#Date : - 16 August 2017
#set -x
#!/bin/bash
echo ""
if [ $# -eq 0 ]
then
echo "Error!! Please enter absolute path of the directory inside which you want to perform the search. Run this utility as ./textinfile /path/to/directory. Prefix sudo if needed."
echo ""
exit
fi
DIRNAME=$1
if [ -d $DIRNAME ]
then
echo "Detecting OS of your machine..."
echo ""
OS=`uname -v | awk '{print$1}' | sed 's:.*-::'`
if [ "$OS" == "Darwin" ]
then
echo "Your OS is : - Mac OS X."
elif [ "$OS" == "Ubuntu" ]
then
echo "Your OS is : - Ubuntu."
else
echo "Your OS is : - RHEL."
fi
echo ""
echo "Checking figlet insallation..."
echo ""
which figlet
if [ $? -ne 0 ]
then
echo "This utility requires a package called figlet in order to display a cool ASCII art ;-) Do you want to install it? Its a one time process only. You can safely ignore this insallation. Press y to install or n to cancel."
read ANSWER
if [ $ANSWER == "y" ] && [ $OS == "Darwin" ]
then
echo "You chose to install figlet. Installing it for your OS now :-)"
brew install figlet
elif [ $ANSWER == "y" ] && [ $OS == "Ubuntu" ]
then
echo "You chose to install figlet. Installing it for your OS now :-)"
sudo apt-get install figlet -y
elif [ $ANSWER == "n" ]
then
echo "You chose not to install figlet :-( No cool ASCII art will be diplayed!"
fi
else
echo "figlet already installed! Lets get started :-)"
fi
echo "TextInFile" | figlet 2>/dev/null
echo "Welcome to TextInFile! A cute, little utility that allows you to search for the text inside directories from the command-line :-)"
echo ""
echo -n "Enter the string that you want to search inside $DIRNAME..."
read TEXT_STRING
echo ""
echo "Searching for $TEXT_STRING in $DIRNAME..."
echo ""
NUM=`grep -r -i "$TEXT_STRING" $DIRNAME | wc -l`
if [ $NUM -eq 0 ]
then
echo "Sorry, $TEXT_STRING wasn't found anywhere inside $DIRNAME."
else
echo "$TEXT_STRING was found $NUM time(s) inside $DIRNAME."
echo ""
echo "Below is your Search Result :-)"
fi
echo ""
grep -r -i "$TEXT_STRING" -n $DIRNAME
echo ""
echo "This utility took $SECONDS seconds to run(including the time you took to type/paste your string)."
echo ""
echo "That's all folks! Hope you liked TextInFile ;-)"
else
echo "The directory $DIRNAME that you specified does't exist. Please choose the correct directory."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment