Locate vs Find

Speed vs Agility in your filesystem

Clint Savage

Founder, Utah Open Source Foundation

Instructor, Guru Labs Training

About Locate

Locate: The Tools

About Find

Find: The Basics

Find: Basic Examples


$ find -name 'Californication*'
./torrents/current/Californication.S01E01.PREAIR.DVDRip.XviD-SiTV.avi
$ find /data -iname 'californication*'
/data/torrents/current/Californication.S01E01.PREAIR.DVDRip.XviD-SiTV.avi
/data/torrents/current/californication.102-caph.avi
/data/torrents/current/californication.103-caph.avi
$find /tmp -name '*dat'
.. snip ..
find: /tmp/lost+found: Permission denied
/tmp/tmprules-444.dat
.. snip ..

Find: AND and OR

  • find can extended with multiple predicates and criteria
    • $ sudo find /data -iname '*iso' -user clints
  • Predicates can also be negated and OR'd
    • $ sudo find /data -type f -not -user clints
    • $ sudo find /data -type f -or -user root
    • $ sudo find /data -type d ! -user clints
  • Grouping is also possible
    • $ sudo find /data \( -user clints -o -user root \) -type d

Find: Permissions, Sizes, Inodes and Times

  • What good sysadmin doesn't want to run this?
    • $ sudo find / -perm /002
    • $ sudo find / -size +1G
    • $ sudo find / -mtime -5
    • $ sudo find / -mmin +60
  • Hard links are sometimes in odd places
    • $ sudo find / -samefile /data/torrents/current/test.avi
  • Filesystem reports sometimes give inode numbers, what file does it belong?
    • $ sudo find / -inum 934950

Find: The Real Power!

  • find has a fifth component, the action
  • find defaults to -print, though there are many more
    • -ls - long listing with inode numbers
    • -delete - delete resulting files
    • -printf - c style printing
    • -quit - quit when found
    • -exec - covered next slide
    • -ok - covered next slide

Find: Acting on Results

  • Find has the power to extend your toolbox beyond any other!
  • -exec and -ok provide the ability to extend find to other commands
    • Because of the nature of these actions, its recommended to use -ok first
  • Some good examples
    • $ sudo find /etc -type d -perm -002 -ok chmod o-w {} \;
    • $ sudo find /tmp -ctime +1 -user clints -ok rm {} \;
    • $ sudo find /etc/httpd -name '*conf" -exec cp {} {}.orig \;
  • And if you want to archive something, try this:
    • $ sudo find /data/torrents/current -iname "*heroes*" -print0 | tar --null -cvjf torrents.tar.bz2 --files-from=-