Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Linux fd command: terminal file search methods

· 6 min read · Home Tech

In this article
  1. Background: Why fd exists
  2. How fd works
  3. Installing fd
  4. Basic usage examples
  5. Advanced features and options
  6. Common problems and troubleshooting
  7. Comparison with find and other tools
  8. Safety and best practices
  9. Frequently Asked Questions

The fd command is a fast, user-friendly alternative to the traditional find command for locating files and directories in the Linux terminal. It offers intuitive syntax, sensible defaults, and impressive performance, making it a favorite among developers and system administrators. This article explains what fd is, how it works, how to install it, and how to use it effectively for common file search tasks.

Background: Why fd exists

The standard find utility has been a Unix staple for decades, but its syntax can be cryptic and its behavior requires explicit flags for even basic operations. For example, to search for a file by name in the current directory, you must type find . -name "filename". fd was created by David Peter in 2017 to address these pain points. It is written in Rust, which contributes to its speed and memory safety. The name is a playful abbreviation of "find" but with a modern twist. Its goal is to be faster, simpler, and more intuitive than find, while still being powerful enough for complex queries.

How fd works

fd operates by recursively walking the directory tree, but it applies several smart defaults that make it stand out:

  • Hidden files are ignored unless you use the -H or --hidden flag.
  • Gitignore rules are respected automatically if you are inside a Git repository, so files listed in .gitignore are excluded.
  • Glob patterns are used by default, so you can search with wildcards like *.txt without needing -name.
  • Case-insensitive search is the default, though you can override it with -s or --case-sensitive.
  • Colors are used in output to distinguish file types, making results easier to scan.

Under the hood, fd uses parallel directory traversal and a simple regex engine, which contributes to its speed. It also caches some metadata to speed up repeated searches.

Installing fd

Installation varies by distribution. On Debian/Ubuntu, the package is named fd-find, and the binary is invoked as fdfind to avoid conflict with another utility. You can install it with:

sudo apt install fd-find

On Fedora, use dnf install fd-find. On Arch, it is pacman -S fd. For macOS, brew install fd. If your package manager doesn't have it, you can download a precompiled binary from the GitHub releases page and place it in your PATH. After installation, you may want to create an alias fd=fdfind on Debian-based systems for convenience.

Basic usage examples

Here are some common commands to get you started:

  • Search for files by name: fd "pattern" – this searches the current directory recursively for any file or directory whose name matches the pattern.
  • Search in a specific directory: fd "pattern" /path/to/dir – the path is given as the second argument.
  • Show only files or directories: fd -t f "pattern" for files, fd -t d "pattern" for directories.
  • Include hidden files: fd -H "pattern".
  • Ignore gitignore rules: fd -I "pattern".
  • Execute a command on each result: fd "pattern" -x command – for example, fd "*.txt" -x wc -l will run wc -l on every text file.

One of the most useful features is the ability to use glob patterns without escaping. For instance, fd "*.log" will find all files ending in .log in the current tree.

Advanced features and options

fd supports a rich set of options for more complex searches:

  • Regular expressions: The pattern is treated as a regex by default. For example, fd '^[0-9]+$' matches files with only digits in the name.
  • File size and modification time: Use --size and --changed-within or --changed-before to filter by size or date. For example, fd --size +10M finds files larger than 10 MB, and fd --changed-within 1d finds files modified in the last day.
  • Exclude directories: Use -E or --exclude to skip certain paths, e.g., fd -E node_modules "pattern".
  • File extensions: -e or --extension lets you specify an extension, e.g., fd -e py finds Python files.
  • Maximum depth: --max-depth limits recursion depth, useful for large trees.

These options can be combined, giving you a powerful command-line search tool that often replaces find in daily workflows.

Common problems and troubleshooting

If fd does not behave as expected, consider these common issues:

  • Command not found: On Debian/Ubuntu, the binary is fdfind. Use that or create an alias.
  • Hidden files not showing: Remember fd ignores hidden files by default. Use -H.
  • Gitignored files missing: If you're in a Git repo, fd respects .gitignore. Use -I to include them.
  • Case sensitivity: By default, search is case-insensitive. Use -s for case-sensitive.
  • Pattern interpreted as regex: If you want a literal string, escape special characters or use -F (fixed string) option.

If you need to debug, use fd --debug to see what it's doing internally.

Comparison with find and other tools

While find is more universal and offers more advanced predicates, fd is often faster and more ergonomic for everyday searches. Here's a quick comparison:

  • Syntax: find requires -name for name matching; fd uses patterns directly.
  • Defaults: find shows everything including hidden files; fd hides them by default.
  • Git awareness: fd respects .gitignore automatically; find does not.
  • Speed: fd is generally faster due to parallel traversal and Rust's performance.
  • Portability: find is available on virtually every Unix-like system; fd may need installation.

Other tools like locate (or mlocate) are faster for whole-system searches but rely on a database that may be outdated. fd is a good middle ground for real-time, directory-specific searches.

Safety and best practices

When using fd with -x (execute), be careful with the command you run, as it will be executed for each match. Always test with echo first. For example, fd "*.tmp" -x echo will list what would be executed. Also, be mindful of symlinks: by default, fd does not follow symbolic links unless you use -L. This prevents infinite loops and unexpected results. Finally, when deleting files, always double-check the output before piping to rm.

Frequently Asked Questions

How do I search for a file by exact name?

Use fd -F "exactname" to treat the pattern as a literal string. Alternatively, escape regex special characters, e.g., fd "exactname" works if the name has no special characters.

Can I use fd to find files modified in the last hour?

Yes, use fd --changed-within 1h. You can also use --changed-before for older files.

How do I exclude a directory from the search?

Use -E or --exclude, e.g., fd -E node_modules "pattern". You can specify multiple exclusions.

Is fd available for Windows?

Yes, there are Windows binaries available on the GitHub releases page. It works in PowerShell and CMD, though some features may differ.

What is the difference between fd and fzf?

fd is a file finder, while fzf is a fuzzy finder that works as an interactive filter. They are often used together: fd | fzf allows you to interactively select a file from the results.

How do I make fd my default search command?

You can create an alias in your shell configuration, e.g., alias find=fd (though that might break scripts expecting find). Better to just use fd directly and learn its syntax.