fd - Best alternative to Find Command
Tags: beginners, ubuntu, linux, computerscience
Published: 2020-07-11 14:26:35

Last few posts in [Linux For Beginners](https://dev.to/yashsugandh/commands-for-viewing-and-sorting-files-452j) 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](https://github.com/sharkdp/fd)

**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](https://dev-to-uploads.s3.amazonaws.com/i/5unvzgx9vnvvtj2i2t4z.png)

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

![find file-name](https://dev-to-uploads.s3.amazonaws.com/i/itltx9utz4p7cg628tul.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/gs0mcjfu56wjs48k7rnr.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/ywa0sl7a7m29q0ovx62k.png)

- exclude idea JSON files

![fd-json-excluded](https://dev-to-uploads.s3.amazonaws.com/i/f4beu9rgh6uavjzlmt5v.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/uvl2hqyk6rhgur75flmf.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/2z0glmncnz8kurzvi2q5.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/u3gi4vsxom6vyhmhr38c.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/if5anig9yj5vd9y4q2lj.png)

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.