Using the find Command

When you need to find a file on your Unix system, you will need to know exactly where it is located (and navigate to it directly), or you may have to run a search on it because you aren't entirely sure what you are looking for. In either case, to locate a file on your system, you will need to know something about the file.

To find a file by its name, you will need to know a portion of the name. The more of the name you give, the quicker the search will be because that will narrow down the results. You can use wildcards to specify unknown characters. This same functionality can be seen in just about any other operating system, especially in Microsoft Windows. You can specify more than one file with a path by using wildcards. A wildcard is a character that matches many characters. The * wildcard matches any number of characters. A Wildcard will be explained more thoroughly and used in other (and more detailed) examples throughout the book.

If you do not know the name or part of the name, you will need to know something else about the file. Things that can be used as searchable criteria include creation date and the size of the file. The find command can then be used to build up matches to the query you construct and execute to find the data you need. In the next exercise we will learn how to use the find command.

To search for a file by name:

1.
Before you search for a file, you have to have an idea of what you want to search for; therefore, specify the filename or some of its searchable criteria. Remember, you can include wildcards in the search for the filename.

2.
Next, select the directory you want to search. If you want to search the entire file system, the starting directory will be /.

3.
This command would appear as find <starting directory> -name <filename> -print, or if you wanted to find a specific file in the root directory, it would appear as follows:

>find / -name *.rpm –print
/var/lib/YasST2/you/mnt/i386/update/9.2/deltas/kernel-source-2.6.8-24_24.13.i586.delta.rpm

Find Files…Now What? If you ask Unix to search for a file and do not tell it to print the results, Unix may find your file and tell you nothing about it. In most distributions of Unix, you will have to specify –print.


In this example, we searched the root directory for RPM files. RPM files can be used for installations and updates on your Unix or Linux system. This search method can be handy if you are unsure where to look for something you may need, or you want to save time by running a search to find files instead of hunting and pecking for them yourself.

Handling Error Messages As you continue your search through the Unix system for files, you may encounter small problems, such as not finding your file at all, or you may get a message stating a problem or issue you need to be aware of. In this example, we see that there is an issue with finding the CD-ROM and floppy medium.

find: /media/cdrom: No medium found
find: /media/floppy: No medium found

This happens to be a common error; it's simply telling you that there was nothing found within those mounted drives.

Other errors you may encounter can range from problems accessing directories that you do not have permission to search, to completely obscure things that only senior Unix administrators may see. Always ask your system administrator for help if needed.


Learn How to Break In Be careful when searching; you don't want to query the entire file system for something too generic, because you could be waiting a long time for those results to print on your screen. If you are stuck, you can try to break the sequence with a keystroke such as [Ctrl+C]. If this does not help, see your system administrator.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.149.45