Two tools for finding files in the file system are recommended

As we all know, in Linux or Unix like file systems, if you want to find a file through the file name keyword, you can use the find Command. Then this article will recommend two tools that can quickly find files, which have 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 The command is used to find files, and a updatedb The command updates the file index for use by locate.

It can be installed directly through the software package management tool of the system

# CentOS/RHEL
$ sudo dnf install mlocate

# Debian/Ubuntu
$ sudo apt install mlocate

After installation, first execute the following commands to index the files

sudo updatedb

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

# 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 indexing, you can use the locate < pattern > command to find files, 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 that when using - b, you need to use \ beforethe key of search.

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

Next, we will introduce another find alternative Ping - fd.

fd

fd It's a product made by David Peter The open source tool developed 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. The specific installation can be viewed https://github.com/sharkdp/fd... .

For example, you can use HomeBrew/LinuxBrew for installation

$ brew install fd

After installation, you can use it directly, for example, to find files with png extension in the current folder

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

$ 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), such as

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

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

❯ 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 placeholders, you can easily operate on files. For more fd command options, you can view fd --help.

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

-> scoop install fd

summary

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

Also published in Mengz's Blog

Tags: Linux find

Posted on Fri, 03 Dec 2021 09:24:41 -0500 by mrdance