Essential Linux Utility: Advanced Alternatives to the Old Find Command

Essential Linux Utility: Advanced Alternatives to the Old Find CommandEssential Linux Utility: Advanced Alternatives to the Old Find Command

Click “Read the original text” to view Liangxu’s original video.

On Linux, fd is a simpler alternative to the find command. It has a simplified syntax and reasonable defaults, so let’s take a look.
What is the difference between fd and find?
The fd command is not meant to replace the traditional find command, which has been around on Linux for some time. Instead, fd aims to satisfy most common use cases of find in a more straightforward way, and it is often eight to nine times faster than find. You can see some benchmarks on the project’s GitHub repository page.
fd has colored output similar to some ls modes. It is recursive, but does not search hidden directories by default. It understands Git and will automatically ignore any patterns in the .gitignore file.
By default, fd is case-insensitive. However, if your search pattern contains an uppercase letter, fd will run in case-sensitive mode. Of course, you can override the defaults, but in many cases, they are beneficial to you.
Installing fd
Starting from Ubuntu 19.04 (Disco Dingo), you can install fd directly by using apt-get to call the officially maintained package. If you are running an older version of Ubuntu, please check the installation instructions on the GitHub page.
Enter the following command:
linuxmi@linuxmi:~/www.linuxmi.com$ sudo apt-get install fd-find
Essential Linux Utility: Advanced Alternatives to the Old Find Command
In Ubuntu, the command is fdfind to avoid name conflicts with another existing utility. If you want it to be fd, you can set an alias:
linuxmi@linuxmi:~/www.linuxmi.com$ alias fd=fdfind
Essential Linux Utility: Advanced Alternatives to the Old Find Command
To keep the alias persistent so that it remains available after a reboot, place it in the .bashrc or .bash_aliases file.
To install fd on Fedora, type the following command:
sudo dnf install fd-find
On Manjaro, type the following:
sudo pacman -Syu fd
fd vs fdfind
To avoid confusion, we kept the default name of the command fdfind on our Ubuntu test PC. fd and fdfind are exactly the same command, as shown in the example below (if you ask fdfind to show its version, it refers to itself as “fd”):
linuxmi@linuxmi:~/www.linuxmi.com$ fdfind –version
Essential Linux Utility: Advanced Alternatives to the Old Find Command
In the example, we will use Ubuntu’s “fdfind”. In other Linux distributions, you can type “fd” instead of “fdfind” to save some keystrokes.
Simple Search with fd
If you use fd without command-line options, it behaves like ls, except that it lists files in subdirectories by default.
Enter the following:
linuxmi@linuxmi:~/www.linuxmi.com$ fdfind
Essential Linux Utility: Advanced Alternatives to the Old Find Command
The output is displayed in different colors for different file types and directories.
To view specific types of files, use the -e (extension) option. Note that you do not need to prepend a period (.) to the extension, and it is case-insensitive.
For example, you can type:
linuxmi@linuxmi:~/www.linuxmi.com$ fdfind -e png
Essential Linux Utility: Advanced Alternatives to the Old Find Command
Now, the only files listed are PNG image files.
To find a single file, type its name on the command line as follows:
linuxmi@linuxmi:~/www.linuxmi.com$ fdfind linuxmi.png
Essential Linux Utility: Advanced Alternatives to the Old Find Command
The file was found, and it happens to be in a subdirectory. We did not have to tell fd to search recursively.
To start searching in a specific directory, include the file path on the command line. The following command will start searching in the /etc directory and look for files with “passwd” in their names:
linuxmi@linuxmi:~/www.linuxmi.com$ fdfind passwd /etc
Essential Linux Utility: Advanced Alternatives to the Old Find Command
Here, we search for files with “linuxmi” in their names among all C source code files:
linuxmi@linuxmi:~/www.linuxmi.com$ fdfind -e c linuxmi
Found 3 matching files.
Essential Linux Utility: Advanced Alternatives to the Old Find Command
File Types and Case Sensitivity
You can ask fd to find directories, files (including executable files and empty files), and symbolic links. You can use the -t (type) option followed by one of the following letters:
  • f: file

  • d: directory

  • l: symbolic link

  • x: executable file

  • e: empty file

Let’s look for a directory named images:

linuxmi@linuxmi:~/www.linuxmi.com$ fdfind -td images

Essential Linux Utility: Advanced Alternatives to the Old Find Command

Found a match in a subdirectory under the current directory.

Let’s see how case sensitivity works with search patterns. We type the following to search for files with “linux” in their names, and then search for files with “Linux” in their names:

linuxmi@linuxmi:~/www.linuxmi.com$ fdfind -tf linux

Essential Linux Utility: Advanced Alternatives to the Old Find Command

linuxmi@linuxmi:~/www.linuxmi.com$ fdfind -tf Linux

Essential Linux Utility: Advanced Alternatives to the Old Find Command

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

In the second command, the pattern contains an uppercase character, which causes fd to operate in a case-sensitive manner. This means only “Linux” is a valid match.

Command Execution

The fd command allows you to start another command and execute it on each file found.

Assuming we know there is a Zip file somewhere in the source code directory tree. We can use the following command to find it, which searches for files with a ZIP extension:

linuxmi@linuxmi:~/linuxmi.com$ fdfind -e zip

Essential Linux Utility: Advanced Alternatives to the Old Find Command

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

linuxmi@linuxmi:~/linuxmi.com$ fdfind -e zip -x unzip {}

This will extract files located in the current working directory. If we want to extract it to the directory containing the ZIP file, we can use one of the following placeholders:

  • {} – a placeholder that will change with the path of the search result (wp-content/uploads/01.jpg).

  • {.} – similar to {}, but without the file extension (wp-content/uploads/01).

  • {/}: a placeholder that will be replaced by the base name of the search result (01.jpg).

  • {//}: the parent directory of the found path (wp-content/uploading).

  • {/.}: only the base name without the extension (01).

To find our ZIP file and extract it into the directory containing it, we can use unzip -d (directory) option and pass the parent directory placeholder ({//}):

linuxmi@linuxmi:~/linuxmi.com$ fdfind -e zip -x unzip {} -d {//}

Essential Linux Utility: Advanced Alternatives to the Old Find Command

Then the ZIP file will be located and extracted into its parent directory.

Essential Linux Utility: Advanced Alternatives to the Old Find Command

Summary

This is a brief overview of the fd command, and some users may find the fd command easier to use and faster. As mentioned earlier in this article, fd is not a complete replacement for find, but it offers simpler usage, easier searching, and better performance. In your toolkit, fd takes up little space and is a great tool.

Liangxu’s Personal WeChat

Add Liangxu’s personal WeChat to receive 3 sets of essential reading materials for programmers

→ Selected technical materials sharing

→ Expert communication community

Essential Linux Utility: Advanced Alternatives to the Old Find Command

All articles of this public account have been organized into a directory. Please reply “m” in the public account to get it!

Recommended reading:

“Top 10 Most Popular Boyfriend Professions”

Office slacking? Say it earlier, I’m good at this…

13 Practical Tools for Linux

5T technical resources are being released! Including but not limited to: C/C++, Linux, Python, Java, PHP, Artificial Intelligence, Single Chip Microcomputer, Raspberry Pi, etc. Reply “1024” in the public account to get it for free!!

Essential Linux Utility: Advanced Alternatives to the Old Find Command

Leave a Comment

×