Well tonight I was supposed to be pulling the new project to bits that I’m working on but I ran into a problem – let me explain.
Every time I have wanted to find some text in a directory I’ve had to either search the internet or my bash history for the right command. With this in mind I thought I may as well take a little time to create a function that would just search for the string that I want. After all while I’m learning the layout etc of this project I’m going to need it. Not knowing where all the files are as yet and what they all do it’s going to be a lot easier to run the function and see what files the string turns up in.
The function to put in your ~/.bashrc or similar file
function find-string() {
# re-iterate what we're looking for
echo 'looking for "'$1'"'
# perform a search on all files
find ./ -xdev -type f -print0 | \
# process each file through the grep,
# --colour=always = always show the coloured version
# --ignore-case = search is not case-sesitive
# --line-number = show the line numbers after the filename
# --context=1 = this will print 1 line both above and below the line where the string occurs
# "${1}" = finally search for the passed variable
xargs -0 grep --colour=always --ignore-case --line-number --context=1 "${1}" | \
# replace each tab with 2 spaces to cut down on the amount of indentation in
# the search results from files with large areas of indentation
sed -re 's/\t/ /g'
}
And to get it up and running just run
source ~/.bashrc
to enable your system to see the function and then when you want to use it you can simply enter something like:
find-string viewer-thumbnails-wrapper
find-string being the function and in this case we are searching for file containing viewer-thumbnails-wrapper which will bring results like these:
Looking for "viewer-thumbnails-wrapper"
./css/viewer.css-418-/* Thumbnails */
./css/viewer.css:419:.viewer-thumbnails-wrapper{
./css/viewer.css-420- background: #FFF;
--
./js/mediagallery.js-61- min_thumbs: 5,
./js/mediagallery.js:62: thumbwrapper: 'div.viewer-thumbnails-wrapper',
./js/mediagallery.js-63- btn_thumbs_show: 'a.viewer-thumbs',
Ok, that’s it, if you can make use of the this function then go ahead and copy and paste.”
Enjoy!
if there’s any help I can offer then let me know in the comments section below.