Fast Find Content in Folder via Command

Cover Image for Fast Find Content in Folder via Command
DevOps5 min read

Searching for content within folders can be time-consuming, especially when dealing with large codebases or document collections. This guide introduces powerful command-line tools that will supercharge your search capabilities and help you find what you're looking for in seconds.

1. fzf (Fuzzy Finder)

fzf is a general-purpose command-line fuzzy finder that can be used with any list; files, command history, processes, hostnames, bookmarks, git commits, etc.

Installation

brew install fzf   # for macOS

For other systems:

# Ubuntu/Debian
sudo apt install fzf

# CentOS/RHEL/Fedora
sudo dnf install fzf

Usage

To search for content within files and then interactively filter the results:

grep -r "search_term" /path/to/folder | fzf

Advanced fzf Examples

# Search all files and preview with bat
find . -type f | fzf --preview 'bat --color=always {}'

# Search git files only
git ls-files | fzf

# Search command history
history | fzf

2. ripgrep (rg) with fzf

ripgrep is a line-oriented search tool that recursively searches the current directory for a regex pattern. It's significantly faster than grep and has better defaults.

Installation

brew install ripgrep   # for macOS

For other systems:

# Ubuntu/Debian
sudo apt install ripgrep

# CentOS/RHEL/Fedora
sudo dnf install ripgrep

# Or download from GitHub releases

Usage

Basic ripgrep search combined with fzf for interactive filtering:

rg "search_term" /path/to/folder | fzf

Advanced ripgrep Examples

# Search only in specific file types
rg "function" --type js | fzf

# Search with context lines
rg "error" -C 3 | fzf

# Search case-insensitive
rg -i "TODO" | fzf

# Search for whole words only
rg -w "class" | fzf

# Exclude certain directories
rg "search_term" --glob '!node_modules' | fzf

3. Combining Tools for Maximum Efficiency

Interactive File Search with Preview

Create a powerful search experience by combining ripgrep and fzf with preview capabilities:

rg --line-number --no-heading --color=always "search_term" | \
  fzf --ansi --preview 'echo {} | cut -d: -f1 | xargs bat --color=always --highlight-line {2}'

Create Aliases for Quick Access

Add these to your ~/.bashrc or ~/.zshrc:

# Quick content search
alias fzf-content='rg --line-number --no-heading --color=always . | fzf --ansi'

# Search in current git repository
alias fzf-git='git ls-files | fzf --preview "bat --color=always {}"'

# Search with live preview
alias fzf-live='rg --line-number --no-heading --color=always . | fzf --ansi --preview "echo {} | cut -d: -f1 | xargs bat --color=always --highlight-line {2}"'

4. Additional Tools Worth Mentioning

ag (The Silver Searcher)

Another fast alternative to grep:

brew install the_silver_searcher
ag "search_term" /path/to/folder | fzf

fd (Alternative to find)

A simple, fast alternative to find:

brew install fd
fd "filename" | fzf

Tips for Better Performance

  1. Use .gitignore: Both ripgrep and ag respect .gitignore files by default
  2. Specify file types: Use --type flag to limit search scope
  3. Use regex patterns: Learn basic regex for more precise searches
  4. Combine with other tools: Pipe results to xargs, awk, or other utilities

Conclusion

These tools transform the way you search for content in folders. The combination of ripgrep's speed and fzf's interactive filtering creates a powerful workflow that can handle even the largest codebases efficiently. Start with the basic commands and gradually incorporate the advanced features as you become more comfortable with the tools.