Using the find CommandWhen 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:
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.
Finding a File by Its DateIn 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:
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 SizeIn 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:
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.
|