How to Use the fd Command in Linux (‘find’ Alternative)

$ fd [options]

fd is an alternative and faster command line utility for the find command. It does the job of finding files and directories based on their names, sizes, types, and other attributes. The fd command has been designed with simplicity and performance in mind and supports features like colors, and a lot more.

It’s available for MacOS, Linux and Windows on the official website and via Homebrew, APT and Chocolatey package managers.

In this tutorial we will cover the fd command line utility. We will cover how to install it and how to use it with useful examples and detailed explanations.

One of the core utilities in Linux is the find command line utility. find is a utility first originally written for Unix systems by Bell Labs Computing Sciences Research Center in version 1 of Unix in 1970.

A good 20+ years before Linus Torvalds created the first version of the Linux kernel. So in terms of computing application software find is one of the most oldest, still in use and still maintained software programs.

Most of the core utilities in Linux are still in use because, besides aging gracefully in function, they still do their one job very well. The design principle back then was to create an application for one job, stick to that one job and not expand so much out of the realm of that job.

They have been tested a billion times and used so much that there’s hardly any bugs found on them. As the building core of an operating system they are as reliable as they can get.

Being this old though, there’s room for improvement. Nowadays there are better programming languages and better software development practices. Open source developed projects are now cooperatively developed and not reigned by a limited number of maintainers.

47 years later after find was first published comes out fd.

What Is fd and Why Rust

fd is developed in the Rust programming language. Rust is a safe language to develop in because it has sane and secure defaults that prevent developers from introducing accidental bugs. It is considered a memory safe language that prevents buffer overflow attacks from happening. Buffer overflows are one of the main ways other programming languages like C++ get attacked, and applications written with them getting hacked.

Besides it’s memory safe features, it is also known to be a fast language to execute, and most of the utilities written in Rust tend to be blazing fast.

fd is a program designed to find entries in your file system. It’s a searching application to find anything in your file system. If it exists in the file system it can find it. While fd aims to function as the find utility it does not completely replaces it, there are still situations where find’s more powerful functionality would be more appropriate.

fd is part of the new breed of applications written in Rust that are replacing old Linux core utilities, like bat replacing cat, exa replacing ls, ripgrep replacing grep and fzf navigation instead of cd. Take a look at our fzf guide to learn how to use one of the most useful command line applications today.

How To Install fd

fd is available to be installed by most of the Linux distribution’s package managers.

On Ubuntu you can install by running.

sudo apt install fd-find

On Debian based systems you can install by running.

sudo apt-get install fd-find

On Arch based system you can install by running.

pacman -S fd
Note that on Ubuntu and Debian based systems the binary name is called fdfind as the fd name was already taken by another application.

The binary are the actual files that contains the source code of the application. There are two things you can do if you are on one of these systems. You can either use the application by it’s name fdfind (and using that in all the examples that we provide in this tutorial) or you can execute the following command.

ln -s $(which fdfind) ~/.local/bin/fd

This will create a symbolic link on the location of the fdfind binary, giving it another name fd by creating an entry in our local system user application bin to execute by this new name.

If you do perform this task forget about it using the fdfind name and you can proceed to use fd as in this tutorial.

This is how your perform the most basic function of fd. Run fd with an argument that will function as the search pattern.

fd running

In this example we ran an ls -lha command to show all the files in the directory for demonstration purpose.

After listing the files we can see there are a few files. With fd we search for a particular search pattern, we searched for running and it outputted all the files that have running in it’s name. This is the most basic search we can perform with fd, just looking for a simple pattern to match for in the file name.

fd Search by Extensions

fd has a lot of available options to use. One of the most useful ones is to search for the extensions pattern.

We can tell it to look for the extensions by using the -e option before the argument. That is the following command.

fd -e mp4

In this demonstration we searched for all the files in the directory that have .mp4 as their extension, then we searched for all the files with .gif extension, and finally we searched for the files with the .png extension. For demonstration purpose only we made a list command to show all the files that were in the directory.

As you can see, filtering an fd search by extensions can be a quick way to find all the files in the directory with this extension.

fd Searching by Regex Syntax

In Linux there are standard syntax systems used and adopted by various applications. It’s called Regular Expression, regex for short. It’s a concept developed by mathematician Stephen Cole Kleene in the 1950s and adopted by Unix in the late 1960s at Bell Labs.

While there are a few in use in Linux today, fd uses the Rust library’s regex syntax that doesn’t deviate much from the others. You can learn more about regex at RegexOne.com.

fd '^run.'

Here we are using the regex syntax to find our files with. The regex format used the ^ to mean the beginning of a search string. So we enclose the code with apostrophes, add our ^ to mean the beginning, input run as our search pattern, and a period at the end to signal the end of the syntax.

