Replace find with fd on Linux

The fd command is a popular, user-friendly alternative to the find command.
52 readers like this.
How to find files in Linux

Lewis Cowles, CC BY-SA 4.0

Many Linux programmers use the find command every single day of their career. But find gives a limited set of filesystem entries, and if you have to do a large set of find operations, it's not even very fast. So instead, I prefer to use the Rust fd command because it provides sensible defaults that work for most use cases.

As its README says, "fd is a program to find entries in your filesystem. It is a simple, fast, and user-friendly alternative to find." It features parallelized directory traversal, so it can search multiple directories at once. It supports regular expressions (regex) and glob-based patterns.

Install fd

On Linux, you can install fd from your software repository (a list of available packages can be found on the fd page on Repology.) For example, on Fedora:

$ sudo dnf install fd-find

On macOS, use MacPorts or Homebrew.

Alternately, you can use Rust's Cargo package manager:

$ cargo install fd-find

Use fd

To do a simple search, run fd after any argument, such as:

$ fd sh
registry/src/github.com-1ecc6299db9ec823/cc-1.0.67/src/bin/gcc-shim.rs
registry/src/github.com-1ecc6299db9ec823/exa-0.10.1/completions/completions.bash
registry/src/github.com-1ecc6299db9ec823/exa-0.10.1/completions/completions.fish
registry/src/github.com-1ecc6299db9ec823/exa-0.10.1/completions/completions.zsh
registry/src/github.com-1ecc6299db9ec823/exa-0.10.1/xtests/run.sh
registry/src/github.com-1ecc6299db9ec823/git2-0.13.18/src/stash.rs
registry/src/github.com-1ecc6299db9ec823/libc-0.2.94/src/unix/solarish
registry/src/github.com-1ecc6299db9ec823/libgit2-sys-0.12.19+1.1.0/libgit2/cmake/SelectHashes.cmake
registry/src/github.com-1ecc6299db9ec823/libgit2-sys-0.12.19+1.1.0/libgit2/include/git2/stash.h
registry/src/github.com-1ecc6299db9ec823/libgit2-sys-0.12.19+1.1.0/libgit2/include/git2/sys/hashsig.h
registry/src/github.com-1ecc6299db9ec823/libgit2-sys-0.12.19+1.1.0/libgit2/script/backport.sh
registry/src/github.com-1ecc6299db9ec823/libgit2-sys-0.12.19+1.1.0/libgit2/script/leaks.sh
registry/src/github.com-1ecc6299db9ec823/libgit2-sys-0.12.19+1.1.0/libgit2/script/valgrind.sh
registry/src/github.com-1ecc6299db9ec823/libgit2-sys-0.12.19+1.1.0/libgit2/src/config_snapshot.c
[...]

If you want to search for a specific directory, provide the directory path as a second argument to fd, such as:

$ fd passwd /etc
/etc/pam.d/passwd
/etc/passwd
/etc/passwd-
/etc/security/opasswd

To search for a particular file extension, use -e as an option. For example:

$ fd . '/home/ssur/exa' -e md
/home/ssur/exa/README.md
/home/ssur/exa/devtools/README.md
/home/ssur/exa/man/exa.1.md
/home/ssur/exa/man/exa_colors.5.md
/home/ssur/exa/xtests/README.md
$ 

You can also execute a command by providing -x or -X.

  • The -x/--exec option runs an external command for each search result (in parallel).
  • The -X/--exec-batch option launches the external command once with all search results as arguments.

For example, to recursively find all ZIP archives and unpack them:

$ fd -e zip -x unzip

Or, to list all files under a particular directory that were changed within the last n number of days, use the --changed-within option:

$ fd . '/home/ssur/Work/' --changed-within 10d
/home/ssur/Work/wildfly/connector/src/main/java/org/jboss/as/connector/subsystems/data_sources/JdbcDriverAdd.java
/home/ssur/Work/wildfly/connector/src/main/java/org/jboss/as/connector/subsystems/data_sources/JdbcExample.java
[...]

And to search all files that were changed before a specific number of days, use the --changed-before n option:

$ fd . '/home/ssur/Work/' --changed-before 365d

Here, . acts as a wildcard entry to instruct fd to return all files.

To learn about more the functionalities of fd, consult its documentation on GitHub.

Conclusion

One thing I especially like about fd is that the search pattern is case-insensitive by default, which makes it easier to find things even when you have imprecise knowledge about what you're looking for. Better yet, it automatically switches to case-sensitive if the pattern contains an uppercase character.

Another benefit is that it uses color-coding to highlight different file types.

If you are already using this amazing Rust tool, please let us know your thoughts in the comments.

What to read next
Sudeshna is from Kolkata and currently working for the Red Hat Middleware team in Pune, India. She loves to explore different open source projects and programs. She started contributing to Open Source a couple of years back as an Hacktober Participant since then she has been an avid blogger at Dzone on Python and Data Science. She enjoys her free time stargazing and loves continental cuisine. She is an RHCSA and is working her way to RHCA.
Sudeshna is from Kolkata and currently working for the Red Hat Middleware team in Pune, India. She loves to explore different open source projects and programs. She started contributing to Open Source a couple of years back as an Hacktober Participant since then she has been an avid blogger at Dzone on Python and Data Science. She enjoys her free time stargazing and loves continental cuisine.

Comments are closed.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.