468x60d

May 13
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

There are times - many in fact - when I need to find a file, or multiple files containing a particular string. For example, I’m developing a web application, and need to find out which pages call a particular class method, say, all the files which call the getTopTenCDs() method - I can do this by opening a Terminal, and running grep (Global Regular Expression Print), with a couple of options:

grep -r -n 'getTopTenCDs()' /var/www/cdcollection/*

This will return a list of files, and the lines within those files, and the line numbers containing the string ‘getTopTenCDs()’ in the directory /var/www/cdcollection/. The -n option tells grep to output the line numbers, the -r option tells grep to search recursively through the sub-directories too, and * tells it to look in all files - which can be changed to *.php etc: /var/www/cdcollection/*.php

So, the output would be something along the lines of:

/var/www/cdcollection/viewcd.php:192:   $topTen = $cd->getTopTenCDs();
/var/www/cdcollection/sidebar.php:215:   $topTen = $cd->getTopTenCDs();

If I just want a list of files without the lines containing the search string, I can use the -l option:

grep -r -l 'getTopTenCDs()' /var/www/cdcollection/*

which would simply return:

/var/www/cdcollection/viewcd.php
/var/www/cdcollection/sidebar.php

As the name implies, grep uses Regular Expressions, and therefore the search string can contain the usual RegEx operatiors: . ? * + {n} etc. RegEx is out of scope for this post I’m affraid - there are dozens of great tutorials available on RegEx, and applying RegEx to grep.

One final point about grep: if the search string begins with a hyphen (minus sign, or whatever you want to call “-”), the search string needs to be preceded with the -e option:

grep -r -e '-starts with a hyphen' /var/www/cdcollection/*

so that grep doesn’t mistake the search pattern for an option! Forget the -e option, and you’ll get:

grep: invalid option -- $
Usage: grep [OPTION]… PATTERN [FILE]…
Try `grep –help’ for more information.

Also, remember the ever useful pipe | and output to file > commands. If the list printed by grep is huge, you can simply pipe it to more:

grep -r 'getTopTenCDs()' /var/www/cdcollection/* | more

or, if you want to save it to a file to peruse later:

grep -r 'getTopTenCDs()' /var/www/cdcollection/* > ~/grep_output.txt

Always useful.

Finally, remember to take a look at the grep man page:

man grep

Which contains a world of useful information, including a wealth of options I haven’t covered.


written by Hodge \\ tags: , , ,

Apr 01
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Google DesktopLike most users, I like to be able to locate files as quickly as possible, without having to plough through dozens of directories trying to remember where I placed that elusive Tibetan Flag image. There are several tools available to aid users searching their computers - the default install of Ubuntu has a built in search, the Tracker Search Tool.

Over a stretch of time, I’ve installed and used several such tools - Beagle, Google Desktop, and others. Both Beagle and Google Desktop offer a simple to use GUI, and are easy to install and set up. Beagle is available via the Ubuntu Repositories:

sudo apt-get install beagle

while Google Desktop is available to download from their website. Once the Google Desktop has downloaded, it can either be installed form the command line, or simply by double-clicking on the package.

Beagle SearchBoth packages will periodically scan your system, and index all of the files it finds (by default, the home directory, and additionally any others you add) which provides faster searching capabilities. They do use a small amount of system resources during this process, and they each use a fair amount of disk space to store the indexes (Google Desktop is currently using 500+Mb on my system, which in total has 9Gb of files and applications - much of that may actually be to do with Google having indexed my gmail inbox too!).

Find | grepThey’re great applications if you like visual feedback, and an interactive GUI, but even though I currently have Google Desktop installed (I’ve since removed beagle, to salvage some space, and will most likely remove Google Desktop eventually too), I rarely use it. The colourful icon sits next to my clock, waiting to be clicked, and the search box even pops up when I mistakenly double tap my CTRL key, but it’s rarely put to work. Invariably, I’ll use the combination of commands that has never failed in finding what I want - find, and grep. I can pass a directory to the find command, and pipe the results through grep to find any file I want, anywhere on my system:

~/$ sudo find / | grep TibetFlag.png
/home/tenzin/Desktop/TibetFlag.png
~/$

Well, there you go - it was on my Desktop all along…

Tibetan Flag

I usually use sudo, because I want the find command to be able to access all directories (I don’t need to use sudo if I’m just searching my home directory). I’m telling find to start the search in the root / directory - although this could be any directory: /home, /usr, /usr/share etc. then I’m piping | the results through to grep to match the actual (or partial) name of the file - I could have just passed “Tibet” as a parameter to grep, if I couldn’t remember the full filename.

Most of the time, this combination of commands will return many more results - in which case, it can be piped once again through more, which will return single pages of results that can be scrolled using the space bar, Page Down, or down keys:

sudo find / | grep Tibet | more

Alternatively, I could have just created a file to dump the results into, and peruse that file later with a text editor:

sudo find / | grep Tibet > tibetFlagSearch.txt

You can of course, also just use the find command, with the -name option:

sudo find / -name 'TibetFlag.png'
sudo find / -name 'TibetFlag.*'
sudo find / -name '*.png'

etc.

What can I say - I’m an old skool Command Linerererer…


written by Hodge \\ tags: , , , ,

Webloogle Blog Directory