Linux fd command: terminal file search methods
In this article
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
-Hor--hiddenflag. - Gitignore rules are respected automatically if you are inside a Git repository, so files listed in
.gitignoreare excluded. - Glob patterns are used by default, so you can search with wildcards like
*.txtwithout needing-name. - Case-insensitive search is the default, though you can override it with
-sor--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-findOn 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 -lwill runwc -lon 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
--sizeand--changed-withinor--changed-beforeto filter by size or date. For example,fd --size +10Mfinds files larger than 10 MB, andfd --changed-within 1dfinds files modified in the last day. - Exclude directories: Use
-Eor--excludeto skip certain paths, e.g.,fd -E node_modules "pattern". - File extensions:
-eor--extensionlets you specify an extension, e.g.,fd -e pyfinds Python files. - Maximum depth:
--max-depthlimits 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
fdignores hidden files by default. Use-H. - Gitignored files missing: If you're in a Git repo,
fdrespects.gitignore. Use-Ito include them. - Case sensitivity: By default, search is case-insensitive. Use
-sfor 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:
findrequires-namefor name matching;fduses patterns directly. - Defaults:
findshows everything including hidden files;fdhides them by default. - Git awareness:
fdrespects.gitignoreautomatically;finddoes not. - Speed:
fdis generally faster due to parallel traversal and Rust's performance. - Portability:
findis available on virtually every Unix-like system;fdmay 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.