Find a file by name with find . -name "file.txt", use -iname when capitalization should not matter, and append 2>/dev/null to hide permission warnings after choosing a safe starting path. For content searches use grep -r, for large files add -size, and use fd when it is installed for a faster, friendlier alternative.
How to Find a File in Linux: The Ultimate Terminal Cheat Sheet
The complete Linux file search toolkit: find, fd, grep, ripgrep, and fzf with real examples for name, content, size, time, permissions, and safe deletion.
// The short answer
In this guide · 6 sections
We’ve all been there: you kick off a search from root, and suddenly your screen is drowned in a wall of "Permission denied" errors while your fans spin up. It is frustrating, slow, and totally unnecessary. When you are tracking down an elusive config file or digging for a crash log, you want quick, surgical precision instead of brute-forcing your entire drive.
- Search by exact filename:
find . -name file.txt - Hunt down text inside files:
grep -r error_code . - Spot giant disk hogs immediately:
find . -type f -size +1G - Modern, ridiculously fast alternative:
fd config
Search by Name and Extension (find vs fd)
The Classic Method: find Command Basics
The good thing about find is that it's already installed on pretty much every box you'll ever log into. You just hand it a starting folder and a pattern, and it gets to work. If you want to ditch those noisy permission warnings cluttering your screen, tack 2>/dev/null onto the end. It silences the errors and keeps your terminal nice and clean.
Bash
find . -name "app.log"
Keep in mind that find is case-sensitive right out of the box. If you aren't sure whether something was saved with capital letters, use -iname instead:
Bash
find . -iname "notes.txt"If you are looking for a specific file extension across your home folder and everything underneath it:
Bash
find /home -name "*.py"When you specifically need folders rather than loose files, throw in -type d. It comes in handy when you remember creating a backup directory somewhere in your workspace, but don't recall where it ended up.
Bash
find . -type d -name "backup"Once you track down what you need, you might also want to grab a directory's full path so you can drop it straight into a script or alias.
The Modern Alternative: Using fd for Faster Searches
If you have the option to install third-party tools, fd (written in Rust) runs circles around find for day-to-day work. The killer feature here is that it skips hidden items and respects your .gitignore rules by default, meaning you don't have to fiddle with extra flags just to keep it out of node_modules.
Bash
# Install on Debian/Ubuntu
sudo apt install fd-find
# On Arch
sudo pacman -S fd
# Basic usage (replaces find . -name "*config*")
fd configIts default setup covers what developers actually need nine times out of ten: recursive searches starting right where you are, case-insensitivity by default, and skipping .git guts automatically. Keep find in your back pocket for tricky administrative tasks involving raw timestamps or strict POSIX permissions, but lean on fd for your daily workflow.
Search Inside File Contents (grep and ripgrep)
Scanning Text with grep -r
Every so often, you completely blank on what a file is called, but you remember a distinctive variable name, an endpoint string, or a specific function. That’s where grep -r saves the day by digging straight through the contents of every file in the tree.
Bash
grep -r "connectDatabase" /var/www/htmlA few flags worth memorizing:
- -n: prints the exact line number alongside the match
- -l: outputs only the names of matching files, sparing you the code dump
- -w: matches whole words only, so you avoid annoying partial hits
Why Developers Switch to ripgrep (rg)
Running grep -r across a massive repository can crawl to a halt, especially when you have thousands of source files and dependencies. ripgrep (rg) solves this effortlessly: it honors .gitignore, bypasses binary blobs without being asked, and rips through huge directories in a fraction of a second.
Bash
# Install
sudo apt install ripgrep
# Search recursively (same idea as grep -r, just blazing fast)
rg "connectDatabase"Once you track down the log or output file you're hunting for, the Linux tail command is usually the natural next step to monitor incoming events live.
Interactive Live Search: Finding Files with fzf
Dumping raw text to the terminal can feel clunky when you aren't 100% sure what you're tracking down. That’s where fzf comes in - it turns your terminal into a responsive, fuzzy-finder window that narrows down options as you type.
Bash
sudo apt install fzfOnce it's set up, hitting Ctrl+T pops up an interactive list that filters down with every keystroke. You can also pipe standard find results directly into it for an instant, interactive picker:
Bash
find . -type f | fzfThis is a lifesaver when you're dropped into someone else's messy project structure and you only have a hazy idea of how a filename was formatted.
Filter by Size, Time, and Permissions
Locating Large Log Files (-size)
Nothing ruins a morning quite like an out-of-disk-space alert caused by a runaway application log. You can zero in on any file tipping over 500MB with a single command:
Bash
find /var/log -type f -size +500MIf you want to isolate files within a specific window - say, between 50MB and 100MB - you can just string two size checks together:
Bash
find . -type f -size +50M -size -100MFiltering by Modified Time (-mtime)
When you're troubleshooting an unexpected outage, figuring out what changed right before things went south is half the battle.
Bash
# Files modified within the last 24 hours
find . -mtime -1
# Files modified exactly 7 days ago
find . -mtime 7
# Files touched more than a month ago
find . -mtime +30Finding World-Writable Files (-perm)
Files with loose permissions are essentially an open door on a shared server. You can audit your filesystem for completely unrestricted files using this:
Bash
find . -perm 777And if you need to scope things down to files owned by a specific user account:
Bash
find /home -user john -type fAdvanced: Execute Commands on Found Files
Performance: xargs vs -exec
Once you've isolated the files you care about, passing them over to another utility like chmod or rm usually comes down to picking between two patterns.
Bash
# -exec spins up an entirely new process for every single file (slow on big batches)
find . -name "*.log" -exec rm {} \;
# xargs groups the arguments together so a single process handles the batch
find . -name "*.log" | xargs rmWhen you are dealing with thousands of items, xargs runs noticeably quicker. If you want that same batching behavior without piping outside find, the -exec {} + syntax is the clean built-in compromise:
Bash
find . -name "*.log" -exec rm {} +Dry-Run Before Deleting
Accidentally wiping the wrong directory is a mistake you usually only make once. Get in the habit of doing a dry run with echo so you can verify what’s on the chopping block first:
Bash
# Safe preview of what would happen
find . -name "*.tmp" -exec echo rm {} \;
# Run the real thing only after confirming the preview looks right
find . -name "*.tmp" -exec rm {} \;Whenever you determine it’s finally safe to remove the directory for good, run rm -rf directly against that specific target path.
Find System Files Instantly with locate
Traversing the entire disk from scratch is complete overkill if you're just looking for standard utilities or static config templates. The locate tool avoids the disk walk by querying a pre-indexed local database, getting you an answer in milliseconds.
Bash
sudo apt install mlocate
sudo updatedb
locate my_secret_file.txtThe tradeoff is that locate only knows what was present the last time updatedb ran, so brand-new files won't show up yet. For anything created recently or when live accuracy is critical, stick with find.
If you're running Linux through WSL on a Windows workstation, these commands work identically, though setting up WSL2 on Windows properly upfront will save you plenty of filesystem performance headaches between Windows and Linux mounts.
When to Use Which Command
- locate: instant lookup for persistent system files you know haven't moved
- find: the heavy lifter for precision filtering, fresh files, timestamps, and permissions
- grep / rg: when the filename escapes you, but you know a snippet of the code inside
- fd: your day-to-day terminal companion, built with sensible defaults and great speed
- fzf: interactive, visual exploration when you want to filter live results on the fly
Quick answers
Frequently asked questions
How do I find a file in Linux by name?
Use the find command with the -name flag: find . -name "filename.txt". For case-insensitive matching use -iname. If you know only part of the name, wrap it in wildcards: find . -name "*config*". The dot means start from the current directory; replace it with a path like /home to search a specific location.
How do I search for text inside files in Linux?
Use grep -r "search_term" /path to recursively search file contents. Add -n to show line numbers and -l to list only filenames without the matched text. For large codebases, ripgrep (rg) is a faster alternative that respects .gitignore rules and skips binary files automatically.
What is the fd command in Linux?
fd is a modern, Rust-based alternative to find that is significantly faster on most workloads. It ignores hidden files and .gitignore patterns by default, which makes it ideal for daily development use. Basic usage is simply fd pattern with no required flags. Install it with sudo apt install fd-find on Debian/Ubuntu.
How do I find and delete files in Linux safely?
Always do a dry run first. Add echo before the deletion command to preview what will be removed: find . -name '*.tmp' -exec echo rm {} \;. Only replace echo rm with rm after confirming the file list is correct. For thousands of files, pipe to xargs instead of using -exec, as xargs batches the operations into fewer process calls.
What is the difference between find and locate in Linux?
find searches the actual filesystem in real-time and is always accurate, while locate queries a pre-built index database and returns results instantly. The trade-off: locate will not find files created after the last updatedb run, while find will always reflect the current state of your disk. Use locate for stable system files and find for recent work or when you need precise filters.
How do I find files modified in the last 24 hours in Linux?
Run find . -mtime -1 to list all files modified within the last 24 hours. Change the number to adjust the window: -mtime -7 covers the last week, and -mtime +30 finds files older than 30 days. This is useful for debugging incidents, finding recently edited configs, or identifying which logs were written during a specific event.
How do I use find with exec or xargs in Linux?
Both methods pass find results to another command. The -exec flag runs the command once per file: find . -name '*.log' -exec rm {} \;. Piping to xargs is faster with large file sets because it batches the files into a single command call: find . -name '*.log' | xargs rm. Use -exec {} + as a middle ground that also batches but stays within the find command itself.
Something wrong or out of date?
Tools change fast. Tell the editor what no longer works.