Searching for Files


The command locate searches for a pattern in a database of filenames. For example,

 $ locate pippin

searches the database for filenames containing the string “pippin”. The database contains the full pathname for each file, so this would find files in the directory pippin-photos as well as files such as 0915-pippin.jpg.

The locate command is very fast and easy to use. However, it will only work if the database of filenames is kept up to date. On many systems, the database is automatically updated once per day

The find command is a more powerful search tool, although it can be difficult to use. With find, you can search through any part of the file system, looking for all files with a particular name or with certain features. This section describes how to use find to do simple searches.

Using find

The find command searches through the contents of one or more directories, including all of their subdirectories. You have to tell find in which directory to start its search. The following example searches user jmf’s directory system for the file new_data and prints the full pathname of any file with that name that it finds:

 $ pwd /home/jmf $ find . -name new_data -print /home/jmf/Dir/Logs/new_data /home/j mf/Cmds/new_data

Here, find shows two files named new_data, one in the directory Dir/Logs and one in the directory Cmds. This example illustrates the basic form of the find command. The first argument is the name of the directory in which the search starts. In this case it is the current directory (represented by the dot). The -name option is followed by the name of the file or files to search for. The final option, -print, tells find to print the full pathnames of any matching files.

Note that you have to include the -print option. If you don’t, find will carry out its search but will not notify you of any files it finds.

To search the entire file system, start in the system’s root directory, represented by the /:

 $ find / -name new_data -print

This will find a file named new_data anywhere in the file system. Note that it can take a long time to complete a search of the entire file system; also keep in mind that find will skip any files or directories that it does not have permission to read.

You can tell find to look in several directories by giving each directory as an argument. The following command first searches the current directory and its subdirectories and then looks in /tmp/project and its subdirectories:

 $ find . /tmp/project -name new_data -print

You can use wildcard symbols with find to search for files even if you don’t know their exact names. For example, if you are not sure whether the file you are looking for was called new_data, new.data, or mydata, but you know that it ended in data, you can use the pattern *data as the name to search for:

 $ find -name "*data" -print

Note that when you use a wildcard with the -name argument, you have to quote it. If you don’t, the filename matching process would replace *data with the names of all of the files in the current directory that end in “data.” The way filename matching works, and the reason you have to quote an asterisk when it is used in this way, are explained in the discussion of wildcards in Chapter 4.

Running find in the Background

If necessary you can search through the entire system by telling find to start in the root directory, /. Remember, though, that it can take find a long time to search through a large directory and its subdirectories, and searching the whole file system, starting at /, can take a very long time on large systems. If you do need to run a command like this, you can use the multitasking feature of UNIX to run it as a background job, which allows you to continue doing other work while find carries out its search.

To run a command in the background, you end it with an ampersand (&). The following command line runs find in the background to search the whole file system and send its output to found:

 $ find / -name new_data -print > found &

The advantage of running a command in the background is that you can go on to run other commands without waiting for the background job to finish.

Note that in the example just given, the output of find was directed to a file rather than displayed on the screen. If you don’t do this, output may appear on your screen while you are doing something else; for example, while you are editing a document. This is rarely what you want. Unfortunately, find will still display error messages (such as the names of directories it cannot search) on your screen. Chapter 4 gives more information about running commands in the background, including how to prevent these error messages from appearing.

Other Search Criteria

The examples so far have shown how to use find to search for a file having a given name. You can use many other criteria to search for files. The -mtime option lets you specify the number of days it has been since the file was modified. For example, to search for a file that was modified fewer than three days ago, use -mtime -3. The -user option restricts the search to files belonging to a particular user.

You can combine these and other find options. For example, the following command line tells find to look for a file called music belonging to user sue that was modified more than a week ago:

 $ find . -name "music" -u sue -mtime +7 -print

The find command can do more than print the name of a file that it finds. For example, you can tell it to execute a command on every file that matches the search pattern. For this and other advanced uses, consult the UNIX man (manual) page for find (see Chapter 2 for an explanation of the UNIX man pages).




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

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