Back to course

31. The `locate` Command vs. `find`

Linux Basics: From Zero to CLI Hero

Database-Driven Search

While find searches the live filesystem, the locate command searches a pre-built database of files (/var/lib/mlocate/mlocate.db).

locate Advantages

  • Speed: It is incredibly fast because it only searches an index, not the actual disk.

locate Disadvantages

  • Accuracy: It can only find files that existed when the database was last updated. Newly created files won't show up immediately.

Basic Usage

bash $ locate sshd_config /etc/ssh/sshd_config /usr/share/man/man5/sshd_config.5.gz

Updating the Database

To ensure the database is current, you must manually run the update command (requires administrative privileges):

bash $ sudo updatedb

Rule of Thumb: Use locate for quick general searches; use find for precise, criteria-based, and execution-oriented searches.