Search file contents in multiple directories with Recursive grep
Posted by Hodge on May 13, 2008 in Linux, Ubuntu • 1 comment •
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.
Something not quite right? Inaccuracies or invalid code? Didn’t work for you? Don’t like me using Ss instead of Zs? Add a comment below! All comments are welcome. Except spam, because spam is a bit crap.
|
|
Seis De Mayo $4.29 It wasn’t long after the recording of this, Trey Anastasio’s third–and most warmly idiosyncratic–solo album, that the Phish frontman/guitarist announced that his improvisation-fueled, cult-fave band would record no more. Longtime Phish-heads (or admirers of Anastasio’s previous free-form jazz excursions with Les Claypool and Stewart Copeland and more conventional solo work) may find this ambitio… |
|
|
Sapphire Video Card 100314-3l Hd 6870 1gb Ddr5 256bit Pci Express D-Dvi/Hdmi/Display Port Retail $222.69 PCI Express based PC is required with one X16 lane graphics slot available on the motherboard.4X75 Watt 6-pin PCI Express power connector is required for CrossFireX system.2X75 Watt 6-pin PCI Express power connector is required.1024MBMinimum of system memory.Installation software requires CD-ROM drive.DVD playback requires DVD drive.Blu-ray/HD DVD playback requires Blu-ray/HD DVD drive.Blu-ray/HD … |
|
|
ASUS Lamborghini VX6-PU17-BK 12.1-Inch Eee PC Netbook (Black) $685.70 Asus Lamborghini VX6-PU17-BK 12.1″ LED Netbook – Atom D525 1.80 GHz – Black VX6-PU17-BK 41… |
|
|
T101MT-BU17-BK 10.1 Netbook $599.99 Asus Eee PC T101MT-BU17-BK 10.1″ LED Net-tablet PC – Atom N450 1.66 GHz – Black T101MT-BU17-BK 202… |
|
|
Laptop Computer Security Cable Lock (Silver) for Asus laptop $7.99 Laptop Computer Security Cable Lock (Silver) for Asus laptop . Laptop Computer Security Cable Lock for you to use to lock your laptop computer in an easy way.Features:Lock down your notebooks laptop computer with this unique, individual keys and locks for each computer. Galvanized steel cable for strong protectionEffective protection, anti-thief design keeps the sneakers away The T-bar mechanism b… |
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.





[...] Search file contents in multiple directories with Recursive grep | 64 Bit Jungle (tags: linux howto grep recursive pattern hacks commandline cli) [...]