Smart Systems for Home

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

fd alternative to find command?

· 4 min read · Home Tech

In this article
  1. Why fd Exists
  2. Key Features of fd
  3. How to Install fd
  4. Common Uses and Examples
  5. fd vs. find: A Comparison
  6. Performance and Speed
  7. Common Pitfalls and How to Avoid Them
  8. Frequently Asked Questions

The fd command is a fast, user-friendly alternative to the traditional find command on Linux and macOS. It offers simpler syntax, sensible defaults, and better performance, making it a popular choice for developers and system administrators.

Why fd Exists

The find command has been a Unix staple for decades, but its syntax can be cryptic. For example, to find all .txt files in the current directory, you write:

find . -name "*.txt"

With fd, the same task is simply:

fd -e txt

This simplicity is the core motivation behind fd. It was created to provide a more intuitive interface while retaining the power of find. The project is open-source and written in Rust, which contributes to its speed and reliability.

Key Features of fd

fd comes with several features that make it stand out:

  • Smart defaults: It ignores hidden files and directories, respects .gitignore, and does not search inside binary files by default.
  • Colorized output: Results are color-coded by file type, making them easier to scan.
  • Parallel execution: It uses multiple threads to search faster, especially on large directory trees.
  • Regex support: Patterns are treated as regular expressions by default, offering more flexibility than simple globs.
  • Automatic exclusion: It skips common noise like .git directories unless explicitly asked to include them.

How to Install fd

Installation is straightforward on most systems:

  • Ubuntu/Debian: sudo apt install fd-find (the binary is named fdfind)
  • macOS: brew install fd
  • Windows (WSL): sudo apt install fd-find or use the native binary from the releases page
  • Arch Linux: sudo pacman -S fd
  • Fedora: sudo dnf install fd-find

After installation, you can verify it works with fd --version.

Common Uses and Examples

Here are practical examples that show how fd simplifies everyday tasks:

  • Find by extension: fd -e md lists all Markdown files.
  • Find by name pattern: fd '^config' finds files starting with "config".
  • Search in a specific directory: fd -e log /var/log searches only in /var/log.
  • Include hidden files: fd -H shows hidden files as well.
  • Execute a command on results: fd -e txt -x wc -l counts lines in every text file.
  • List directories only: fd -t d shows only directories.

fd vs. find: A Comparison

While fd is excellent for interactive use, find still has advantages in certain scenarios:

  • POSIX compliance: find is available on virtually every Unix-like system, making it more portable for scripts.
  • Complex expressions: find supports advanced logical operators like -not, -o, and -prune which can be more expressive for complex queries.
  • Performance on huge trees: In some benchmarks, find can be faster when searching millions of files, though fd is often faster in typical use.
  • System utilities: Some system scripts rely on find syntax, so it remains essential for maintenance tasks.

For daily use, fd is generally more pleasant and less error-prone. Many users alias find to fd in their shell configuration to get the best of both worlds.

Performance and Speed

fd is designed for speed. It uses parallel directory traversal, which can significantly reduce search time on large filesystems. In practice, it often feels instant for typical queries. However, the actual speed depends on the filesystem, disk type, and the number of files. On network drives or extremely large directories, find might occasionally be comparable, but fd generally wins in interactive scenarios.

Common Pitfalls and How to Avoid Them

Here are a few mistakes users make when switching to fd:

  • Expecting find syntax: fd uses regex, not glob. For example, fd '*.txt' will not work as expected because * is a quantifier. Use fd -e txt instead.
  • Forgetting to include hidden files: By default, fd ignores hidden files. Use -H to include them.
  • Assuming it respects .gitignore everywhere: This behavior is on by default, but you can disable it with -I to search ignored files.
  • Using fd in scripts without checking availability: Since fd is not installed everywhere, scripts should fallback to find for portability.

Frequently Asked Questions

Is fd faster than find?

Yes, fd is generally faster because it uses parallel traversal and has optimized internals. In typical use, it feels quicker, especially on large directory trees. However, the difference varies by system and workload.

Can I use fd as a drop-in replacement for find?

Not completely. fd has a different syntax and does not support all find options. For simple searches, it works as a replacement, but for complex expressions or POSIX compliance, you may still need find.

How do I include hidden files in fd?

Use the -H flag. For example, fd -H will show hidden files and directories. To also respect .gitignore but include hidden files, use -H without -I.

Does fd work on Windows?

Yes, fd can be installed on Windows via WSL, or as a native binary. The native version works in PowerShell and CMD, but some features like colorized output may depend on the terminal.

How do I exclude a directory in fd?

Use the -E flag. For example, fd -E node_modules will exclude the node_modules directory. You can also use multiple -E flags.

Can fd search for files by content?

No, fd only searches file names. For content search, use grep -r or tools like ripgrep (which is often used alongside fd).