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

mlocate

Most of the Linux distributions provide the mlocate package, which contains a locate command to find files, and a updatedb command to update the file index.

Can be installed directly through the system's software package management tool

# CentOS/RHEL
$ sudo dnf install mlocate

# Debian/Ubuntu
$ sudo apt install mlocate

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

sudo updatedb

The index file will be stored in /var/lib/mlocate/mlocatedb 161aae0cb6deed by . You can also modify the configuration file /etc/updatedb.conf file to add some folders that do not need to be indexed, for example

# 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"

After the index is completed, you can use the locate <pattern> command to find the file, for example

$ 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 matching, such as the difference between the results of the following two queries

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

Note the use of -b , you need to use before the search key from \ .

$ 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 search, you can view locate --help or man locate .

Next we will introduce another alternative to find-fd.

fd

fd is an David Peter . It is used to find files in the file system. In most cases, it can replace the find command.

fd can be used on multiple platforms, including most Linux distributions, MacOS, and Windows. For specific installation, please see https://github.com/sharkdp/fd#installation .

For example, you can use HomeBrew/LinuxBrew to install

$ brew install fd

After the installation is complete, you can use it directly, for example, find the file with the extension png

$ 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 of the fd command is the current directory. You can use --base-directory or --search-path to specify the search path. For example, we search for /etc/ and match the regular files of docker

$ 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 operation (similar to the --exec option of the find command), for example

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

This will find all PNG files in the current directory and convert them to JPG files. {} and {.} are used in the above command. Look at the following example to show the results represented by the placeholders

❯ 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

Through the placeholder, you can easily perform related operations on the file. For more fd command options, you can check fd --help .

If you want to use fd on Windows, you can Scoop package manager and open PowerShell

-> scoop install fd

Summarize

Here are two tools for quickly finding files in the file system through file name matching. Mlocate uses index files, so it is very efficient when searching for files globally. The fd tool provides many functions, which can be used instead of the find command in most scenarios, and its performance is higher than that of the find command. In addition, in addition to this tool, the developers of the fd tool have also developed other very useful tools, such as bat -a tool that can replace the cat command, and other support for many programming languages to output file content in the form of syntax highlighting. Also recommended.

Also posted on Mengz's Blog

梦哲
74 声望58 粉丝

寻找人生的意义!