It ’s fast , it ’s brawny , and its very name suggest that it does something technological : grep . With this workhorse of the statement line of work , you could quickly find textbook hidden in your filing cabinet . Understanding grep is the first dance step in joining the guild of command - line wizards .

Why Not Use the Finder?

It ’s easy to find Indian file with the Finder when you know their figure . But the grep command is a time - saver when you ’re trying to find what’sinsidefiles . you’re able to use grep easy from the command line to search for specific schoolbook , and you ’ll get results in secondment . ( you may also use grep within certain text editors . )

The Finder provide a like function : the Find By Content hunting . ( Press Command - F in the Finder , select Content in the Search For Items Whose pop out - up menu , and enter a hunt string in the schoolbook line of business . ) But the Finder search only inside files it has indexed , and it ignores hidden system files unless you expressly take to search for visible and invisible files and tally your organization brochure to the search .

The Finder also miss grep ’s flexibility : while it ’s good for searching for a specific discussion ( for example , Walden ) , it becomes less useful when you desire to ascertain a longer string . Search forWalden Pond , and it returns all files that arrest eitherWaldenorPond .

Using grep also gives you get at toregular expression . These combinations of letter of the alphabet , numbers , wild cards , and other special characters rent you find much more than mere word . you could search for just about any string of fibre : IP destination in logarithm files ; phone numbers in striking lists ; or specific strings containing a serial publication of numbers , letters , or other characters . Using regular expressions , you ’re limited only by your imagery .

Get a Grip on Grep

The grep command looks inside one or several data file for the drawstring , or school text , you specify . Its phrase structure is : grep options search_string file ….

At its most canonical , you tell grep what to look for and where : grep AppleTalk /etc / services , for model . Here , you tell grep to look for AppleTalk in the services file located in the /etc directory . ( This useful file stop a leaning of web interface act for commonly used services . ) The command displays each line of merchandise that contains your search string :

And so on . you’re able to expend the conversant star ( * ) wild card to have grep search a group of files in the current working directory , as ingrep Walden * . This command searches all the files in the current directory for the wordWalden , render the chase :

And so on . As the above exercise shows , the lookup returns several matches . The first , Walden.doc , is a Word file cabinet . The grep command calls such proprietary single file typesbinary files . It ca n’t display the contents of binary files , but it can search inside them and say you if something match . The next examples occur from unmistakable - text file , for which grep can exhibit the results . You see the single file name , espouse by a match of the search string in its context of use .

you could search for any multiword text strand by enfold the string in exclusive quotes . For representative , if you want to search for the phraseWalden Pond , you’d typegrep ' Walden Pond ' * .

notice that grep does n’t like Macintosh personal credit line breaks . It returns lines hold in the search string , but it does n’t see Mac line breaks as anything other than characters . In such a sheath , the “ melodic line ” it returns is the intact file ; this can dump a lot of text into your Terminal window .

In the old example , grep ran in a specific brochure , checking all the files it contained . What if you desire to run grep on a folderandits subfolders , or you want grep to appear for the string regardless of case ? You need to addoptions . For example , to search forWaldenanywhere in a brochure or its subfolders , use the - r(recursive ) choice : grep -r Walden ~/Documents/ * .

Fine-Tune Your Searches

The grep instruction has several choice that countenance you fine - melodic phrase the path you look for for textbook , as well as the kind of resultant role grep returns . Get started with the helpful selection listed here . ( Note that you may also combine choice — for representative , grep -rl Waldensearches subfolders and returns only a listing of Indian file containing the wordWalden . )

Search for Multiple Strings

Using the tobacco pipe ( | ) , a Unix redirection operator , you may evidence grep to search for more than one string . Say you want to find files hold bothWaldenandPondon the same assembly line . You ’d utilise this command : grep Walden * | grep Pond . The first part of the instruction look for the wordWaldenin any files in the current directory , and the 2d course another grep command on the results of the first dictation . terminus exhibit only the last results of the two mastery flux .

You could string together many grep commands , like this :

. This bid looks in a particular dictionary file for Book containing the lowercase lettera . It then wait for Word containingein the results , and so on , in conclusion returning only those words that contain all five vowels .

This purpose of grep is most useful when you ’re searching for specific string in output from other commands . In this way you’re able to whittle down retentive and complex output . For example , here ’s a common direction to find the process ID of a programme that ’s stuck so you’re able to force - quit it from the command line : typeps -ax | grep Finder .

This dictation first gets a list of all processes running on your Mac , and then sieve through this inclination count for lines containing the wordFinder . For case , your results might be as follows :

The physical process ID is the first telephone number on each line of merchandise ; here , the Finder is 390 . ( You ’ll notice the command also come back itself ; since the wordFinderis in the grep command , that gets list as well . ) So if the Finder is stuck , this gives you the information you ask to force - stop it . Now you would type the command ( where the process ID is the final argument)kill -9 390 .

Add Regular Expressions to the Mix

While you have seen some of the power of the grep command , you could go much further usingregular expressions , particular combinations of type that act as wild board . Here are a few case .

If you ’re not sure how to spell the wordseparate , for example ( is that anaor ane ? ) , run this program line to check the special dictionary file hide in your Mac ’s innards :

You ’ll get back a list of words that includesseparate , separately , separately , separateness , andseparates .

Note the two special graphic symbol in the command : the caret ( ^ ) and the dot ( . ) . The caret tell grep to search for the twine at the source of a line , so the result do n’t admit Son likeinseparate . The dot tally any graphic symbol except a new line .

What if you need to bump all the phone number in a specific Indian file ? Try this command , which will find phone number in the555 - 1234format :

Each of the[0 - 9]wild cards matches any character in the mountain range specified in brackets . you could use ranges such as[1 - 3]to demarcation line your hunt to specific train . This works for letters , too:[a - n]matches any minuscule fibre fromaton . you’re able to build your own grasp with sets of character — for example,[aeiou]will match only vowels . you may learn more about regular expressions by typingman grepin Terminal , or by consulting Jeffrey Friedl ’s splendid bookMastering Regular Expressions , second edition ( O’Reilly , 2002 ) . With a moment of practice , you ’ll be using grep and regular face to find anything you want .

Kirk McElhearn is the author ofThe Mac OS X Command wrinkle : Unix Under the Hood(Sybex , 2004 ) . His web log , Kirkville , has article and tips on using the command line with Mac OS X. ]