用 Rust 制作的高度自以为是的简化查找命令。
默认情况下,它会搜索工作目录中的文件/文件夹,并将结果分为完全匹配的结果和仅包含查询的结果。
结果将按字母顺序排序。
例如, hunt SomeFile /
将从根目录搜索“SomeFile”,输出可能是:
Contains:
/SomeFileIsHere
/home/lyon/Downloads/abcdefgSomeFileeee
/mnt/Files/--SomeFile--
Exact:
/home/lyon/SomeFile
检查基准以与其他工具进行比较。
hunt [OPTIONS] [NAME] [SEARCH_IN_DIRS]...
默认情况下,搜索不区分大小写,除非[NAME]
包含大写字母或设置了--case-sensitive
标志。
-f, --first
Stop when first occurrence is found
-e, --exact
Only search for exactly matching occurrences, any file only containing the query will be skipped
e.g. if query is "SomeFile", "I'mSomeFile" will be skipped, as its name contains more letters than the search
-c, --canonicalize
If enabled, all paths will be canonicalized
-C, --case-sensitive
If enabled, the search will be case-sensitive
Note that case-sensitivity will be activated automatically when the search query contains an uppercase letter
-v, --verbose
Print verbose output
It'll show all errors found: e.g. "Could not read /proc/81261/map_files"
-s, --simple...
Prints without formatting (without "Contains:" and "Exact:")
-ss Output is not sorted
-H, --hidden
If enabled, it searches inside hidden directories
If not enabled, hidden directories will be skipped
--select
When the search is finished, choose one file between the results
The selected file will be printed as if -ss was used
--multiselect
When the search is finished, choose between the results
The selected files will be printed one after the other, separated by spaces
-S, --starts <STARTS_WITH>
Only files that start with this will be found
-E, --ends <ENDS_WITH>
Only files that end with this will be found
-t, --type <FILE_TYPE>
Specifies the type of the file
'f' -> file | 'd' -> directory
-i, --ignore <IGNORE_DIRS>
Ignores this directories. The format is:
-i dir1,dir2,dir3,...
-h, --help
Print help (see a summary with '-h')
-V, --version
Print version
如果设置了--first
标志,则搜索文件的顺序为[current_dir, home_dir, root]
。
如果您已经位于这些目录之一,则将跳过current_dir
。
如果未设置--hidden
标志,则将跳过隐藏文件/目录。
[NAME] Name of the file/folder to search
By default, searches are case-insensitive, unless the query contains an uppercase letter.
[SEARCH_IN_DIRS]...
Directories where you want to search
If provided, hunt will only search there
These directories are treated independently, so if one is nested into another the
search will be done two times:
e.g. "hunt somefile /home/user /home/user/downloads" will search in the home
directory, and because /home/user/downloads is inside it, /downloads will be
traversed two times
在整个系统上搜索特定文件(一旦找到就会停止搜索)
hunt -f -e SomeFile
搜索包含“SomeFile”的文件
hunt SomeFile
在主目录中搜索文件
hunt -e SomeFile ~/
在下载和图片目录中搜索文件
hunt -e SomeFile ~/downloads ~/pictures
搜索所有以“.exe”结尾的文件
hunt --ends .exe
搜索wine目录下所有以“.exe”结尾的文件
hunt --ends .exe ~/.wine
搜索所有以“.”开头的文件(所有隐藏文件)
hunt --starts .
搜索wine目录下所有以“.exe”结尾、以“M”开头且包含“wind”的文件
hunt --starts=M --ends=.exe wind ~/.wine
搜索名为“folder”的目录
hunt -t=d folder
搜索名为“notfolder”的文件
hunt -t=f notfolder
删除所有名为“SomeFile”的文件
hunt -s -e SomeFile | xargs rm -r
通常,当我搜索文件时,我不知道它所在的确切子目录,因此我最终会在整个 $HOME 目录中搜索。
使用find
命令最终会变得非常慢,因为遍历所有目录需要花费大量时间,而且输出也很难阅读。
locate
速度更快,但它并不总能找到我正在寻找的文件,因为它只在其数据库中搜索,而数据库并未实时更新。
看到find
根本不执行任何并行性,我决定制作它的多线程版本,这就是 Hunt 的诞生。
Hunt 是多线程的,因此它比find
快得多,并且比locate
更可靠(无法使用它找到最近的文件)。
从版本下载最新的二进制文件。
或者使用cargo-binstall
安装:
cargo binstall hunt
首先检查是否安装了 Rust,然后运行
cargo install hunt
让我们将 Hunt 与一些最常用的工具进行比较:GNU定位和查找以及非常流行的也是用 rust 编写的fd 。
对于基准测试,我使用 hyperfine,这是 fd dev 开发的工具。
这些工作是在一个包含大约 2,762,223 个文件、带有网络驱动器和外部驱动器的系统中完成的。
其他系统上的结果可能会有所不同,因此请将此比较作为指导。
如果您想重现基准测试,可以通过运行此存储库中的benchmarks.sh
文件来实现。
在主目录的隐藏文件夹中查找第一次出现的严重嵌套文件。文件位于/home/user/.wine/drive_c/users/user/AppData/Local/mygame/User Data/Crashpad/reports/SomeFile
。
Benchmark 1: hunt --hidden --first --exact SomeFile ~/
Time (mean ± σ): 180.2 ms ± 7.4 ms [User: 406.6 ms, System: 1135.9 ms]
Range (min … max): 167.2 ms … 198.5 ms 16 runs
Benchmark 2: fd --hidden --no-ignore --glob --color=never --max-results=1 SomeFile ~/
Time (mean ± σ): 913.6 ms ± 52.5 ms [User: 2584.8 ms, System: 4628.6 ms]
Range (min … max): 858.6 ms … 1018.6 ms 10 runs
Benchmark 3: find ~/ -name SomeFile -print -quit 2>/dev/null
Time (mean ± σ): 2.219 s ± 0.071 s [User: 0.587 s, System: 0.988 s]
Range (min … max): 2.160 s … 2.395 s 10 runs
Benchmark 4: locate -n 1 -A SomeFile
Time (mean ± σ): 1.244 s ± 0.015 s [User: 1.231 s, System: 0.010 s]
Range (min … max): 1.231 s … 1.281 s 10 runs
Summary
'hunt --hidden --first --exact SomeFile ~/' ran
5.07 ± 0.36 times faster than 'fd --hidden --no-ignore --glob --color=never --max-results=1 SomeFile ~/'
6.90 ± 0.30 times faster than 'locate -n 1 -A SomeFile'
12.31 ± 0.64 times faster than 'find ~/ -name SomeFile -print -quit 2>/dev/null'
--hidden,搜索所有文件(通常会忽略忽略列表中的隐藏文件和目录)。
--first,在找到第一次出现时停止。
--exact,仅搜索名为“SomeFile”的文件/文件夹,仅包含该模式的名称将被跳过。
从根目录查找所有出现的“SomeFile”(最坏的情况,检查系统中的所有文件)。
有问题的事件是:
/home/lyon/Downloads/abcdefgSomeFileeee
/SomeFileIsHere
/mnt/Files/--SomeFile--
/home/lyon/.wine/drive_c/Program Files (x86)/Internet Explorer/SomeFile
对于此基准测试,我将跳过“定位”。它显然更快,因为它没有遍历所有文件系统,因为它是由数据库备份的。
但必须注意的是,/mnt/Files 中的文件未找到,因为数据库不保留其他驱动器中的文件记录。
令人好奇的是,它的时间为 486.8 毫秒,仅比 Hunt 快 1.32 倍。
Benchmark 1: hunt -H SomeFile /
Time (mean ± σ): 633.6 ms ± 25.1 ms [User: 2876.7 ms, System: 2507.5 ms]
Range (min … max): 589.4 ms … 671.2 ms 10 runs
Benchmark 2: fd -HI -c never SomeFile /
Time (mean ± σ): 1.452 s ± 0.014 s [User: 4.116 s, System: 8.693 s]
Range (min … max): 1.431 s … 1.474 s 10 runs
Benchmark 3: find / -name "*SomeFile*"
Time (mean ± σ): 3.473 s ± 0.144 s [User: 1.234 s, System: 1.602 s]
Range (min … max): 3.374 s … 3.874 s 10 runs
'hunt -H SomeFile /' ran
2.29 ± 0.09 times faster than 'fd -HI -c never SomeFile /'
5.48 ± 0.31 times faster than 'find / -name "*SomeFile*"'
如果您不需要很多功能(例如正则表达式),Hunt 比其他替代方案更快。
将其视为一个简单的“我将该文件放在哪里?”解决方案。