fd – Simple and Fast alternative to the find command
fd is a command-line tool to find files on the filesystem. This tool is similar to the find command in Linux, but fd is more fast and simple to use. It is not powerful as the find command but provides the basic functionality of the find command in simple ways. Let’s see the features of fd tool.
Features
- Simple syntax i.e fd PATTERN
- Support use of Regular expression
- Uses different colors to highlight different file types
- By default, ignores hidden files and directories.
- Ignore patterns from your .gitignore,
- Fast output due to parallelized directory traversal.
Now let’s see the installation of fd tool
Installation
The fd tool is available on all package managers of Linux. Use one of the following commands according to your system to install fd tool:
For Debian/Ubuntu/Kali Linux:
sudo apt install fd-find
For Fedora:
sudo dnf install fd-find
For Alpine Linux
apk add fd
For Arch Linux:
pacman -S fd
For macOS with Homebrew:
brew install fd
For macOS with
sudo port install fd
For Windows with Scoop
scoop install fd
For windows with Chocolatey:
choco install fd
For FreeBSD
pkg install fd-find
To install fd using npm use the following command:
npm install -g fd-find
Usage
Now let’s see how to use the fd command.
Simple search with fd
The most basic file search with fd is to search a specific pattern with the fd command. To search pattern with fd command, just mention a pattern that should be part of the filename which we have to search. Here is one example:
find lvm
fd search for the provided pattern from the current directory recursively.
Regular expression search
Every quoted pattern is treated as a regular expression in the fd command. To find the files end with “se ” string use the following command
fd '.*s3$'
Specifying the root directory
If you want to search the file from the other directory as a source directory i.e the fd will start to search recursively from that mentioned folder. Here is one example:
fd profile /etc/
In the above command, we have provided the /etc/ as the root folder.
List all files recursively
To list all files recursively in the current directory, use the fd command.
fd
To list all files in the other directory, use a catch-all pattern such as . or ^ mention the directory path:
fd . /src/
Searching for a particular file extension
To search for the files having the specific file extensions use the -e option with fd and mention the file extension type.
find -e md
We can also use the -e option to find the specific pattern with the extension
fd -e js app
Searching for a particular file name
To search for exactly provided search pattern, use the -g or –glob option with fd command:
fd -g libc.so /usr
Hidden and ignored files
By default, the fd tool does not show the hidden files and directories. To search the pattern in hidden folders, use the –hidden options
fd -H pre-commit
If you are working with the git repository, then the fd will not folders that match in .gitingore files. To enable search in the folder and files in the .gitignore file, use the –no-ignore or -I option:
fd -I abbrev
Matching the full path
By default, the fd matched only the filenames with pattern to match the full path usings the -p or –full-path option:
Command execution
We can use the fd command to do more things other than just finding the pattern. We can also execute the commands with the fd command. There are two options provided by the fd to execute the command
- -x / –exec : This option executes the command on each result generated by the fd command.
- -X / –exec-batch: This option will execute one command only once.
Example with option -x:
To find and format the .cpp or .h files with the clang-format, we can use the following command:
fd -e h -e cpp -x clang-format -i
Example of option -X:
To open all files in the current directory in vim, use the following command:
fd -X vim
Placeholder in Fd command
Placeholders in fd commands can be used to more efficiently handle the command with the -x option. Following is the command that is used to *.jpg files into the *.png files.
fd -e jpg -x convert {} {.}.png
In the above command, {} is the placeholder which indicated search result and {.} this option is similar to the {} but {.} placeholder indicate search result without the file extension.
Following are the placeholders that can be used with -x or -X options:
- {}: This is a placeholder that will be replaced with the path of the search result(files/profile.jpg).
- {.}: This is a placeholder that will be replaced with the path of search result but without extension(files/profile)
- {/}: This is a placeholder that will be replaced with the basename of the file (profile.jpg)
- {//}: This is a placeholder that will be replaced with the parent discovered path(files/)
- {/.}: This is a placeholder that will be replaced with the filename without the extension(profile)
Excluding specific files or directories
If you want to exclude the come result from the fd command, then use the option -E with fd command. To search all hidden files and exclude the .git directories, you can use the following command:
fd -H -E .git
To exclude the mounted drives from the search, we can use the following command:
fd -E /mnt/external-drive …
To exclude files permanently create the .fdignore file in the home directory and put files to be ignored in that file
touch ~/.fdignore echo /mnt/external-drive >> ~/.fdignore
To exclude the files permanently with globally put the files to be ignored, put them into ~/.config/fd/ignore file.
Deleting files
To delete the files which have extension .cpp with the fd command in the current directory, we can use the following command
fd -e cpp -X rm
If you want to delete the files interactively then use the option -i with -X option
fd -e cpp -X rm -i
To know all options of fd command, use the –help option with fd command:
fd --help
To know more about the fd command, read the man page of fd .
man fd