Finding files is one area where Macs excel. Since System 7, you simply hit “Command” and “F” and entered part of the file or folder name and hit return. Your Mac opened each found file or folder one at a time. This grew into a sophisticated “Find” application and was later renamed “Sherlock.” It finally made sense that the “Finder” actually found files.
Since imitation is the sincerest form of flattery, it made sense that Microsoft added, “Search” to the Start menu when they introduced Windows 95. Once again users could search for files and folders on their PCs.
So when Apple shows off the new Finder at trade shows, they are greeted with the ubiquitous “Oohs” and “Aahs!” The best-kept secret is that the “find” program has been on Unix systems for many years now. Here’s how it works: Open the Terminal application (Applications => Utilities => Terminal.)
Once again you start in your “home” directory. At the prompt you enter the name of the application “find” and follow it with “options”.
[timsg3:~] timmitra% find . -name "*txt" –print
After the prompt I have typed “find” followed by a “.” to indicate that what I want to find is in this folder. Next I follow with the option “-name” and in quotes the part of the name, or “string” that I’m searching for. The last option “-print” will print the results in the terminal window. You may also note that I used an asterisk to represent “any number of letters” – as a wild card. What will result is that I will find all TEXT (.txt) files in my home directory. Additionally, the Finder’s Find command will look inside every folder in my home directory.
If I change the “.” to a “/” which represents the top most directory or “root” directory, then my find command will search the whole computer. The command would look like this:
[timsg3:~] timmitra% find / -name "*txt" –print
I will then be presented with every text file on my computer. By the way, any folder that I don’t have permission to look inside will report an error (“Permission Denied”.) If you must see inside all folders (and you are an Administrator) you can precede the “find” command with “sudo”. The “sudo” command allows you to act as if you are the “super user” in order to run a command that you can’t run as a normal user. More on “sudo” in future articles (In the mean time – be careful with that one!)
The other nifty feature of the new Finder is the ability to search the contents of files. Once again that has been available all along in the form of the “grep” of “fgrep” programs. Grep comes from “get regular expression”. To use “fgrep” you follow it with the “string” you’re looking for in your files.
In my home directory I have a file that has “Hello World” inside. You can quickly make one – Type:
Then hit “Control D”. This creates a file called “fgrep_test.txt”. Now to search for that you can type:
fgrep “Hello World” *
And hit Return. Your Mac will then print every file that contains “Hello World” in your current directory. If I want to check on a specific file(s) I would name them instead of using the asterisk. I could have entered:
fgrep “Hello World” fgrep_test.txt
This would have worked and only would have looked at one file. (If I had specified “*.txt” then I would look through every text file again.)
You can also combine “fgrep” with other programs to search for instances within large files with a “pipe” “|”. You may find that you have a large file and you want to look through it. You can look through your system log file (/var/log/system.log) with “cat” and clean up by following it with the fgrep program.
cat/var/log/system.log | fgrep “ConsoleMessage”
The result of this example will be the messages that your Mac shows as it boots up.
Using fgrep with a pipe will become one of the most useful programs you will use on the command line.