find is used to output paths beneath a set of directories that match as a set of options, called predicates. For example, the following command:
find dir1 dir2 -name "foo" -type d
This command has two directories to look under: dir1 and dir2, and two predicates. The first indicates that the name of the object must be foo. The second indicates that the object must be a directory. Thus find will only output the paths to directories named foo that are beneath dir1 or dir2. find has many predicates. In this class, we will only highlight the two simplest and most useful options: -name and -type:
command | |
-name "pattern" | here pattern is a wildcard pattern. Make sure to use quotes around your search query. |
-type x | here x is a file type. File types include: regular files (f), directories (d) and symbolic links (l) |
For example, if the output of the command:
find . -name "A*"is
./Amy ./work/Apple ./hw/Answers
this means that there are three objects beneath the current directory whose name starts with A. Note, however, that you do not know whether these objects are regular files or directories. (Note: if you had not quoted the wildcard pattern in the find command above, only the first object would have been found. Can you explain why?)
The general form of a find command is
find list-of-directories [ list-of-predicates ]
On hills, grab the directory findExercise on hills using
cp -r /pub/cs/grwoo/cs160a .
Then write find commands to do the following:
The locate command is useful for finding data that has been on the filesystem for at least a day. It does not allow you to list objects by attributes - it only searches a list of all data objects on the filesystem produced periodically (usually overnight). Thus you can only search for patterns in file paths, not by what type of data it is.
Since locate does not examine current files, rather it examines a list of files produced previously, it is much faster than find. The pattern you use to search for the filenames can be used to restrict the output.
locate [options] pattern
The default (no options) is to interpret the pattern as a wildcard pattern. If the pattern does not contain wildcard characters, leading and trailing asterisks are added - thus locate pattern is really locate '*pattern*'
You can alter the type of pattern used by one of the --regex (regular expression) or --regexp (extended regular expression) options.
find usr -type f
find practice
You can nearly draw the directory tree from this output. If a directory isn't empty, you can tell it is a directory since it has something in it. You cannot, however, distinguish a regular file from an empty directory.
Transfer your output script session as an attachment on Canvas
If you find something particularly interesting, please include that in your homework submission as a comment. I may ask you to share your insights at the beginning of class for participation points.