Post

Mastering Linux File Discovery: The Power of Find and Locate

Mastering Linux File Discovery: The Power of Find and Locate

nix

In the world of Linux administration, being able to find the right file quickly is a superpower. While there are many ways to search, two tools stand above the rest: find and locate.

Here is a breakdown of how to use them, from real-time dynamic searches to lightning-fast database queries.


1. The Power of find

The find command dynamically searches the given directory structure for files meeting specific criteria. It is highly flexible and allows you to run commands against each found file.

Searching by Modification Time

Searching for files based on when they were last changed is essential for troubleshooting.

  • Modified exactly 60 mins ago: find /etc -mmin 60
  • Modified less than 60 mins ago: find /etc -mmin -60
  • Modified more than 60 mins ago: find /etc -mmin +60

Pro-Tip: Use 2>/dev/null to suppress permission errors. find /etc -mmin -60 2>/dev/null

Searching by Type and Depth

Limit your search to specific file types or directory depths to improve performance.

  • Find Symbolic Links in root only: find / -maxdepth 1 -type l
  • Find regular files by name: find /usr/share/doc -type f -name '*.html'

Executing Actions on Results

You can perform tasks like copying or deleting files directly within the search command.

  • Copy found files: find /usr/share/doc -type f -name '*.html' -exec cp {} ~/links/ \;
  • Delete found files: find . -type f -name '*.html' -delete

2. Using locate for Efficiency

Rather than dynamically searching the complete filesystem, locate searches a pre-built database. This makes it significantly faster for broad searches.

Installation & Maintenance

If mlocate isn’t on your system, install it and initialize the database:

1
2
sudo apt install -y mlocate
sudo updatedb

Essential locate Commands

  • locate -S Show database stats
  • locate -i “hosts” Case-insensitive search
  • locate -e “hosts” Verifies the file still exists on disk before showing it
Featurefindlocate
SpeedSlower (real-time disk scan)Faster (database query)
AccuracyAlways up-to-dateNeeds updatedb for new files
CapabilityCan execute actions (-exec)Search only
This post is licensed under CC BY 4.0 by the author.