DEV Community

Cover image for Lost and Found: Mastering File Searches with find on Linux
Marzena Pugo
Marzena Pugo

Posted on

Lost and Found: Mastering File Searches with find on Linux

Table of Contents


Why You Need find in Your Linux Toolkit

If you’ve ever lost a file in a maze of directories or needed to hunt down logs, configs, or that one rogue script, find is your best friend.

It’s lightning-fast, works everywhere, and doesn’t care if your desktop is GNOME, KDE, or pure terminal.

Sure, you can use the GUI, but when things get big or messy, the command line wins every time


Basic Syntax: The Anatomy of find

Let’s break it down:

find [path] [options] [expression]

[path]: Where do you want to start searching? (. for current directory, / for the whole system)

[options]: How should find behave? (e.g., follow symlinks, optimize)

[expression]: What are you looking for? (name, type, size, etc.)

Example:

find /home -name "*.jpg"
This will find every .jpg file under /home and all its subfolders.


Everyday Examples You’ll Actually Use

  • Find by name

find . -name "myfile.txt"
(Looks for myfile.txt in the current directory and below

  • Case-insensitive search

find /var/log -iname "*.log"
(Finds all .log files, upper or lowercase.)

  • Find directories only

find /etc -type d
(Returns only directories, not files)

  • Find files larger than 100MB

find / -size +100M
(Great for cleaning up disk space)

  • Find empty files

find . -type f -empty
(Handy for spotting zero-byte files)


Next-Level Searches: Power User Flags

  • Find by owner

find /home -user alice
(All files owned by alice)

  • Find by group

find /srv -group devs
(All files belonging to the devs group)

  • Find by permissions

find / -perm 644
(Files with exact permissions (read/write for owner, read for others))

  • Find by modification time

find /var/log -mtime -7
(Files changed in the last 7 days)

  • Follow symlinks

find -L /some/path -name "*.conf"
(Follows symbolic links as it searches)


Chaining Commands: -exec and Beyond

Here’s where find gets really fun. You can run commands on what you find:

  • Delete old .tmp files

find /tmp -name "*.tmp" -mtime +30 -exec rm {} \;
(Deletes .tmp files older than 30 days)

  • List details of found files

find . -name "*.sh" -exec ls -lh {} \;

  • Find files containing text

find . -type f -exec grep -l "search_term" {} +
(Searches inside files for a specific string)


Quick Tips & Gotchas

  • Use -maxdepth and -mindepth to control how deep find goes

  • Combine conditions with -and, -or, and ! for complex searches

  • Always double-check before running -exec rm or -delete a small typo
    can mean big trouble

  • For massive searches, consider adding 2>/dev/null to ignore
    permission errors


Wrapping Up

The find command is a must-have for any Linux user.

Whether you’re cleaning up, tracking down lost files, or automating system tasks, it’s the Swiss Army knife you didn’t know you needed.

Start with the basics, experiment with flags, and soon you’ll be wielding find like a pro.

Top comments (0)