If you’re like most Linux users, you probably know how to use the command line. But did you know that you can use the fd command to access files and directories on your computer? Here’s how: To open a file using the fd command, type its name followed by a colon (:). For example, if you want to open the file myfile.txt in your current directory, you would type: fd myfile.txt: To open a directory using the fd command, type its name followed by a forward slash (/). For example, if you want to open the directory my Documents in your current directory, you would type: fd /my Documents: ..
fd versus find: What’s the Difference?
The fd command isn’t meant to replace the traditional find command, which has been on Linux, well, forever. Instead, fd tries to satisfy the majority of common uses of find in a more straightforward way—and, it’s often eight or nine times faster than find. You can see some of its benchmarks on the project’s GitHub repository page.
fd has a colorized output, similar to that of some ls modes. It’s recursive, but doesn’t search hidden directories by default. It knows about Git and will also automatically ignore any patterns in your “.gitignore” file.
fd is case insensitive by default. However, if your search pattern contains an uppercase letter, fd operates in a case sensitive mode. Of course, you can override the defaults, but, in many cases, they work in your favor.
RELATED: How to Use All Linux’s Search Commands
Installing fd
Since Ubuntu 19.04 (Disco Dingo,) you can install fd directly by calling the officially maintained package with apt-get. If you’re running an older version of Ubuntu, check the installation instructions on the Git hub page.
Type the following:
In Ubuntu, the command is fdfind to avoid a name clash with another existing utility. If you want it to be fd, you can set up an alias:
To make the alias persistent so it will remain available after reboots, put it in your “.bashrc” or “.bash_aliases” file.
RELATED: How to Create Aliases and Shell Functions on Linux
To install fd on Fedora, type this command:
On Manjaro, type the following:
fd versus fdfind
To avoid confusion, we’ve left the command with its default name, fdfind, on our Ubuntu test PC. fd and fdfind are exactly the same command, as you’ll see in the following example (if you ask fdfind to show its version, it calls itself “fd”):
We’ll call the command “fed,” but in the examples, we’ll use the Ubuntu “fdfind.” On other Linux distributions, you can type “fd” instead of “fdfind” to save a few keystrokes.
Simple Searches with fd
If you use fd with no command-line options, it behaves a little like ls, except it lists files in subdirectories by default.
Type the following:
The output appears in different colors for different file types and directories.
To see files of a specific type, use the -e (extension) option. Note that you don’t have to precede the extension with a period (.), nor is it case sensitive.
For example, you could type the following:
Now, the only files listed are PNG image files.
To look for a single file, type its name on the command line, like so:
The file is found and happens to be in a subdirectory. We didn’t have to tell fd to search recursively.
To have the search start 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:
Here, we’re searching for all C source code files that contain “coord” in the file name:
Two matching files were found.
fd and Git
Git is an 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 concern itself with, and which it can ignore. By default, fd respects the settings in your “.gitignore” files.
In this directory, we’ve got a Git repository and “.gitignore” file. We type the following:
Let’s ask fd to list any files that contain “coord” in the file name. We’ll then repeat the search and use the -I (no ignore) option. This tells fd to ignore the settings in the “.gitignore” file and report every matching file.
To do all of this, we type the following:
The two extra files in the second set of results are object files. These are created when a file program is compiled. They’re then used by the linker to create the final executable version of the program.
Object files are typically ignored by source code version control programs. They’re regenerated every time you compile your program, so you don’t have to store copies of them. There’s an entry in the “.gitignore” file that instructs Git to ignore object files, and, by default, fd ignores them, too.
The -I (no ignore) option forces fd to return everything it finds, rather than being guided by the “.gitginore” file.
File Types and Case Sensitivity
You can ask fd to look for directories, files (including those that are executable and empty), and symbolic links. You can do so by using the -t (type) option, followed by one of the letters below:
f: File. d: Directory. l: Symbolic link. x: Executable file. e: Empty file.
The following looks for a directory called images:
A match is found, one subdirectory lower than the current one.
Let’s see how case sensitivity works with search patterns. We type the following to first search for files that contain “geo” in their file names, and then for those that contain “Geo” in their file names:
In the first command, we used a lowercase search pattern, which caused fd to operate in a case-insensitive way. This means 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 only “Geo” is a valid match.
Command Execution
The fd command allows you to launch another command and execute it on each of the found files.
Let’s say we know there’s a Zip file somewhere in our source code directory tree. We can look for it using the following command, which searches for files with the ZIP extension:
With the -x (exec) option, you can pass each found file to another command to be processed by it. For example, we can type the following to call the unzip utility to unzip our ZIP file (the “{}” is a placeholder representing the found file):
This will unzip the file in the current working directory. If we want it to be unzipped in the directory containing the ZIP file, we can use one of the following placeholders:
{}: The full file path and name of the found file. {/}: The file name of the found file. {//}: The directory containing the found file. {/. }: The file name of the found file, without the extension.
For our ZIP file to be found and unzipped in the directory that contains it, we can use the unzip -d (directory) option, and pass in the parent directory placeholder ({//}):
The ZIP file is then located and the unzipped in its parent directory.
Your Go-to Find?
Because it covers the most common uses with such simplicity, fd can easily become your go-to “find” command. Whenever you need its more advanced features, you can always return to that seasoned veteran, find.