fd find files linux macos?
In this article
Yes, fd is a fast, user-friendly alternative to the traditional find command, and it works on both Linux and macOS. It is designed to be more intuitive, with sensible defaults like ignoring hidden files and respecting .gitignore, while still offering powerful search capabilities.
What is fd and Why Use It?
fd is a modern command-line tool written in Rust that simplifies file searching. It was created to address the complexity and verbosity of the classic find command. While find is incredibly powerful, its syntax can be cryptic for everyday tasks. fd provides a more straightforward approach: you type fd pattern and it searches the current directory recursively, showing results in a clean, colored output.
Key advantages include:
- Speed:
fdis parallelized and often faster thanfindfor large directory trees. - Simple syntax: No need for
-nameor-typeflags for basic searches; just provide the pattern. - Sensible defaults: Hidden files and directories (like
.git) are ignored by default, as are entries in.gitignorefiles. - Regex support: Patterns are treated as regular expressions by default, but you can switch to glob-style matching with
-g.
Installation on Linux and macOS
Installing fd is straightforward on both platforms.
macOS
The easiest way is via Homebrew:
brew install fdIf you prefer MacPorts, you can use:
sudo port install fdLinux
Most distributions have fd in their repositories. For Debian/Ubuntu:
sudo apt install fd-findNote that on Debian-based systems, the binary is named fdfind to avoid conflict with another package. You can create an alias: alias fd=fdfind in your shell configuration.
For Fedora:
sudo dnf install fd-findFor Arch Linux:
sudo pacman -S fdIf your distribution doesn't have it, you can download a precompiled binary from the GitHub releases page.
Basic Usage and Examples
Once installed, using fd is intuitive. Here are some common examples:
- Find files by name:
fd report— finds files or directories with "report" in the name. - Find by extension:
fd -e txt— finds all.txtfiles. - Find directories only:
fd -t d— lists only directories. - Find files only:
fd -t f— lists only files. - Search in a specific directory:
fd pattern /path/to/dir— searches in that directory. - Case-insensitive search:
fd -i pattern— ignores case. - Execute a command on results:
fd -e txt -x wc -l— counts lines in each text file.
The output is color-coded by file type, making it easy to scan.
How fd Differs from find
While find is a POSIX standard and available everywhere, fd offers a different philosophy. Here's a quick comparison:
| Feature | find | fd |
|---|---|---|
| Syntax for name search | find . -name "*.txt" | fd -e txt |
| Hidden files | Included by default | Ignored by default (use -H to include) |
| Respects .gitignore | No | Yes (use -I to ignore) |
| Regex support | Limited (via -regex) | Full regex by default |
| Parallel execution | No | Yes |
For simple searches, fd is faster to type and read. For complex, scripted operations, find remains powerful and portable.
Advanced Options and Customization
fd includes several advanced features that make it a versatile tool:
- Exclude directories:
fd -E node_modules— excludes a directory from the search. - Search for files by size:
fd -s +10M— finds files larger than 10 MB. - Search by modification time:
fd --changed-within 2d— finds files modified in the last 2 days. - Use glob patterns:
fd -g '*.log'— uses glob-style matching instead of regex. - List all files including hidden:
fd -H— shows hidden files and directories. - Ignore .gitignore:
fd -I— includes ignored files.
You can also create aliases or wrapper functions in your shell to customize behavior. For example, to always show hidden files, add alias fd='fd -H' to your .bashrc or .zshrc.
Common Issues and Troubleshooting
Here are a few problems you might encounter and how to solve them:
- Command not found: If you installed
fd-findon Debian/Ubuntu, usefdfindinstead, or create an alias. - No results when searching for a file: Remember that
fdignores hidden files and.gitignoreby default. Use-Hand-Ito override. - Search is slow on large directories: Use
-Eto exclude large directories likenode_modulesor.git. - Want to search for an exact filename? Use
-gwith a glob pattern likefd -g 'exact.txt'.
Frequently Asked Questions
Is fd available on Windows?
Yes, fd is cross-platform and can be installed on Windows via choco install fd or by downloading the binary. However, this article focuses on Linux and macOS usage.
Can I use fd in scripts instead of find?
Yes, fd is script-friendly. It outputs results one per line, which works well with xargs or while read loops. However, if you need strict POSIX compliance, find is more portable.
Does fd support searching file contents?
No, fd only searches filenames. For content search, use grep or ripgrep (which is also written in Rust and pairs well with fd).
How do I update fd to the latest version?
If installed via a package manager, use the same manager to update (e.g., brew upgrade fd on macOS). For manual installs, download the new binary and replace the old one.
Why is fd faster than find?
fd uses parallel directory traversal and avoids stat calls on every file by default, which reduces overhead. It also stops early when it finds matches in some cases, though for full listings it still traverses everything.
Can I make fd the default find command?
You can create a shell alias like alias find=fd, but be aware that scripts expecting find syntax will break. It's safer to use fd explicitly.