用 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 比其他替代方案更快。
將其視為一個簡單的“我將該文件放在哪裡?”解決方案。