Main Course Webpage

Course Slack Page

In-Class Lab 4: Find and Locate

Objective: Practice simple forms of the find and locate commands

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 ]

Exercise

Part One: Using find

On hills, grab the directory findExercise on hills using

cp -r /pub/cs/grwoo/cs160a .
      

Then write find commands to do the following:

Procedure

  1. Output the paths to regular files beneath usr
  2. Output the paths to directories beneath practice
  3. Output the paths to regular files beneath practice whose names are tree
  4. Output the paths to directories beneath usr whose names are five characters long
  5. Output the paths to objects beneath usr whose names have a . in them
  6. Output the paths to everything beneath the current directory. Put the output in a file named find.out in your home directory. Then examine the contents of the find.out using more.
  7. Last, output the paths to all objects beneath practice. Can you use this output to create a directory tree of practice on the classroom Linux machines? How can you know what is a file and what is a directory?

Exercise

Part Two: Using locate

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.

Procedure

  1. Output all file paths that contain the string bad.txt
  2. Output all file paths where the filename part (the last path segment) is bad.txt
  3. Create a new file named bad.txt in your current directory and re-issue the command for 2. Does your new file show up?
  4. Limit the search for files ending with ".conf" to paths beneath the system directory /etc

Answers

Part One

  1. find usr -type f

  2. find practice -type d
  3. find practice -type f -name "tree"
  4. find usr -type d -name "?????"
  5. find usr -name "*.*"
  6. find . > ~/find.out; more ~/find.out
  7. 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.

Part Two

  1. locate bad.txt
  2. locate "*/bad.txt"
  3. no notes
  4. locate "/etc/*/*.conf"

Submitting Your Homework Assignment

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.