As you know, in Linux or Unix-like file systems, you can use the find command if you want to find a file by the filename keyword. Then this article will recommend 2 tools that can quickly find files with better performance than the find command and can replace the use of find in some scenarios.

mlocate

Most Linux distributions provide the mlocate package, which includes a locate command for finding files and an updatedb command to update the file index for locate.

It can be installed directly through the system’s package management tool.

1
2
3
4
5
# CentOS/RHEL
$ sudo dnf install mlocate

# Debian/Ubuntu
$ sudo apt install mlocate

After the installation is complete, you first need to execute the following command for file indexing.

1
sudo updatedb

The index files will be stored in /var/lib/mlocate/mlocatedb by default, or you can modify the configuration file /etc/updatedb.conf to add certain folders that do not require indexing, for example.

1
2
# Paths which are pruned from updatedb database
PRUNEPATHS="/tmp /var/tmp /var/cache /var/lock /var/run /var/spool /mnt /cdrom /usr/tmp /proc /media /sys /.snapshots /var/run/media"

Once the indexing is complete, you can use the locate command to find files, for example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
$ locate mlocate
/etc/systemd/system/timers.target.wants/mlocate.timer
/usr/bin/rpmlocate
/usr/lib/systemd/system/mlocate.service
/usr/lib/systemd/system/mlocate.timer
/usr/sbin/rcmlocate
/usr/share/doc/packages/mlocate
/usr/share/doc/packages/mlocate/AUTHORS
/usr/share/doc/packages/mlocate/ChangeLog
/usr/share/doc/packages/mlocate/NEWS
/usr/share/doc/packages/mlocate/README
/usr/share/licenses/mlocate
/usr/share/licenses/mlocate/COPYING
/usr/share/man/man5/mlocate.db.5.gz
/var/lib/mlocate
/var/lib/mlocate/mlocate.db
/var/lib/mlocate/mlocate.db.9O5YsQ
/var/lib/systemd/migrated/mlocate
/var/lib/systemd/timers/stamp-mlocate.timer

You can use the -b option for exact matches, such as the difference between the results of the two queries below.

1
2
$ locate -b '\updatedb'
/usr/bin/updatedb

Note that when you use -b, you need to use \ before the keyword you are searching for.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ locate 'updatedb'
/etc/updatedb.conf
/etc/apparmor.d/usr.bin.updatedb
/usr/bin/updatedb
/usr/share/augeas/lenses/dist/updatedb.aug
/usr/share/man/man5/updatedb.conf.5.gz
/usr/share/man/man8/updatedb.8.gz
/usr/share/nvim/runtime/ftplugin/updatedb.vim
/usr/share/nvim/runtime/syntax/updatedb.vim
/usr/share/vim/vim80/ftplugin/updatedb.vim
/usr/share/vim/vim80/syntax/updatedb.vim

You can also use -r for basic regular expression pattern matching lookups, see locate --help or man locate.

Next we will introduce another alternative to find - fd.

fd

fd is an open source tool developed by David Peter to find files in the filesystem, replacing the find command in most cases.

fd can be used on multiple platforms, including most Linux distributions, MacOS, Windows, see the documentation for installation at https://github.com/sharkdp/fd#installation.

For example, you can use HomeBrew/LinuxBrew for installation

1
$ brew install fd

Once installed, you can use it directly, for example to find files with the extension png in the current folder.

1
2
3
4
5
$ fd -e png
go/src/github.com/Go-zh/tour/static/img/gopher.png
go/src/github.com/Go-zh/tour/content/img/tree.png
go/src/github.com/containous/yaegi/doc/images/yaegi.png
...

Note that the default search path for the fd command is the current directory, you can use -base-directory or -search-path to specify the search path, for example we look under /etc/ for regular files matching docker.

1
2
3
4
$ fd --base-directory /etc/ -t f 'docker'
audit/rules.d/docker.rules
bash_completion.d/docker-compose.bash
sysconfig/docker

You can also use the -x option to output the results to other commands for manipulation (similar to the -exec option of the find command), for example.

1
$ fd -d 1 -e png -x convert {} {.}.jpg

This will find all PNG files in the current directory and convert them to JPG files. The above command uses the placeholders {} and {.}, see the following example to show the result of the placeholders.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
❯ fd 'recognition.db' -x echo {}
Pictures/recognition.db
Pictures/Photos/recognition.db

❯ fd 'recognition.db' -x echo {.}
Pictures/Photos/recognition
Pictures/recognition

❯ fd 'recognition.db' -x echo {/}
recognition.db
recognition.db

❯ fd 'recognition.db' -x echo {//}
Pictures
Pictures/Photos

❯ fd 'recognition.db' -x echo {/.}
recognition
recognition

The placeholders make it easy to perform related operations on files. For more fd command options, check out fd --help.

If you want to use fd on Windows, you can install it through the Scoop package manager and open PowerShell.

1
-> scoop install fd

Summary

Here are two tools for finding files quickly by filename matching in the file system. mlocate uses indexed files, so it is very efficient in finding files globally. And the fd tool provides a lot of features that can be used instead of the find command in most scenarios, and the performance is higher than the find command. In addition, the developers of the fd tool have developed other useful tools in addition to this one, such as bat - a tool that can be used instead of the cat command, and others that support syntax highlighting in many programming languages to output file content, which is also recommended.