DEV Community

loading...
Cover image for fd - Best alternative to Find Command

fd - Best alternative to Find Command

yash sugandh
A software engineer trying to figure out how stuff works
・4 min read

Last few posts in Linux For Beginners has been quite hectic. So I thought why not learn something fun and useful today the fd command.

I know what you are thinking that learning a command is not a very nice way to have fun but if you are like me then learning something new that easy to use but packs quite a punch will definitely bring a smile to your face.

So let's not waste any time and go-ahead to our today's topic fd command.

The fd command is a simple, fast, and user-friendly alternative to find.

So why use this over find?

We use fd over find because

  1. It's easy to use
  2. Does not look in hidden files and directories by default.
  3. It's faster than the find command
  4. Parallel command execution
  5. Ignores patterns from your .gitignore, by default.

I can keep going but let's look at them in detail later :)

So, can we replace find with fd?

Well not exactly, fd covers almost 80% of find utilities. So think of fd as an extension to the find command.

Let's start with how to install the fd command

To install fd on Debian we use

sudo apt install fd-find

For some other Distros

  • Fedora:

    dnf install fd-find

  • Alpine:

    apk add fd

  • Arch Linux:

    pacman -S fd

  • Mac OS:

    brew install fd

If I missed your Distro look into fd command docs

Note: In some distros like Debian there is already a package installed named fd so we install fdfind. To use fd we can just set

alias fd=fdfind

in our shells initialization file.

Now that we have installed fd let's start using it

The syntax of fd command

fd-syntax

To find something we just use fd along with PATTERN(filename, textfiles, images)

find file-name

In the above example, we used the command fd hello-world which returned us the path to the files named hello-world.

Let's take some more examples for better understanding

Note: In the examples mentioned below we are only seeing a portion of the actual output since the complete output is huge.

  • find all the JSON files

fd-with-json-extension

In the above example, we used the command fd -e json where
fd represents the fd command
-e represents the options to filter using the extension
json represents the filter type JSON files

Now what if we wanted all the JSON files but exclude some unwanted files

fd-idea-included

  • exclude idea JSON files

fd-json-excluded

In the above example, we used the command fd -e json -E idea* where
fd represents the fd command
-e represents the options to filter using the extension
json represents the filter type JSON files
-E represents the options to exclude files
idea* represents the files to be excluded idea*

So that was awesome filtering out some unwanted files from search results

Till now we have been searching through all our directories but what if we wanted to search files only in a specific directory

  • find all .txt files in the Documents directory

fd-documents

In the above example, we used the command fd .txt Documents where
fd represents the fd command
.txt represents the filter type text files
Documents represents the directories we want to find the file in

Another functionality that we need would be to filter the files on the basis of type

-filter on the basis of type directory

fd-type

In the above example, we used the command fd -td where
fd represents the fd command
-td represents the option to filter the results on the basis of type which in d directory in this case

We can filter on the basis of other types as well

fd-type

What about the hidden files and directories aren`t they ignored by default then how do we search for those?

  • search .hidden.txt file

fd hidden

In the above example, we used the command fd -H .hidden where
fd represents the fd command
-H represents the option to search through hidden files and directories.
.hidden.txt represents the file to be searched for

That's it for today let me know if you enjoyed it as much as I did.

Please let me know in the discussion below if you have any questions and you can suggest me in the discussion if you have used some utility that you want others to know and learn about.

Discussion (0)