Press the 'Command' and 'F' keys simultaneously to launch the 'Finder' window. Enter the name of the deleted file in the search bar at the top right part of the window. Click on the file you found in Finder and drag it onto your desktop or the location you wish to save the file.
To quickly find any text string within any text file, try this from a terminal window: grep -l [text to find] [files to look in] For example, grep -l 123abc *.html will list the name of any file in the current directory that ends in.html and contains the string 123abc. A command line prompt is a way of interacting with your computer without a graphical interface. While your modern Mac uses a graphical user interface, (GUI) older computers used only command prompts. The command line usually gives you more options than the GUI. Command-I: Italicize the selected text, or turn italics on or off. Command-K: Add a web link. Command-U: Underline the selected text, or turn underlining on or off. Command-T: Show or hide the Fonts window. Command-D: Select the Desktop folder from within an Open dialog or Save dialog. Control-Command-D: Show or hide the definition of the selected word. Shift-Command-Colon (:): Display the Spelling and Grammar window. Command-Semicolon (;): Find misspelled words in the document.
It's fast, it's powerful, and its very name suggests that it does something technical: grep. With this workhorse of the command line, you can quickly find text hidden in your files. Understanding grep is the first step in joining the guild of command-line wizards.
Why Not Use the Finder?
It's easy to find files with the Finder when you know their names. But the grep command is a time-saver when you're trying to find what's inside files. You can use grep easily from the command line to search for specific text, and you'll get results in seconds. (You can also use grep within certain text editors.)
The Finder offers a similar function: the Find By Content search. (Press Command-F in the Finder, select Content in the Search For Items Whose pop-up menu, and enter a search string in the text field.) But the Finder searches only inside files it has indexed, and it ignores hidden system files unless you expressly choose to search for visible and invisible files and add your System folder to the search.
The Finder also lacks grep's flexibility: while it's good for searching for a specific word (for example, Walden ), it becomes less useful when you want to find a longer string. Search for Walden Pond, and it returns all files that contain either Walden or Pond.
Using grep also gives you access to regular expressions. These combinations of letters, numbers, wild cards, and other special characters let you find much more than mere words. You can search for just about any string of characters: IP addresses in log files; phone numbers in contact lists; or specific strings containing a series of numbers, letters, or other characters. Using regular expressions, you're limited only by your imagination.
Get a Grip on Grep
The grep command looks inside one or several files for the string, or text, you specify. Its syntax is:
grep options search_string file..
.![Search Search](https://cdn1.tekrevue.com/wp-content/uploads/2014/08/text-to-speech-osx-voices.png)
At its most basic, you tell grep what to look for and where:
grep AppleTalk /etc/services
, for example. Here, you tell grep to look for AppleTalk in the services file located in the /etc directory. (This useful file contains a list of network port numbers for commonly used services.) The command displays each line that contains your search string:And so on. You can use the familiar asterisk (
*
) wild card to have grep search a group of files in the current working directory, as in grep Walden *
. This command searches all the files in the current directory for the word Walden, returning the following:And so on. As the above example shows, the search returns several matches. The first, Walden.doc, is a Word file. The grep command calls such proprietary file types binary files. It can't display the contents of binary files, but it can search inside them and tell you if something matches. The next examples come from plain-text files, for which grep can display the results. You see the file name, followed by a match of the search string in its context.
You can search for any multiword text string by enclosing the string in single quotes. For example, if you want to search for the phrase Walden Pond,you'd type
grep 'Walden Pond' *
.Note that grep doesn't like Macintosh line breaks. It returns lines containing the search string, but it doesn't see Mac line breaks as anything other than characters. Text editor for mac sierra. In such a case, the “line” it returns is the entire file; this can dump a lot of text into your Terminal window.
In the previous example, grep ran in a specific folder, checking all the files it contained. What if you want to run grep on a folder and its subfolders, or you want grep to look for the string regardless of case? You need to add options. For example, to search for Waldenanywhere in a folder or its subfolders, use the
-r
(recursive) option: grep -r Walden ~/Documents/*
.Fine-Tune Your Searches
To Find… | Use This Option | Example |
---|---|---|
Text in subfolders | -r | grep -r Walden ~/Documents/* Finds Walden in any file in any subfolder of ~/Documents. |
Whole words only | -w | grep -w live Finds only live ; does not find liver , lives , lived , and so on. |
Case-insensitive text | -i | grep -i pond Finds pond , POND , or Pond . |
File names only | -l | grep -l Walden Finds files containing Walden , but returns only a list of file names. |
Number of occurrences only | -c | grep -c Walden Returns the names of files containing Walden and the number of hits in each file. |
The grep command has several options that let you fine-tune the way you search for text, as well as the kind of results grep returns. Get started with the helpful options listed here. (Note that you can also combine options—for instance,
grep -rl Walden
searches subfolders and returns only a list of files containing the word Walden .)Search for Multiple Strings
Using the pipe (
|
), a Unix redirection operator, you can tell grep to search for more than one string. Say you want to find files containing both Walden and Pondon the same line. You'd use this command: grep Walden * | grep Pond
. The first part of the command looks for the word Walden in any files in the current directory, and the second runs another grep command on the results of the first command. Terminal displays only the final results of the two commands combined.You could string together many grep commands, like this:
. This command looks in a special dictionary file for words containing the lowercase letter a. It then looks for words containing e in the results, and so on, finally returning only those words that contain all five vowels.
This function of grep is most useful when you're searching for specific strings in output from other commands. In this way you can whittle down long and complex output. For example, here's a common way to find the process ID of a program that's stuck so you can force-quit it from the command line: type
ps -ax | grep Finder
.This command first gets a list of all processes running on your Mac, and then sifts through this list looking for lines containing the word Finder. For example, your results might be as follows:
The process ID is the first number on each line; here, the Finder is 390. (You'll notice the command also returns itself; since the word Finderis in the grep command, that gets listed as well.) So if the Finder is stuck, this gives you the information you need to force-quit 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 can go much further using regular expressions, special combinations of characters that act as wild cards. Here are a few examples.
If you're not sure how to spell the word separate, for example (is that an a or an e?), run this command to check the special dictionary file hidden in your Mac's entrails:
![How to search for text command in macbook air How to search for text command in macbook air](https://www.wikihow.com/images/thumb/4/4d/Get-to-the-Command-Line-on-a-Mac-Step-10.jpg/aid1007661-v4-728px-Get-to-the-Command-Line-on-a-Mac-Step-10.jpg)
You'll get back a list of words that includes separate, separately, separately, separateness, and separates.
Note the two special characters in the command: the caret (
^
) and the dot (.
). The caret tells grep to search for the string at the beginning of a line, so the results don't include words like inseparate. The dot matches any character except a new line.What if you want to find all the phone numbers in a specific file? Try this command, which will find phone numbers in the 555-1234 format:
.
Each of the
[0-9]
wild cards matches any character in the range specified in brackets. You can use ranges such as[1-3]
to limit your search to specific strings. This works for letters, too:[a-n]
matches any lowercase character from a to n.You can build your own range with sets of characters—for example, [aeiou]
will match only vowels. You can learn more about regular expressions by typingman grep
in Terminal, or by consulting Jeffrey Friedl's excellent book Mastering Regular Expressions, second edition (O'Reilly, 2002). With a bit of practice, you'll be using grep and regular expressions to find anything you want.How To Search For Text Command In Macbook
Kirk McElhearn is the author of The Mac OS X Command Line: Unix Under the Hood (Sybex, 2004). His blog, Kirkville, has articles and tips on using the command line with Mac OS X. ]
Note: When you purchase something after clicking links in our articles, we may earn a small commission. Read our affiliate link policy for more details.
If the weird name throws you, 'grep' is an acronym for 'general regular expression program'. If that doesn't help, it's probably because you're wondering what a
regular expression ('re' or 'regex') is. Basically, it's a pattern used to describe
a string of characters, and if you want to know aaaaaaall about them, I highly
recommend reading Mastering Regular Expressions by Jeffrey Friedl and
published by Unix 端ber-publisher O'Reilly & Associates.
Regexes (regices, regexen, ..the pluralization is a matter of debate) are an extremely
useful tool for any kind of text processing. Searching for patterns with grep is
most people's first exposure to them, as like the article says, you can use them to search
for a literal pattern within any number of text files on your computer. The cool thing is
that it doesn't have to be a literal pattern, but can be as complex as you'd like.
useful tool for any kind of text processing. Searching for patterns with grep is
most people's first exposure to them, as like the article says, you can use them to search
for a literal pattern within any number of text files on your computer. The cool thing is
that it doesn't have to be a literal pattern, but can be as complex as you'd like.
The key to this is understanding that certain characters are 'metacharacters', which have
special meaning for the regex-using program. For example, a plus character (+) tells the
program to match one or more instances of whatever immediately precedes it, while parentheses
serve to treat whatever is contained as a unit. Thus, 'ha+' matches 'ha', but it also matches
'haa' and 'haaaaaaaaaaa', but not 'hahaha'. If you want to match the word 'ha', you can use
'(ha)+' to match one or more instances of it, such as 'hahaha' and 'hahahahahahahahaha'.
Using a vertical bar allows alternate matching, so '(ha|ho)+' matches 'hohoho', 'hahaha', and
'hahohahohohohaha'. Etc.
special meaning for the regex-using program. For example, a plus character (+) tells the
program to match one or more instances of whatever immediately precedes it, while parentheses
serve to treat whatever is contained as a unit. Thus, 'ha+' matches 'ha', but it also matches
'haa' and 'haaaaaaaaaaa', but not 'hahaha'. If you want to match the word 'ha', you can use
'(ha)+' to match one or more instances of it, such as 'hahaha' and 'hahahahahahahahaha'.
Using a vertical bar allows alternate matching, so '(ha|ho)+' matches 'hohoho', 'hahaha', and
'hahohahohohohaha'. Etc.
There are many of these metacharacters to keep in mind. Inside brackets ([]), a carat (^)
means that you don't want to match whatever follows inside the brackets. For Magritte
fans, '[^(a cigar)]' matches any text that is not 'a cigar'. The rest of the time, the carat tells
the program to match only at the beginning of a line, while a dollar sign ($) matches only at
the end. Therefore, '^everything$' matches the word 'everything' only when it is on a line all
by itself and '^[^(anything else)]' matches all lines that do not begin with 'anything else'.
means that you don't want to match whatever follows inside the brackets. For Magritte
fans, '[^(a cigar)]' matches any text that is not 'a cigar'. The rest of the time, the carat tells
the program to match only at the beginning of a line, while a dollar sign ($) matches only at
the end. Therefore, '^everything$' matches the word 'everything' only when it is on a line all
by itself and '^[^(anything else)]' matches all lines that do not begin with 'anything else'.
The period (.) matches any character at all, and the asterisk (*) matches zero or more times.
Compare this to the plus, which matches one or more times -- a subtle but important
difference. A lot of regular expressions look for '.*', which is zero or more of anything
(that is, anything at all). This is useful when searching for two things that might or might
not have anything else (that you probably don't care about) between them: 'foo.*bar' will match
on 'foobar', 'foo bar' & 'foo boo a wop bop a lop bam boo bar'. Changing the previous example
to a plus, 'foo.+bar', requires that anything -- come between foo and bar, but it doesn't matter
what, so 'foobar' doesn't match but the other two examples given do match.
Compare this to the plus, which matches one or more times -- a subtle but important
difference. A lot of regular expressions look for '.*', which is zero or more of anything
(that is, anything at all). This is useful when searching for two things that might or might
not have anything else (that you probably don't care about) between them: 'foo.*bar' will match
on 'foobar', 'foo bar' & 'foo boo a wop bop a lop bam boo bar'. Changing the previous example
to a plus, 'foo.+bar', requires that anything -- come between foo and bar, but it doesn't matter
what, so 'foobar' doesn't match but the other two examples given do match.
For details, try the man pages -- 'man grep'. There are a lot of different versions of the
program, so details may vary. All of this should be valid for OSX though.
program, so details may vary. All of this should be valid for OSX though.
Confusing? Maybe, but regular expressions aren't that bad when you get used to them, and
they can be a very useful tool to take advantage of it you know what you're doing. An example.
they can be a very useful tool to take advantage of it you know what you're doing. An example.
Let's say you have an website stored on your computer as a series of html documents.
As a cutting edge developer, you've seen the CSS light and want to delete all the
tags wherever they're just saying e.g. face='sans-serif' &/or size='12', because the
stylesheet can now do that for you. On the other hand, it's possible that the patterns
'face='sans-serif' or 'size='12' could show up in normal text (though admittedly
that's unlikely). In fact, what you really want to know is wherever those patterns show up in
a font tag, but you don't care about anywhere else that they might appear. Here's one way to
find that pattern:
As a cutting edge developer, you've seen the CSS light and want to delete all the
tags wherever they're just saying e.g. face='sans-serif' &/or size='12', because the
stylesheet can now do that for you. On the other hand, it's possible that the patterns
'face='sans-serif' or 'size='12' could show up in normal text (though admittedly
that's unlikely). In fact, what you really want to know is wherever those patterns show up in
a font tag, but you don't care about anywhere else that they might appear. Here's one way to
find that pattern:
This does a number of things. The -i tells grep to ignore case (otherwise it's case sensitive,
and won't match 'FONT' if you're looking for 'font' or 'Font'). The -r tells it to recursively
descend through the directories from wherever the command starts -- in this case, all htm and
html files in the current directory. Everything in single quotes is the pattern we're matching.
We tell grep to match on any text that starts with ' (thus staying within the font tag), and then either the face or
size definition that we're interested in. The one glitch here is that line breaks can break
things, though there are various ways around that. Finding them is left as the proverbial
exercise for the reader. :)
and won't match 'FONT' if you're looking for 'font' or 'Font'). The -r tells it to recursively
descend through the directories from wherever the command starts -- in this case, all htm and
html files in the current directory. Everything in single quotes is the pattern we're matching.
We tell grep to match on any text that starts with ' (thus staying within the font tag), and then either the face or
size definition that we're interested in. The one glitch here is that line breaks can break
things, though there are various ways around that. Finding them is left as the proverbial
exercise for the reader. :)
How To Search For Text Command In Mac Terminal
The next question is, what do you want to do with this information you've come up with?
Presumably you want to edit those files in order to fix them, right? With that in mind, maybe
it would be useful to just make a list of matches. Grep normally outputs all the lines that
match the pattern, but if you just want the filenames, use the -l switch. If you want to save
the results into a file, redirect the output of the command accordingly. With those changes,
we now have:
Presumably you want to edit those files in order to fix them, right? With that in mind, maybe
it would be useful to just make a list of matches. Grep normally outputs all the lines that
match the pattern, but if you just want the filenames, use the -l switch. If you want to save
the results into a file, redirect the output of the command accordingly. With those changes,
we now have:
Great. But we can do better still. If you are comforable with the vi editor, you can call vi
with that command directly. The trick is to wrap the command in backticks (`). This is a cool
little Unix trick that runs the contained command & returns the result for whatever you want
to do with it. Thus you can simply put this command:
with that command directly. The trick is to wrap the command in backticks (`). This is a cool
little Unix trick that runs the contained command & returns the result for whatever you want
to do with it. Thus you can simply put this command:
The result of this command, as far as your tcsh shell is concerned, is something along the lines
of
of
etc. The beautiful thing here is that if you quit vi & re-run the command later, it will be
able to effectively 'pick up where you left off', since files you've already edited will
presumably no longer match the grep command.
able to effectively 'pick up where you left off', since files you've already edited will
presumably no longer match the grep command.
And if you want to get really ambitious, you can use these techniques in ways that
allow you to do all your editing directly from the command line, without having to go into an
interactive editor such as vi or emacs or whatever. If you make it this far in your experiments,
then the next step is to learn to filter the results of a match and process the filtered data
in some way, using tools such as sed, awk, and perl. Using these tools, you can find all
instances of the pattern in question, break it down however you like, substitute or shuffle the
parts around however you like, and then build it all back up again. This is fun stuff! By this
point, you're getting pretty heavily into Unix arcana, and the best book that I've seen about
these tricks is O'Reilly's Unix Power Tools, by various authors. If you really want to leverage
the power of the tools that all Unixes come with, including OSX, then this is a great place to
both start & end up. There's plenty of material in there to keep you busy for months & years..
allow you to do all your editing directly from the command line, without having to go into an
interactive editor such as vi or emacs or whatever. If you make it this far in your experiments,
then the next step is to learn to filter the results of a match and process the filtered data
in some way, using tools such as sed, awk, and perl. Using these tools, you can find all
instances of the pattern in question, break it down however you like, substitute or shuffle the
parts around however you like, and then build it all back up again. This is fun stuff! By this
point, you're getting pretty heavily into Unix arcana, and the best book that I've seen about
these tricks is O'Reilly's Unix Power Tools, by various authors. If you really want to leverage
the power of the tools that all Unixes come with, including OSX, then this is a great place to
both start & end up. There's plenty of material in there to keep you busy for months & years..