Using the find Command


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.


Finding a File by Its Date

In some cases, you may not be able to recall what a file is named or what its extension is, but you may know what day you created it. Creation date is another searchable criterion that you can select. To search, you will need to adapt a "how many days ago" mentality because Unix will search for files that have been made since the time that you specify. Let's look at an example:

1.

Determine how many days ago your file was made.

2.

Choose a starting directory for the search.

3.

Use the find command with the -ctime option: find <starting directory> -ctime <how many days old> -print.

For example:

 >find ~/ -ctime 5 -print 

This command specifies that Unix should find a file in your home directory that is less than five days old and print it to the screen.

Now that you know how to find files using their creation date, let's learn how to find files by their size.

Finding a File by Its Size

In the previous examples, you used the find command to search for a file based on a specified location and to find a file based on its creation date. Both are good examples of searching for a good reasonyou don't know where the file is! But why would you search for a file by its size? It may be easy to remember a name or when you worked on a file, but its size?

Believe it or not, there will be many times when you need to search a system for a file by its size. One great example is if you are a web designer and have large graphics files on your Unix system. Here, it's a great bet that these files will be larger than your word processor documents or spreadsheets.

To search by size, use the following steps:

1.

Select a target file size, and find will locate all files of the selected size and larger.

2.

Choose a starting directory.

3.

Start the find program using the -size option: find <starting directory> -size <k> -print.

For example:

 >find ~/ -size 1024k -print /home/rob/updates/testgraphic.jpg 

The find command you see here has located a graphics file that is larger than 1,024k.

What Else Can You Find? The find command is powerful. As you can see, using the find command with the -name, -ctime, and -size options can be helpful, but believe it or not, there is more.

Remember, with Unix, the power is at the command line. Use the man pages (as discussed in Lesson 2, "Getting Help") to learn other helpful options.




    SAMS Teach Yourself Unix in 10 Minutes
    Sams Teach Yourself Unix in 10 Minutes (2nd Edition)
    ISBN: 0672327643
    EAN: 2147483647
    Year: 2005
    Pages: 170

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