Our fd regex syntax search returned us all the files in our directory that starts with run. Regex syntax searching can be infinitely powerful to find file names that end a certain way, start a certain way, contain a certain word. That has a special character pattern, that has a special numbering system. The possibilities are endless in order for us to find the files we need using syntax searching. The Regular Expression syntax is a deep topic and out of scope to explain here in this article, but to learn more about the syntax that fd uses you can visit here. https://docs.rs/regex/1.0.0/regex/#syntax

Searching Hidden Files With fd

In Linux there are files that are usually system or configuration files that regular computer users maybe don’t want laying around visibly.

Those files are usually hidden as to not clutter the file managers of the OS or the command line. The usual way to hide files in Linux is to start their name with a period. To follow this Linux convention to not clutter and inconvenience the user, fd also doesn’t search for hidden files as the default.

To enable searching for hidden files we use the -H option.

fd -H episodes

In this demonstration we first listed the files in our directory to see all the files in it. We can see there is a hidden file named .episodes-i-want-dl, we know it’s a hidden file because it starts with a period. Then we do a fd search for episodes but fd doesn’t find anything. When we finally use the -H option to search also for hidden files, fd finds our .episodes-i-want-dl file and outputs it.

Searching and Executing Files Simultaneously Using FD

fd has another time saving feature that can be very useful when doing lots of manually intensive work or data entry. We can search a regular pattern like normally but at the same time we execute the file that fd finds with whatever other command line utility on our system. To execute a file that fd finds we use the option -x.

We know that there is a file named big bunny something in our directory, we don’t quite remember it’s full name and we want to search for it using the regex syntax that starts the name with and we want to open it right away because we know there is only one such file here. So we run the following command.

fd '^big.' -x open

Here we searched for any file that starts with big in its name. With the fd option of -x we told it to execute the file that it finds with the open command. The open command opens any file with the default application that is set to open that extension.

The syntax for executing files is to add the -x option after the search pattern, and after the -x option to input the utility we want to execute.

In the below example we make an fd search, including hidden files with the -H option, with the search pattern episodes to search for our .episodes-i-want-dl file, right away we wanted to delete it so we used the -x option follow by the rm command to remove the file. Right after, for demostration purpose only we listed the files with ls -lha and we see that indeed our .episodes-i-want-dl file is no longer there, it has been found by fd, passed along with the -x to the rm utility and removed by it.

fd -H episodes -x rm

word image 10820 1

The execute option is useful and powerful, combined with the regex syntax you can command it to do almost anything with all the utilities to any file in your system.

With the exclude option we can exclude files from appearing in our search. The exclude option is used with the -E option, capital letter E not to be confused with the lower case -e option that means extensions.

fd -E '*.mp4'

Here we input the fd command by itself to find all the files in the directory. fd by itself works in a similar manner as listing files with the ls command.

We see all the files in the directory and notice that there are lots of mp4 video files that we have no interest in right now. So we run the fd command once again with the -E option and regex syntax * to find all the files that contain .mp4 in their name. The star * regex syntax is used through out Linux and it’s one of the most regularly used Regular Expression, * almost always means to find all. In this case it means to find all .mp4 files. Which it did after we execute the command above.

Smart Case Sensitivity in fd

fd handles case sensitivity in a smart way. By default the case is insensitive to so that it’s able to find as much files as possible. If we use an uppercase character then the search becomes case sensitive. In the below example we see that there are files named running man, some starting with uppercase and some with lowercase.

word image 10820 2

First we did a search for files named running, as it is the default method the case is insensitive. It found 4 files, two starting with uppercase and two with lowercase. Then we did another search, this time with an uppercase search term Running. Since we specifically wrote it in uppercase, fd smart switched to find only files containing running in it’s name but that start with uppercase only.

There will be times that we want to find a file named exactly a certain way and there’s an option to force fd to search in a case sensitive manner. The option -s is to force case sensitivity. There’s another option that acts the opposite, even if we write something in uppercase we would want it to stay case insensitive in the search and that is done with the option -i for insensitivity.

  • fd running (default insensitive)
  • fd Running (smart sensitive)
  • fd -s Running (ignore smart and force sensitivity)
  • fd -i Running (ignore smart and force insensitivity )

Conclusion

As part of the new breed of Rust based utilities for the Linux command line, fd is a wonderful application that should be installed on your Linux box. While there will always be room for the classic core utility find, fd is a welcome addition to our Linux world.

It is fast, faster than find. It is safe. And it is open source, actively developed by hundreds of developers around the world. So check it out and let us know how it goes.

 

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)
0 Comments
Inline Feedbacks
View all comments
You May Also Like