As a SysAdmin &/or Developer, going through the log files or code is a daily chore. We do it hundreds of times everyday while at work. Some of us do it even when we are not at work 😉
Many a time, we need to search for a particular piece of text/string in either the log-files or the code. There are many text-editors available that you can use to drill into log-files or your code. Some editors also allow you to search inside directories.
But, if you want a simple, command-line utility that does the job for you, I have written a Bash Shell script that allows you to search for your string inside any given directory. It also counts the total occurrences and shows the line-number for those entries.
My script takes care of the OS (Tested on Mac, Ubuntu & RedHat) & performs installation of a small utility called figlet (according the OS) which I have used to produce an ASCII art for my script. You can press “n” when prompted, if you don’t want to install or use it.
You can download the script from my GitHub gist.
Below is the script in action (executed on Ubuntu).
If you don’t specify a directory, the script exits.
root@shashank-dbserver:/home/shashank/Desktop# ./textinfile.sh 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.
Script after specifying a valid directory.
root@shashank-dbserver:/home/shashank/Desktop# ./textinfile.sh /var/log/apache2/ Detecting OS of your machine.. Your OS is : - Ubuntu. Checking figlet insallation... 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. y You chose to install figlet. Installing it for your OS now :-) Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: linux-headers-4.4.0-81 linux-headers-4.4.0-81-generic linux-image-4.4.0-81-generic linux-image-extra-4.4.0-81-generic Use 'apt-get autoremove' to remove them. The following NEW packages will be installed: figlet 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 0 B/190 kB of archives. After this operation, 744 kB of additional disk space will be used. Selecting previously unselected package figlet. (Reading database ... 1548998 files and directories currently installed.) Preparing to unpack .../figlet_2.2.5-2_amd64.deb ... Unpacking figlet (2.2.5-2) ... Processing triggers for man-db (126.96.36.199-1ubuntu1) ... Setting up figlet (2.2.5-2) ... update-alternatives: using /usr/bin/figlet-figlet to provide /usr/bin/figlet (figlet) in auto mode _____ _ ___ _____ _ _ |_ _|____ _| |_|_ _|_ __ | ___(_) | ___ | |/ _ \ \/ / __|| || '_ \| |_ | | |/ _ \ | | __/> <| |_ | || | | | _| | | | __/ |_|\___/_/\_\\__|___|_| |_|_| |_|_|\___| Welcome to TextInFile! A cute, little utility that allows you to search for the text inside directories from the command-line :-) Enter the string that you want to search inside /var/log/apache2/...GET /static/4d441666/scripts/yui/menu/assets/skins/sam/menu.css HTTP/1.1 Searching for GET /static/4d441666/scripts/yui/menu/assets/skins/sam/menu.css HTTP/1.1 in /var/log/apache2/... GET /static/4d441666/scripts/yui/menu/assets/skins/sam/menu.css HTTP/1.1 was found 2 time(s) inside /var/log/apache2/. Below is your Search Result :-) /var/log/apache2/access_reverse_proxy.log.1:7:192.168.0.50 - - [25/Jun/2017:05:35:51 +0200] "GET /static/4d441666/scripts/yui/menu/assets/skins/sam/menu.css HTTP/1.1" 200 1654 "http://192.168.0.51/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36" /var/log/apache2/access_reverse_proxy.log.1:2807:10.0.2.2 - - [27/Jun/2017:03:44:31 +0200] "GET /static/4d441666/scripts/yui/menu/assets/skins/sam/menu.css HTTP/1.1" 200 1654 "http://localhost:8000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" This utility took 84 seconds to run(including the time you took to type/paste your string). That's all folks! Hope you liked TextInFile ;-)
If you provide an incorrect directory, the script shows an appropriate error & then exits.
root@shashank-dbserver:/home/shashank/Desktop# ./textinfile.sh /var/log/apache The directory /var/log/apache that you specified does't exist. Please choose the correct directory.