How to use the fd command in Linux

A terminal window on a Linux PC with an Ubuntu-style desktop.
Fatmawati Achmad Zaenuri / Shutterstock

On Linux, fd is an easier alternative to find command. It has a simplified syntax, uses sensible defaults, and has built-in common sense behavior. Let’s put it to the test.

fd versus find: what is the difference?

The fd the command is not intended to replace the traditional find command, which has been on linux, well, forever. Instead of, fd tries to satisfy most of the common uses of find in a simpler way and is often eight or nine times faster than find. You can see some of your landmarks in the GitHub repository page.

fd has a colored output, similar to some ls modes. It is recursive, but does not search for hidden directories by default. Knows about git and it will also automatically ignore any patterns in your “.gitignore” file.

fd It is not case sensitive by default. However, if your search pattern contains an uppercase letter, fd It operates in a case-sensitive mode. You can of course override the defaults, but in many cases they work to your advantage.

RELATED: How to use all Linux search commands

Fd installation

From Ubuntu 19.04 (Disco Dingo,) you can install fd directly by calling the officially maintained package with apt-get. If you are running an older version of Ubuntu, check the installation instructions on the Git hub page.

Write the following:

sudo apt-get install fd-find

In Ubuntu, the command is fdfind to avoid a name conflict with another existing utility. If you want it to be fd, you can set an alias:

alias fd=fdfind

To make the alias persistent and remain available after reboot, put it in your “.bashrc” or “.bash_aliases” file.

RELATED: How to create aliases and shell functions in Linux

Install fd On Fedora, type this command:

sudo dfn install fd-find

In Manjaro, enter the following:

sudo pacman -Syu fd

fd versus fdfind

To avoid confusion, we leave the command with its default name, fdfind, on our Ubuntu test PC. fd Y fdfind they are exactly the same command, as you will see in the following example (if you ask fdfind to show its version, it calls itself “fd”):

fdfind --version

We will call the command “fed”, but in the examples, we will use Ubuntu “fdfind”. On other Linux distributions, you can type “fd” instead of “fdfind” to save some keystrokes.

Simple searches with fd

If you use fd without command line options, it behaves a bit like lsexcept that it lists files in subdirectories by default.

Write the following:

fdfind

The output appears in different colors for different types of files and directories.

To view files of a specific type, use the -e (extension) option. Note that you do not have to precede the extension with a period (.), Nor is it case sensitive.

For example, you can write the following:

fdfind -e png

Now the only files listed are PNG image files.

To search for a single file, type its name on the command line, like this:

fdfind index.page

The file is located and is in a subdirectory. We didn’t have to say fd to search recursively.

To start the search in a particular directory, include a file path on the command line. The following command will start a search in the “/ etc” directory and look for files that include “passwd” in the file name:

fdfind passwd /etc

Here, we look for all C source files that contain “coord” in the file name:

fdfind -e c coord

Two matching files were found.

fd and Git

Git is extremely popular source code version control system. If you use Git on your computer, you probably use “.gitignore” files to tell Git which files it should worry about and which ones it can ignore. Default, fd respect the settings of your “.gitignore” files.

In this directory, we have a Git repository and a “.gitignore” file. We write the following:

ls -adl .git*

Let’s ask fd to list any file that contains “coord” in the file name. Then we will repeat the search and use the -I (don’t ignore) option. This says fd to ignore the settings in the “.gitignore” file and report each matching file.

To do all this, we write the following:

fdfind coord
fdfind coord -I

The two additional files in the second result set are object files. These are created when an archive program is compiled. The linker then uses them to create the final executable version of the program.

Source code version control programs often ignore object files. They are regenerated every time you compile your program, so you don’t have to store copies of them. There is an entry in the “.gitignore” file telling Git to ignore object files and by default fd it also ignores them.

The -I (not to ignore) the forces of choice fd to return everything it finds, instead of going through the “.gitginore” file.

File types and case sensitivity

You can ask fd to search for directories, files (including executables and empty ones), and symbolic links. You can do it using the -t (type) option, followed by one of the following letters:

  • F: Proceedings.
  • D: Directory.
  • l: Symbolic link.
  • X: Executable file.
  • me: Empty file.

The following looks for a directory called images:

fdfind -td images

A match is found, one subdirectory lower than the current one.

Let’s see how case sensitivity works with search patterns. We write the following to find first the files that contain “geo” in their file names, and then those that contain “Geo” in their file names:

fdfind -tf geo
fdfind -tf Geo

In the first command, we used a lowercase search pattern, which caused fd to operate in a case-insensitive manner. This means that both “Geo” and “geo” are valid matches.

Our second command contained an uppercase character, which caused fd to operate in a case-sensitive manner. This means that only “Geo” is a valid match.

Command execution

The fd command allows you to launch another command and run it on each of the found files.

Let’s say we know that there is a Zip file somewhere in our source code directory tree. We can search for it using the following command, which searches for files with a ZIP extension:

fdfinf -e zip

With the -x (exec), you can pass each found file to another command for processing. For example, we can write the following to call the unzip utility to unzip our ZIP file (the “{}” is a placeholder representing the found file):

fdfind -e zip -x unzip {}

This will unzip the file into the current working directory. If we want it to be unzipped into the directory that contains the ZIP file, we can use one of the following placeholders:

  • {}: The full path of the file and the name of the file found.
  • {/}: The file name of the found file.
  • {//}: The directory that contains the found file.
  • {/.}: The filename of the found file, without the extension.

In order for our ZIP file to be found and unzipped in the directory that contains it, we can use the unzip command -d (directory) and pass the parent directory placeholder ({//}):

fdfind -e zip -x unzip {} -d {//}

The ZIP file is then located and unzipped into its main directory.

Your favorite search?

Because it covers the most common uses with such simplicity, fd it can easily become your “find” command. Whenever you need its more advanced features, you can always go back to that seasoned veteran, find.

Leave a Reply