Locate vs Find
Speed vs Agility in your filesystem
Clint Savage
Founder, Utah Open Source Foundation
Instructor, Guru Labs Training
About Locate
- Was: locate
- one search could return other users' files
- Became: slocate
- Now: mlocate
- mlocate merges the database changes for speed
Locate: The Tools
- Queries an updateable db of the current state of the filesystem
- Update can only be performed by a privileged user (aka root/sudo)
- [sudo] updatedb
- Updates the database with the current list of files
- Generally run as a cron job in the daily system crontab
- locate bash
- Will list all files with the word bash somewhere in the path
- Search will only look where user has rights
- Other useful switches
- locate -r '^mon'
- locate -n 5 happy
- locate -i happy
About Find
- GNU Foundation findutils package
- Slower but more accurate than locate
- Doesn't search the entire tree unless specified
- Use CWD if directory is not provided
- Extensible
- Can perform action upon results
- Search will only look where users has rights
Find: The Basics
- A standard find query consists of 4 parts
- The find command
- The path (Where to look)
- The predicate (How to find)
- The criteria (What to find)
- find displays a short listing by default
- File globbing is supported as well as regular expressions
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?
- 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
- -exec - 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=-