Section 8.1. Finding Files


8.1. Finding Files

When you are working on your Mac, most of the time it's not a matter of having the correct permissions to access a file that gets in your way; it's being able to find the file in the first place. With Mac OS X Tiger, Apple brings us Spotlight technology, a great new tool for helping you find the right file. Spotlight works by indexing the data contained not only within a file itself, but also the file's metadata. Metadata is simply data about data. By tracking information like a document's author, its modification time, and other forms of special file data, Spotlight allows you to search for files using much more than a filename.

For example, image files can sometimes be hard to find in the filesystem. Digital cameras have notoriously poor file-naming schemes and scanning through a folder full of thumbnails in the Finder is quite cumbersome. However, if you know that the picture you're looking for was taken on your Olympus digital camera sometime in late October last year, Spotlight can help you quickly narrow down the possibilities, as shown in Figure 8-1.

Figure 8-1. Using the Finder and Spotlight to search for image files


A particularly nice aspect of searching in Tiger's Finder is that its searches are live updating. If you were to import more pictures that met those search criteria, the results window would update with the new files automatically. An even more powerful feature is that the search can be saved for later use. With just a click of a button, these searches become Smart Folders . You access a Smart Folder much like any other folder in the Finder. However, when you're in a Smart Folder, you'll see a gray bar telling you that you're viewing a Smart Folder and offering an Edit button to gain access to the search criteria. Figure 8-2 is an example of a Smart Folder's contents, based upon the search criteria specified in Figure 8-1.

In addition to using the Finder's search functions, you can perform a search using Spotlight itself. While the Finder's search results are composed solely of files and folders, a search in Spotlight reveals items in other parts of the Spotlight store. This makes it easy to search across all of your data for your chosen criteria. Figure 8-3 contains a search of my Home directory for the word "Apple." Spotlight's search window offers several ways to filter and sort the search results, as well as extended information for each item.

8.1.1. Finding Files on the Command Line

While the Spotlight-enabled Finder makes it easy to find files using the GUI, there are times when it's easier (or even necessary) to use the command line. Thankfully,

Figure 8-2. The contents of a Smart Folder


Smart Folders Aren't Really Folders at All

A quick glance in the Terminal reveals that a Smart Folder isn't really a folder at all. Instead, Smart Folders are stored as Property List (.plist) files that have a .savedSearch file extension. While it's a shame you can't directly access the contents of a Smart Folder using traditional Unix commands, Apple has provided command-line tools for using Spotlight data. You can learn more about these commands later in this chapter.

Since a Smart Folder is simply a Property List, you can edit one using Property List Editor (part of the Xcode Tools and installed in /Developer/Applications/Utilities) or TextEdit (/Applications). Within the file, you'll find the RawQuery field, which contains your Spotlight query, and the file path the query scans. Though modifying a Smart Folder by hand doesn't provide the polish of the Finder's GUI, it's a great way to see how Mac OS X handles Smart Folders behind the scenes.


Mac OS X's BSD subsystem includes traditional Unix search tools like find,grep, and locate. Your options for command-line searching are even further broadened by Tiger's Spotlight commands. With so many powerful utilities at your disposal, it can be tough to pick the right command for the job. Here's a breakdown of each to aid your decision.

Figure 8-3. A Spotlight search


8.1.1.1. find

The aptly named find is one of the original Unix file-searching tools and allows you to search for files based on their filenames and other attributes such as when they were last modified. The basic syntax for using find is:

     find pathname conditions 

where the pathname argument indicates where in the filesystem hierarchy the search should occur, and the conditions argument indicates the attributes a file must have to match the search. There are more than 40 types of conditions you can use, the most common of which are listed in Table 8-1. However, the option you'll probably find yourself using the most is -name.

Table 8-1. Commonly used find conditions

Condition

Description

-group groupname

Find files belonging to the specified group.

-mtime +n | -n | n

Find files that were modified more than n (+n), less than n (-n), or exactly n (n) days ago.

-name pattern

Find files with names matching the given pattern.

-newer file

Find files that have been modified more recently than the given file.

-user username

Find files that belong to the specified user.


Example 8-1 shows how to find all the files that have a .xls extension in the directory ~/Documents.

Example 8-1. Using the find command with the -name argument
 $ find ~/Documents -name *.xls /Users/jldera/Documents/Infrastructure Layouts.xls /Users/jldera/Documents/Jason Deraleau.xls

When run, the find command scans through every file in the ~/Documents directory and its subdirectories. Once it's finished, it lists each file with a name matching the pattern *.xls.

Another useful option is -mtime, which lets you search for files that were modified in a particular time frame. Example 8-2 shows how to find all the files that were modified in the last day in the ~/Documents directory.

Example 8-2. Finding documents based on modification time
 $ find ~/Documents -mtime  -1 /Users/jldera/Documents/iChats/Fraser Speirs on 2005-04-18 at 09.34.ichat /Users/jldera/Documents/Infrastructure Layouts.xls /Users/jldera/Documents/Purchase Requisition.pdf

