Find and Locate

Introduction

In Unix-like and some other operating systems, find is a command-line utility that searches through one or more directory trees of a file system, locates files based on some user-specified criteria and applies a user-specified action on each matched file. The possible search criteria include a pattern to match against the file name or a time range to match against the modification time or access time of the file. By default, find returns a list of all files below the current working directory.

The related locate programs use a database of indexed files obtained through find (updated at regular intervals, typically by cron job) to provide a faster method of searching the entire filesystem for files by name. This sacrifices overall efficiency (because filesystems are regularly interrogated even when no users needs information) and absolute accuracy (since the database is not updated in real time) for significant speed improvements (particularly on very large filesystems). On fast systems with small drives, locate is not necessary or desirable.

locate is a Unix utility first created in 1983 used to find files on filesystems. It searches through a prebuilt database of files generated by updatedb or a daemon and compressed using incremental encoding. It is significantly faster than find, but requires the database to be updated regularly.

Notes

  1. Do not run find on a top-level directory such as the root filesystem, /. When searching large filesystems, find puts a very large load on the system and networks. Use locate instead:

    % locate foo.c
    

Examples

  • Search your home directory and print all instances of files named foo.c:

    % find ~ -name foo.c -print
    

  • Recursively delete all *.eml files in your present working directory:

    % find . -name \*.eml -exec rm {} \;
    

  • Delete all files beginning with the text string "mgetty":

    % find . -name mgetty\* -exec rm {} \;
    

  • Change ownership of all files on the entire system owned by userid 500:

    find / -user 500 -exec chown - {} \;
    

  • Set sticky bit group permission on all directories named "cse116":

    find cse116 -type d -exec chmod g+s {} \;
    

References

  1. http://en.wikipedia.org/wiki/Find
  2. http://en.wikipedia.org/wiki/Locate_%28Unix%29