Dev tools and workflow · Beginner · 16 min
Find files and text fast with ripgrep and fd
By Padding · Updated 26 Nov 2026
Ubuntu 26.04 · ripgrep 16.0.0 · fd 10.5.2 (official release binaries)
You will install ripgrep 16 and fd 10.5, search a real open-source codebase by file type, glob, whole word and fixed string, feed results to scripts as JSON, set your own defaults in a config file, find files by name and age, run commands on them, and leave with five searches you can use every day.
On this page
grep -r works everywhere, but in a real project it wastes your time: it reads build output, virtual environments and node_modules, and you have to tell it what to skip every time. ripgrep (rg) searches file contents and fd (fd) finds files by name. Both skip what your .gitignore already says is not source, skip hidden folders, and search in parallel, so the first answer is usually the right one.
You will work in a shallow copy of Requests, the Python HTTP library, pinned to the tag v2.32.3 so that the counts below match what you see. Ubuntu 26.04 packages ripgrep 15.1 and fd 10.3. This tutorial installs the current upstream releases, ripgrep 16.0.0 and fd 10.5.2, but every command except -G also works on the Ubuntu packages.
Step 1 · Install ripgrep and fd
Install the tools you need to download and unpack the releases, plus Git and jq, which later steps use:
sudo apt update
sudo apt install -y git curl ca-certificates jqBoth projects publish static Linux binaries. The commands below choose the right download for your processor (x86_64 for most servers and laptops, aarch64 for ARM machines such as a Raspberry Pi 4 or an ARM cloud server), check the ripgrep download against its published SHA-256 sum, and copy both programs to /usr/local/bin:
RG_VERSION=16.0.0
FD_VERSION=10.5.2
ARCH="$(uname -m)"
mkdir -p ~/search-tools-download
cd ~/search-tools-download
curl -fsSLO "https://github.com/BurntSushi/ripgrep/releases/download/${RG_VERSION}/ripgrep-${RG_VERSION}-${ARCH}-unknown-linux-musl.tar.gz"
curl -fsSLO "https://github.com/BurntSushi/ripgrep/releases/download/${RG_VERSION}/ripgrep-${RG_VERSION}-${ARCH}-unknown-linux-musl.tar.gz.sha256"
sha256sum -c "ripgrep-${RG_VERSION}-${ARCH}-unknown-linux-musl.tar.gz.sha256"
curl -fsSLO "https://github.com/sharkdp/fd/releases/download/v${FD_VERSION}/fd-v${FD_VERSION}-${ARCH}-unknown-linux-musl.tar.gz"
tar xzf "ripgrep-${RG_VERSION}-${ARCH}-unknown-linux-musl.tar.gz"
tar xzf "fd-v${FD_VERSION}-${ARCH}-unknown-linux-musl.tar.gz"
sudo install -m 755 "ripgrep-${RG_VERSION}-${ARCH}-unknown-linux-musl/rg" /usr/local/bin/rg
sudo install -m 755 "fd-v${FD_VERSION}-${ARCH}-unknown-linux-musl/fd" /usr/local/bin/fd
cd ~
rm -rf ~/search-tools-downloadsha256sum -c prints ripgrep-16.0.0-x86_64-unknown-linux-musl.tar.gz: OK (with your architecture in the name). fd does not publish a checksum file next to its tarballs, so that download is trusted on HTTPS alone.
Binaries copied by hand get no security updates from apt. To upgrade later, check the two release pages, change RG_VERSION and FD_VERSION, and run the same block again. To remove them, run sudo rm /usr/local/bin/rg /usr/local/bin/fd.
If you prefer packages that apt keeps up to date, install Ubuntu's older versions instead. Ubuntu names the fd program fdfind, because another package already uses fd, so the alias makes the commands in this tutorial work:
sudo apt install -y ripgrep fd-find
alias fd=fdfindOn macOS, brew install ripgrep fd installs both under their usual names.
brew install ripgrep fdCheck it worked
rg --version
fd --versionThe first line of output is ripgrep 16.0.0 followed by a revision, and the second command prints fd 10.5.2.
Step 2 · Clone a project and compare grep with rg
Clone Requests at a fixed tag into a throwaway folder. --depth 1 downloads only that snapshot, about 10 MB on disk instead of the full history, which matters when you are on mobile or prepaid data:
mkdir -p ~/search-demo
cd ~/search-demo
git clone --depth 1 --branch v2.32.3 https://github.com/psf/requests.git
cd requestsGit prints a note about a "detached HEAD", which is normal when you check out a tag. Now make the folder look like a project someone has been working in. Building a Python package leaves a copy of the source in build/, and the project's .gitignore lists build/. Copy the source there to stand in for that output:
mkdir -p build
cp -r src/requests build/
git check-ignore -v build/requests/api.pygit check-ignore -v prints something like .gitignore:10:build/ build/requests/api.py: the line of .gitignore that excludes the file. Now search for function definitions that start with request, first with grep, then with ripgrep:
grep -rn "def request" . | wc -l
rg -n "def request" | wc -l
rg -n "def request"grep finds 6 lines, because it reports every match twice, once in src/ and once in the copy in build/. ripgrep finds 3, all in src/requests/: it read .gitignore and skipped build/. You did not have to tell it to, and you did not give it a path, because it searches the current folder by default.
In a terminal, rg groups results under each file name and colours the matches. When its output goes to a pipe or a file, it prints one path:line:text line per match instead, which is what scripts want. The -n flag forces line numbers in both cases.
Check it worked
rg -n "def request" src/requests/api.pyThe output is 14:def request(method, url, **kwargs):. When you name a single file, rg leaves out the path.
Step 3 · Narrow a search by file type and glob
Most searches only care about one kind of file. -t limits rg to a file type it knows, and -T excludes one. --type-list shows which extensions belong to each type:
rg --type-list | grep -E '^(py|md|rst):'
rg -t py -l timeout
rg -T py -l timeoutThe first command prints py: *.py, *.pyi and the matching lines for Markdown and reStructuredText. -l lists only the names of files that match, not the lines. -t py -l timeout lists 8 Python files: four in src/requests/ and four in tests/. -T py lists the documentation and changelog files that mention timeouts instead.
Globs (-g) match paths, the way .gitignore patterns do. A glob that starts with ! excludes. Quote globs so the shell does not expand them first:
rg -l Session -g '!tests/'
rg -l requests -g '*.md'The first lists the 10 files outside tests/ that mention Session. The second prints README.md and HISTORY.md. ripgrep 16 adds -G as a clearer way to write an exclusion, so rg -l Session -G 'tests/' gives the same result. Use -g '!...' if your copy of ripgrep might be older.
Check it worked
rg -t py -l timeout | wc -lThe output is 8.
Step 4 · Match whole words and literal text
By default the pattern can match inside a longer word. Searching sessions.py for session also counts sessions, session_hooks and session_setting. -w only accepts matches with a non-word character (or the start or end of the line) on both sides:
rg -c session src/requests/sessions.py
rg -c -w session src/requests/sessions.py-c counts matching lines. The first prints 20, the second 9.
The pattern is a regular expression, so characters such as ., *, ( and [ have special meanings. A search for **kwargs is not a valid regular expression at all:
rg '**kwargs' src/requests/api.py || echo "rg exited with status $?"
rg -F -c '**kwargs' src/requests/api.pyThe first command prints regex parse error with repetition operator missing expression, then rg exited with status 2. -F (fixed strings) treats the whole pattern as plain text, and the second command prints 16. Use -F whenever you paste an error message, a URL or a piece of code into a search.
ripgrep's exit status is worth remembering for scripts: 0 means it found something, 1 means no match, and 2 means an error, such as a bad pattern.
Patterns and files are read as UTF-8, and case-insensitive matching (-i) understands letters outside English, so a search for -i 'ọjọ́' also finds Ọjọ́ in Yorùbá text. Try it without a file by piping a line in:
printf 'Ọjọ́ Ajé\n' | rg -i 'ọjọ́'rg prints the line Ọjọ́ Ajé, with the capital letter matched by your lowercase pattern.
Check it worked
rg -n -w 'def merge_setting'You see one line, src/requests/sessions.py:61:def merge_setting(..., the place the function is defined.
Step 5 · Show context, count, and preview a replacement
A matching line on its own rarely tells you enough. -A adds lines after each match, -B lines before, and -C both:
rg -n -A 3 '^def get\(' src/requests/api.py
rg -n -C 1 -w 'def get' src/requests/sessions.pyThe first shows line 62, def get(url, params=None, **kwargs):, and the three lines of its docstring that follow. Context lines use - after the line number and matching lines use :, so you can tell them apart. When two groups of context are far apart, rg separates them with --.
There are three ways to count, and they answer different questions:
rg -c timeout src/requests/adapters.py
rg --count-matches timeout src/requests/adapters.py
rg -t py --stats timeout | tail -8-c prints 20, the number of lines that match. --count-matches prints 28, because some lines mention timeout more than once. --stats adds a summary after the results: for all Python files it reports 117 matches, 93 matched lines and 8 files contained matches, plus timing.
-r replaces each match in the output. It never edits a file, so it is a safe way to preview a rename. Groups in parentheses become $1, $2 and so on. Keep the replacement in single quotes so the shell does not treat $1 as a variable:
rg -n 'requests\.get\(' -r 'httpx.get(' README.md
rg -o 'requests\.(get|post)\(' -r 'httpx.$1(' docs/user/quickstart.rst | sort | uniq -cThe first prints line 7 of the README with requests.get( shown as httpx.get(. In the second, -o prints only the matched text, so the counts show 15 httpx.get( and 9 httpx.post(: how many call sites a move to another library would touch in that page.
Check it worked
git status --shortThe output is empty. Nothing in the repository changed, and build/ does not show because Git ignores it.
Step 6 · Use rg output in scripts with --json
Parsing path:line:text breaks as soon as a file name or a line contains a colon. --json prints one JSON object per line instead. Each object has a type: begin and end mark each file, match carries one matching line with its path, line number and the exact position of each match, and a final summary holds the totals. -m 1 stops after the first match in each file, which keeps this first look short:
rg --json -m 1 -w timeout src/requests/api.pyYou get four lines: begin, one match, end and summary. The match line starts {"type":"match","data":{"path":{"text":"src/requests/api.py"},"lines":... and includes "line_number":32. Use jq to pick fields out, as in the jq tutorial. Count matching lines per file, then read the totals from the summary:
rg --json -t py timeout | jq -r 'select(.type == "match") | .data.path.text' | sort | uniq -c
rg --json -t py timeout | jq -c 'select(.type == "summary") | .data.stats | {matched_lines, matches}'The first lists the 8 files with a count beside each, 29 for tests/test_requests.py being the largest. The second prints {"matched_lines":93,"matches":117}, the same numbers --stats gave.
Check it worked
rg --json -t py timeout | jq -s '[.[] | select(.type == "match")] | length'The output is 93.
Step 7 · Set your own defaults in ~/.ripgreprc
ripgrep reads extra flags from a file, but only when the environment variable RIPGREP_CONFIG_PATH points to it. Put one flag per line. Lines starting with # are comments. These four defaults suit most people:
cat > ~/.ripgreprc <<'EOF'
# Ignore case unless the pattern contains a capital letter
--smart-case
# Search hidden files and folders such as .github/, but never inside .git/
--hidden
--glob=!.git/
# Cut very long lines (minified files) to a short preview
--max-columns=200
--max-columns-preview
EOF
echo 'export RIPGREP_CONFIG_PATH="$HOME/.ripgreprc"' >> ~/.bashrc
export RIPGREP_CONFIG_PATH="$HOME/.ripgreprc"The echo line makes new terminals pick up the setting, and the export applies it to this one. If you use zsh, which is the default on macOS, append the same line to ~/.zshrc instead. Flags you type on the command line override the file, and --no-config ignores it for one search.
Check it worked
rg -c session src/requests/sessions.py
rg --no-config -c session src/requests/sessions.py
rg -l codeqlThe first prints 31: with --smart-case, the lowercase pattern now also matches Session. The second ignores the file and prints 20 again. The third finds .github/workflows/codeql-analysis.yml, a hidden folder ripgrep skipped before.
Step 8 · Find files by name, extension and age with fd
fd's pattern is a regular expression matched against file names, anywhere below the current folder. Like ripgrep, it skips hidden files and anything in .gitignore:
fd api
fd -e md
fd -g 'test_*.py'
fd -t d testfd api prints docs/api.rst and src/requests/api.py, and nothing from build/. -e md finds files by extension: the two Markdown files at the top and three README.md files under tests/certs/ (in tests/certs/, tests/certs/expired/ and tests/certs/mtls/). -g switches to a glob that must match the whole name, here the 9 test modules. -t d returns only directories (f is files, l is symbolic links), here tests/ and tests/testserver/.
-H includes hidden files and -I stops reading .gitignore. Compare the counts:
fd -e py | wc -l
fd -I -e py | wc -l
fd -H -e ymlThe first prints 36 and the second 54, because -I brings in the 18 copies in build/. -H -e yml lists the YAML files in .github/, which fd skipped before.
fd can also filter by when a file last changed. Everything you cloned is a few minutes old, so mark the two top-level Markdown files as last changed on 1 January 2026. touch -t takes the date as YYYYMMDDhhmm and works on both Linux and macOS:
touch -t 202601011200 README.md HISTORY.md
fd -t f --changed-before 30d
fd -t f -e md --changed-within 1d--changed-before 30d prints only HISTORY.md and README.md. --changed-within 1d lists the three Markdown files under tests/certs/, which you did not touch. Both options take durations such as 2h, 3d or 2weeks, or a date such as 2026-06-01.
Check it worked
fd -t f --changed-before 30d | wc -lThe output is 2.
Step 9 · Run a command on every file fd finds
-x runs a command once for each result, several at a time, so the output order changes from run to run. -X runs the command once with all the results as arguments, which suits commands that print a total. A path after the pattern limits the search to that folder, and . as the pattern matches every name:
fd -e py . src/requests -x wc -l
fd -e py . src/requests -X wc -lThe first prints one line count per file, in no fixed order. The second prints them in one list ending with a total line for the 18 files.
Inside the command, placeholders stand for parts of each path: {} is the whole path, {/} the file name, {//} the folder, {.} the path without its extension, and {/.} the file name without its extension:
fd -e rst . docs/user -x echo "{/.} is in {//}"This prints lines such as quickstart is in docs/user.
Before you use -x or -X with anything that deletes or moves files, run it with echo in front of the command so you see exactly what would happen. Here, find every folder called build, including ignored ones, and preview the clean-up:
fd -I -t d '^build$' -X echo rm -rfThe output is rm -rf ./build: the command is printed, not run. When fd hands paths to a command with -x or -X, it puts ./ in front of each one, so a file whose name starts with - cannot be mistaken for an option. Only remove echo once the list is what you expect.
Check it worked
fd -e py . src/requests -X wc -l | tail -1The last line ends in total, with the number of lines in all 18 files.
Step 10 · Keep five searches you will use every day
These five cover most of what you reach for when you open an unfamiliar project. Run each one here, then in your own code:
rg -n -w 'def merge_setting'
rg -n -w merge_setting -g '!tests/'
rg -n 'TODO|FIXME|XXX'
fd -t f --changed-within 2h --max-results 5
fd -H -e yml -e yaml -e toml- Where is it defined?
-wwith the definition keyword (def,function,class,func) finds the one line, heresessions.py:61. - Who uses it? The same name without the keyword, excluding tests, lists every line that mentions it: nine lines, all in
sessions.py, the definition at line 61 and eight calls. Your~/.ripgreprcis active by now, so--smart-caseand--hiddenapply, and the result is the same. - What did people leave unfinished? The capitals in the pattern keep
--smart-casecase-sensitive, so you get theTODOandXXXnotes, not every word with "todo" in it. - What changed recently?
--changed-within 2h(here capped at five results with--max-results) answers "which files did I just edit?" after a break or a branch switch. - Where is the configuration? Settings for CI, linters and tools hide in YAML and TOML, often in hidden folders, so
-Hmatters.
Check it worked
Each command prints at least one result. The third lists notes in adapters.py, hooks.py and auth.py among others, and the last includes pyproject.toml and the workflow files under .github/workflows/.
Step 11 · Clean up
Remove the practice project. ripgrep, fd and your ~/.ripgreprc stay, because you will keep using them:
cd ~
rm -rf ~/search-demoIf you do not want the config file, delete ~/.ripgreprc and remove the RIPGREP_CONFIG_PATH line from ~/.bashrc (or ~/.zshrc) in your editor.
Next steps
- Pick from search results interactively instead of reading them: search files and shell history interactively with fzf.
- Take the
--jsonoutput further: process JSON on the command line with jq. - Brush up on the shell basics these tools build on: use the terminal as a programmer.
Related tutorials
- Work on two branches at once with git worktreeYou will fix an urgent bug in a second working tree without stashing your half-finished feature, merge the fix, and clean up every extra tree afterwards.
- Version releases with semantic versioning, Git tags and a changelogYou will take a small shell library from 1.0.0 to 1.1.0 with written version rules, Conventional Commits, a Keep a Changelog file, annotated Git tags, a build stamped by git describe and release notes drafted from the commits since the last tag.
- Search files and shell history interactively with fzfYou will have fzf 0.75 wired into your shell for history search, file insertion and directory jumps, fed by fd with a preview pane, plus two small pickers that switch Git branches and stop processes.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials