Hi there! I'm Shrijith Venkatrama, founder of Hexmos. Right now, I’m building LiveAPI, a tool that makes generating API docs from your code ridiculously easy.
Searching files and their contents in Linux is a daily task for developers. Whether you’re tracking down a config file or hunting for a specific code snippet, Linux’s command-line tools make it fast and efficient. This post dives into two main categories: tools for finding files and tools for searching file contents. We’ll explore each with practical examples, tables, and tips to streamline your workflow.
Searching for Files: Finding the Needle in the Directory Haystack
These tools help you locate files by name, type, or other attributes. They’re perfect for navigating complex directory structures or finding misplaced files.
1. find: The Ultimate File Search Powerhouse
The find command is the most versatile tool for locating files. It lets you filter by name, type, size, or modification time. Use find for precise, customizable searches.
Example: Find All JavaScript Files
Search for all .js files in /home/user/projects.
find /home/user/projects -type f -name "*.js"
# Output:
# /home/user/projects/app.js
# /home/user/projects/utils/helper.js
Key Options
| Option | Description | Example |
|---|---|---|
-type f |
Files only | find . -type f |
-name |
Case-sensitive name match | find . -name "config.js" |
-iname |
Case-insensitive name match | find . -iname "config.js" |
-mtime -n |
Modified within n days | find . -mtime -7 |
-size +nM |
Larger than n MB | find . -size +10M |
Pro Tip
Use -exec to act on results. Delete all .tmp files:
find . -type f -name "*.tmp" -exec rm -v {} \;
# Output:
# removed './file1.tmp'
# removed './file2.tmp'
2. locate: Speedy File Name Lookups
The locate command uses a prebuilt database for lightning-fast name searches. It’s less flexible than find but ideal for quick lookups.
Example: Locate All Markdown Files
Find all files named readme.md:
locate readme.md
# Output:
# /home/user/projects/repo1/readme.md
# /home/user/docs/readme.md
Updating the Database
Refresh the database (requires sudo):
sudo updatedb
# Output: None (updates /var/lib/mlocate/mlocate.db)
When to Use
Choose [SENSITIVE CONTENT] for speed when searching by name. Use find for advanced filters or if the database is outdated.
3. fzf: Fuzzy Finding for Interactive Searches
fzf is a fuzzy finder that makes interactive file searches a breeze. It’s great for exploring directories or piping with other commands.
Example: Interactive File Search
Search files in the current directory:
fzf
# Output: Interactive prompt; type to filter files, e.g.:
# config.json
# app.js
Installation
Install fzf on Ubuntu:
sudo apt-get install fzf
# Output: Package installation messages
Pro Tip
Pipe find into fzf for filtered searches:
find . -type f | fzf
4. fd: A Modern, User-Friendly find Alternative
The fd command is a faster, simpler alternative to find. It has intuitive syntax and colorful output, making it perfect for quick searches.
Example: Find All JSON Files
Search for .json files:
fd --type f ".json$"
# Output:
# config.json
# data/settings.json
Installation
Install fd on Ubuntu:
sudo apt-get install fd-find
# Output: Package installation messages
Searching File Contents: Hunting for Text in Files
These tools let you search inside files for specific patterns, making them essential for debugging code, analyzing logs, or finding TODOs.
5. grep: The Classic Content Search Tool
The grep command is the go-to for searching file contents. It’s reliable and widely available, perfect for finding text in logs or code.
Example: Find Error Logs
Search for "ERROR" in log files:
grep "ERROR" /var/log/app/*.log
# Output:
# /var/log/app/app1.log:2025-04-30 ERROR: Connection failed
# /var/log/app/app2.log:2025-04-30 ERROR: Timeout
Recursive Search
Search all files in a directory:
grep -r "TODO" /home/user/projects
# Output:
# /home/user/projects/main.py: # TODO: Fix bug
# /home/user/projects/utils.py: # TODO: Add tests
Key Options
| Option | Description |
|---|---|
-i |
Case-insensitive |
-r |
Recursive search |
-l |
List filenames only |
-n |
Show line numbers |
6. ripgrep: Blazing-Fast Content Searches
ripgrep (rg) is a Rust-based tool that’s faster than grep and developer-friendly. It skips binary files and respects .gitignore by default.
Example: Search for a Class
Find all files defining class User:
rg "class User" /home/user/projects
# Output:
# models.py:5:class User:
# auth.py:12:class User(BaseModel):
Installation
Install ripgrep on Ubuntu:
sudo apt-get install ripgrep
# Output: Package installation messages
7. ag (The Silver Searcher): Code-Friendly Searches
ag is another fast alternative to grep, optimized for codebases. It ignores .gitignore patterns and binary files, making it ideal for developers.
Example: Search for a Function
Find calculate_total in a project:
ag calculate_total /home/user/projects
# Output:
# main.py:10:def calculate_total(items):
# utils.py:25: total = calculate_total(cart)
Installation
Install ag on Ubuntu:
sudo apt-get install silversearcher-ag
# Output: Package installation messages
8. ack: A Perl-Powered Search Tool
ack is a developer-focused tool designed for searching code. It’s simpler than grep and highlights matches, but it’s slower than ripgrep or ag.
Example: Search for a Variable
Find all occurrences of user_id:
ack user_id /home/user/projects
# Output:
# main.py
# 15: user_id = request.get('id')
# utils.py
# 8: def get_user(user_id):
Installation
Install ack on Ubuntu:
sudo apt-get install ack
# Output: Package installation messages
Choosing the Right Tool for Your Search
With so many tools, picking the right one depends on your use case. Here’s a guide to help you decide:
For File Searches
| Tool | Best For | Pros | Cons |
|---|---|---|---|
find |
Complex searches with filters (size, type, etc.) | Highly customizable, built-in | Steeper learning curve |
locate |
Quick name-based searches | Extremely fast | Requires database updates, name-only |
fzf |
Interactive searches | User-friendly, great for exploration | Best for interactive use, not scripts |
fd |
Simple, fast searches | Intuitive syntax, colorful output | Fewer advanced options than find
|
Recommendation: Start with fd for quick searches and fzf for interactive exploration. Use find for scripts or complex criteria. Reserve locate for speed when you know the file name.
For Content Searches
| Tool | Best For | Pros | Cons |
|---|---|---|---|
grep |
General-purpose searches | Universally available, reliable | Slower on large codebases |
ripgrep |
Large codebases, speed | Fastest, respects .gitignore
|
Requires installation |
ag |
Code searches | Fast, developer-friendly defaults | Slightly slower than ripgrep
|
ack |
Small projects, highlighted output | Simple, great for code | Slower, less maintained |
Recommendation: Use ripgrep for most content searches due to its speed and smart defaults. Fall back to grep on systems where it’s preinstalled. Try ag or ack if you prefer their output styles or need specific features.
Combining Tools
For advanced workflows, combine tools. For example, use find to locate files and pipe to ripgrep for content searches:
find . -type f -name "*.py" -exec rg "TODO" {} \;
# Output:
# ./main.py:10:# TODO: Fix bug
# ./utils.py:5:# TODO: Add tests
Experiment with these tools to find what fits your workflow. Share your favorite combos or tips in the comments!
Top comments (0)