8.1.1.2. locate

One of the fastest ways to find filesand quite a bit easier to use than findis to use the locate command. Unfortunately, on most Macs, this tool won't return any results because it depends on a database (located in the file /var/db/locate.database) that only gets created if your machine is running at 4:00 a.m. on a Sunday morning. Chapter 13 shows you how to reschedule this task for a better time, but in the meantime, it's possible to create the database manually with the following command:

     $ sudo /usr/libexec/locate.updatedb 

When you issue this command, you are prompted to enter an administrator's password. Because you're using sudo, you must be an administrative user to execute the command. Since it takes a while to create the locate database, you'll want to run this command in its own Terminal window if you plan to continue working in the shell while it is being executed, or add an ampersand (&) at the end of the command to run it as a background process.

Once the database has been created, you can use the locate command with the following syntax:

     locate pattern

Example 8-3 shows how to find a file that you can't quite remember the location or name of.

Example 8-3. Using the locate command
 $ locate 2004Tax /Users/jldera/Documents/Financial/Taxes/2004/2004TaxReturn.pdf

8.1.1.3. grep

While find and locate search for files based on their filename, grep looks within a file, allowing you to search for something within a file's contents instead of just its filename. You can either search for a string within a single file or a whole group of files. Its basic syntax is:

     grep pattern file

where pattern is what grep will look for and file is the file, or list of files, in which to look. By itself, this command only searches the specified files. Quite often, however, you'll want to search an entire directory. To do so, you can use the -r option, which puts grep into recursive mode. Example 8-4 shows how to find a document in the ~/Documents directory that has the word "Appcasting" in it.

Example 8-4. Finding documents based on content with grep
 $ grep -r Appcasting ~/Documents /Users/jldera/Documents/Appcasting Article.txt: Fraser Speirs defines Appcasting as "the practice of using

In addition to telling you the name of the file in which that the term is found, the command shown in Example 8-4 gives you the line of text in which the term is found, which is a way to make sure that grep has found the document you really want.

8.1.1.4. mdfind

Unfortunately, not all file formats store data in the same way. While grep is an excellent choice for searching text files, it doesn't do as great a job when working with binary files. By using Spotlight commands, you can open up many other types of content for searching. The Spotlight command you'll work with most is mdfind. This tool provides command-line access to the Spotlight database. You can search Spotlight with the syntax:

 mdfind options query

There are only three options for use with mdfind:


-onlyin

Limits the search to a given path, much like the find command.


-live

Since one of the beauties of Spotlight is its live updating, you can supply mdfind with this option for those same live results, ad infinitum (or you can hit Control-C).


-0

Directs mdfind to print an ASCII NUL character after each result for use with tools like xargs; something discussed more in Chapter 14.

When structuring your query, you can either use simple terms, like "Apple" (as shown in Example 8-5), or more advanced search criteria based on Spotlight attributes.

Example 8-5. Searching Spotlight with mdfind
 $ mdfind -onlyin /Users/jldera "Apple" /Users/jldera/Library/Preferences/com.apple.print.PrintingPrefs.plist /Users/jldera/Library/Preferences/com.apple.SetupAssistant.plist /Users/jldera/Library/Preferences/com.apple.MenuBarClock.plist /Users/jldera/Library/Preferences/com.apple.HIToolbox.plist /Users/jldera/Library/Preferences/com.apple.JapaneseAnalysis /Users/jldera/Library/Mail/Mac-jldera/Archive.imapmbox/Messages/98.emlx /Users/jldera/Library/Mail/Mac-jldera/Archive.imapmbox/Messages/101.emlx /Users/jldera/Library/Preferences/com.apple.dashboard.client.plist

Unfortunately, there is no master list of the types of metadata available in the Spotlight store. Spotlight's expandability makes creating such a list quite difficult, as each new application built for Spotlight brings its own custom metadata. However, using the mdls command discussed later in this chapter, you can view the metadata associated with a file, including the same attribute names used when creating Spotlight queries.

Example 8-6 shows the results of an mdfind query similar to that created using the Finder in Figure 8-1.

Example 8-6. Using Spotlight attributes with mdfind
  $ mdfind -onlyin /Users/jldera/Pictures "kMDItemAcquisitionMake == 'Olympus'" /Users/jldera/Pictures/iPhoto Library/2004/10/23/PA220010.JPG /Users/jldera/Pictures/iPhoto Library/2004/10/23/PA220013.JPG /Users/jldera/Pictures/iPhoto Library/2004/10/23/PA220029.JPG /Users/jldera/Pictures/iPhoto Library/2004/10/23/PA220033.JPG /Users/jldera/Pictures/iPhoto Library/2004/10/23/PA220034.JPG /Users/jldera/Pictures/iPhoto Library/2004/10/23/PA220039.JPG




Running Mac OS X Tiger
Running Mac OS X Tiger: A No-Compromise Power Users Guide to the Mac (Animal Guide)
ISBN: 0596009135
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net