├── .gitignore
├── doc
└── screencast.svg
├── en.md
└── readme.md
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 | .DS_Store
3 | fork
4 | source
--------------------------------------------------------------------------------
/doc/screencast.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/en.md:
--------------------------------------------------------------------------------
1 | # fd
2 |
3 | [](https://travis-ci.org/sharkdp/fd)
4 | [](https://ci.appveyor.com/project/sharkdp/fd)
5 | [](https://crates.io/crates/fd-find)
6 |
7 | _fd_ is a simple, fast and user-friendly alternative to
8 | [_find_](https://www.gnu.org/software/findutils/).
9 |
10 | While it does not seek to mirror all of _find_'s powerful functionality, it provides sensible
11 | (opinionated) defaults for [80%](https://en.wikipedia.org/wiki/Pareto_principle) of the use cases.
12 |
13 | ## Features
14 |
15 | - Convenient syntax: `fd PATTERN` instead of `find -iname '*PATTERN*'`.
16 | - Colorized terminal output (similar to _ls_).
17 | - It's _fast_ (see [benchmarks](#benchmark) below).
18 | - Smart case: the search is case-insensitive by default. It switches to
19 | case-sensitive if the pattern contains an uppercase
20 | character[\*](http://vimdoc.sourceforge.net/htmldoc/options.html#'smartcase').
21 | - Ignores hidden directories and files, by default.
22 | - Ignores patterns from your `.gitignore`, by default.
23 | - Regular expressions.
24 | - Unicode-awareness.
25 | - The command name is _50%_ shorter[\*](https://github.com/ggreer/the_silver_searcher) than
26 | `find` :-).
27 | - Parallel command execution with a syntax similar to GNU Parallel.
28 |
29 | ## Demo
30 |
31 | 
32 |
33 | ## Benchmark
34 |
35 | Let's search my home folder for files that end in `[0-9].jpg`. It contains ~190.000
36 | subdirectories and about a million files. For averaging and statistical analysis, I'm using
37 | [hyperfine](https://github.com/sharkdp/hyperfine). The following benchmarks are performed
38 | with a "warm"/pre-filled disk-cache (results for a "cold" disk-cache show the same trends).
39 |
40 | Let's start with `find`:
41 |
42 | ```
43 | Benchmark #1: find ~ -iregex '.*[0-9]\.jpg$'
44 |
45 | Time (mean ± σ): 7.236 s ± 0.090 s
46 |
47 | Range (min … max): 7.133 s … 7.385 s
48 | ```
49 |
50 | `find` is much faster if it does not need to perform a regular-expression search:
51 |
52 | ```
53 | Benchmark #2: find ~ -iname '*[0-9].jpg'
54 |
55 | Time (mean ± σ): 3.914 s ± 0.027 s
56 |
57 | Range (min … max): 3.876 s … 3.964 s
58 | ```
59 |
60 | Now let's try the same for `fd`. Note that `fd` _always_ performs a regular expression
61 | search. The options `--hidden` and `--no-ignore` are needed for a fair comparison,
62 | otherwise `fd` does not have to traverse hidden folders and ignored paths (see below):
63 |
64 | ```
65 | Benchmark #3: fd -HI '.*[0-9]\.jpg$' ~
66 |
67 | Time (mean ± σ): 811.6 ms ± 26.9 ms
68 |
69 | Range (min … max): 786.0 ms … 870.7 ms
70 | ```
71 |
72 | For this particular example, `fd` is approximately nine times faster than `find -iregex`
73 | and about five times faster than `find -iname`. By the way, both tools found the exact
74 | same 20880 files :smile:.
75 |
76 | Finally, let's run `fd` without `--hidden` and `--no-ignore` (this can lead to different
77 | search results, of course). If _fd_ does not have to traverse the hidden and git-ignored
78 | folders, it is almost an order of magnitude faster:
79 |
80 | ```
81 | Benchmark #4: fd '[0-9]\.jpg$' ~
82 |
83 | Time (mean ± σ): 123.7 ms ± 6.0 ms
84 |
85 | Range (min … max): 118.8 ms … 140.0 ms
86 | ```
87 |
88 | **Note**: This is _one particular_ benchmark on _one particular_ machine. While I have
89 | performed quite a lot of different tests (and found consistent results), things might
90 | be different for you! I encourage everyone to try it out on their own. See
91 | [this repository](https://github.com/sharkdp/fd-benchmarks) for all necessary scripts.
92 |
93 | Concerning _fd_'s speed, the main credit goes to the `regex` and `ignore` crates that are also used
94 | in [ripgrep](https://github.com/BurntSushi/ripgrep) (check it out!).
95 |
96 | ## Colorized output
97 |
98 | `fd` can colorize files by extension, just like `ls`. In order for this to work, the environment
99 | variable [`LS_COLORS`](https://linux.die.net/man/5/dir_colors) has to be set. Typically, the value
100 | of this variable is set by the `dircolors` command which provides a convenient configuration format
101 | to define colors for different file formats.
102 | On most distributions, `LS_COLORS` should be set already. If you are looking for alternative, more
103 | complete (and more colorful) variants, see
104 | [here](https://github.com/seebi/dircolors-solarized) or
105 | [here](https://github.com/trapd00r/LS_COLORS).
106 |
107 | ## Parallel command execution
108 |
109 | If the `-x`/`--exec` option is specified alongside a command template, a job pool will be created
110 | for executing commands in parallel for each discovered path as the input. The syntax for generating
111 | commands is similar to that of GNU Parallel:
112 |
113 | - `{}`: A placeholder token that will be replaced with the path of the search result
114 | (`documents/images/party.jpg`).
115 | - `{.}`: Like `{}`, but without the file extension (`documents/images/party`).
116 | - `{/}`: A placeholder that will be replaced by the basename of the search result (`party.jpg`).
117 | - `{//}`: Uses the parent of the discovered path (`documents/images`).
118 | - `{/.}`: Uses the basename, with the extension removed (`party`).
119 |
120 | ```bash
121 | # Convert all jpg files to png files:
122 | fd -e jpg -x convert {} {.}.png
123 |
124 | # Unpack all zip files (if no placeholder is given, the path is appended):
125 | fd -e zip -x unzip
126 |
127 | # Convert all flac files into opus files:
128 | fd -e flac -x ffmpeg -i {} -c:a libopus {.}.opus
129 |
130 | # Count the number of lines in Rust files (the command template can be terminated with ';'):
131 | fd -x wc -l \; -e rs
132 | ```
133 |
134 | ## Installation
135 |
136 | ### On Ubuntu
137 |
138 | _... and other Debian-based Linux distributions._
139 |
140 | Download the latest `.deb` package from the [release page](https://github.com/sharkdp/fd/releases) and install it via:
141 |
142 | ```bash
143 | sudo dpkg -i fd_7.0.0_amd64.deb # adapt version number and architecture
144 | ```
145 |
146 | ### On Fedora
147 |
148 | Starting with Fedora 28, you can install `fd` from the official package sources:
149 |
150 | ```bash
151 | dnf install fd-find
152 | ```
153 |
154 | For older versions, you can use this [Fedora copr](https://copr.fedorainfracloud.org/coprs/keefle/fd/) to install `fd`:
155 |
156 | ```bash
157 | dnf copr enable keefle/fd
158 | dnf install fd
159 | ```
160 |
161 | ### On Arch Linux
162 |
163 | You can install [the fd package](https://www.archlinux.org/packages/community/x86_64/fd/) from the official repos:
164 |
165 | ```
166 | pacman -S fd
167 | ```
168 |
169 | ### On Gentoo Linux
170 |
171 | You can use [the fd ebuild](https://packages.gentoo.org/packages/sys-apps/fd) from the official repo:
172 |
173 | ```
174 | emerge -av fd
175 | ```
176 |
177 | ### On openSUSE Linux
178 |
179 | You can install [the fd package](https://software.opensuse.org/package/fd) from the official repo:
180 |
181 | ```
182 | zypper in fd
183 | ```
184 |
185 | ### On Void Linux
186 |
187 | You can install `fd` via xbps-install:
188 |
189 | ```
190 | xbps-install -S fd
191 | ```
192 |
193 | ### On macOS
194 |
195 | You can install `fd` with [Homebrew](http://braumeister.org/formula/fd):
196 |
197 | ```
198 | brew install fd
199 | ```
200 |
201 | … or with MacPorts:
202 |
203 | ```
204 | sudo port install fd
205 | ```
206 |
207 | ### On Windows
208 |
209 | You can download pre-built binaries from the [release page](https://github.com/sharkdp/fd/releases).
210 |
211 | Alternatively, you can install `fd` via [Scoop](http://scoop.sh):
212 |
213 | ```
214 | scoop install fd
215 | ```
216 |
217 | Or via [Chocolatey](https://chocolatey.org):
218 |
219 | ```
220 | choco install fd
221 | ```
222 |
223 | ### On NixOS / via Nix
224 |
225 | You can use the [Nix package manager](https://nixos.org/nix/) to install `fd`:
226 |
227 | ```
228 | nix-env -i fd
229 | ```
230 |
231 | ### On FreeBSD
232 |
233 | You can install `sysutils/fd` via portmaster:
234 |
235 | ```
236 | portmaster sysutils/fd
237 | ```
238 |
239 | ### From source
240 |
241 | With Rust's package manager [cargo](https://github.com/rust-lang/cargo), you can install _fd_ via:
242 |
243 | ```
244 | cargo install fd-find
245 | ```
246 |
247 | Note that rust version _1.20.0_ or later is required.
248 |
249 | ### From binaries
250 |
251 | The [release page](https://github.com/sharkdp/fd/releases) includes precompiled binaries for Linux, macOS and Windows.
252 |
253 | ## Development
254 |
255 | ```bash
256 | git clone https://github.com/sharkdp/fd
257 |
258 | # Build
259 | cd fd
260 | cargo build
261 |
262 | # Run unit tests and integration tests
263 | cargo test
264 |
265 | # Install
266 | cargo install
267 | ```
268 |
269 | ## Command-line options
270 |
271 | ```
272 | USAGE:
273 | fd [FLAGS/OPTIONS] [] [...]
274 |
275 | FLAGS:
276 | -H, --hidden Search hidden files and directories
277 | -I, --no-ignore Do not respect .(git|fd)ignore files
278 | --no-ignore-vcs Do not respect .gitignore files
279 | -s, --case-sensitive Case-sensitive search (default: smart case)
280 | -i, --ignore-case Case-insensitive search (default: smart case)
281 | -F, --fixed-strings Treat the pattern as a literal string
282 | -a, --absolute-path Show absolute instead of relative paths
283 | -L, --follow Follow symbolic links
284 | -p, --full-path Search full path (default: file-/dirname only)
285 | -0, --print0 Separate results by the null character
286 | -h, --help Prints help information
287 | -V, --version Prints version information
288 |
289 | OPTIONS:
290 | -d, --max-depth Set maximum search depth (default: none)
291 | -t, --type ... Filter by type: file (f), directory (d), symlink (l),
292 | executable (x), empty (e)
293 | -e, --extension ... Filter by file extension
294 | -x, --exec Execute a command for each search result
295 | -E, --exclude ... Exclude entries that match the given glob pattern
296 | --ignore-file ... Add a custom ignore-file in .gitignore format
297 | -c, --color When to use colors: never, *auto*, always
298 | -j, --threads Set number of threads to use for searching & executing
299 | -S, --size ... Limit results based on the size of files.
300 |
301 | ARGS:
302 | the search pattern, a regular expression (optional)
303 | ... the root directory for the filesystem search (optional)
304 | ```
305 |
306 | ## Tutorial
307 |
308 | First, to get an overview of all available command line options, you can either run
309 | `fd -h` for a concise help message (see above) or `fd --help` for a more detailed
310 | version.
311 |
312 | ### Simple search
313 |
314 | _fd_ is designed to find entries in your filesystem. The most basic search you can perform is to
315 | run _fd_ with a single argument: the search pattern. For example, assume that you want to find an
316 | old script of yours (the name included `netflix`):
317 |
318 | ```bash
319 | > fd netfl
320 | Software/python/imdb-ratings/netflix-details.py
321 | ```
322 |
323 | If called with just a single argument like this, _fd_ searches the current directory recursively
324 | for any entries that _contain_ the pattern `netfl`.
325 |
326 | ### Regular expression search
327 |
328 | The search pattern is treated as a regular expression. Here, we search for entries that start
329 | with `x` and end with `rc`:
330 |
331 | ```bash
332 | > cd /etc
333 | > fd '^x.*rc$'
334 | X11/xinit/xinitrc
335 | X11/xinit/xserverrc
336 | ```
337 |
338 | ### Specifying the root directory
339 |
340 | If we want to search a specific directory, it can be given as a second argument to _fd_:
341 |
342 | ```bash
343 | > fd passwd /etc
344 | /etc/default/passwd
345 | /etc/pam.d/passwd
346 | /etc/passwd
347 | ```
348 |
349 | ### Running _fd_ without any arguments
350 |
351 | _fd_ can be called with no arguments. This is very useful to get a quick overview of all entries
352 | in the current directory, recursively (similar to `ls -R`):
353 |
354 | ```bash
355 | > cd fd/tests
356 | > fd
357 | testenv
358 | testenv/mod.rs
359 | tests.rs
360 | ```
361 |
362 | ### Searching for a particular file extension
363 |
364 | Often, we are interested in all files of a particular type. This can be done with the `-e` (or
365 | `--extension`) option. Here, we search for all Markdown files in the fd repository:
366 |
367 | ```bash
368 | > cd fd
369 | > fd -e md
370 | CONTRIBUTING.md
371 | README.md
372 | ```
373 |
374 | The `-e` option can be used in combination with a search pattern:
375 |
376 | ```bash
377 | > fd -e rs mod
378 | src/fshelper/mod.rs
379 | src/lscolors/mod.rs
380 | tests/testenv/mod.rs
381 | ```
382 |
383 | ### Hidden and ignored files
384 |
385 | By default, _fd_ does not search hidden directories and does not show hidden files in the
386 | search results. To disable this behavior, we can use the `-H` (or `--hidden`) option:
387 |
388 | ```bash
389 | > fd pre-commit
390 | > fd -H pre-commit
391 | .git/hooks/pre-commit.sample
392 | ```
393 |
394 | If we work in a directory that is a Git repository (or includes Git repositories), _fd_ does not
395 | search folders (and does not show files) that match one of the `.gitignore` patterns. To disable
396 | this behavior, we can use the `-I` (or `--no-ignore`) option:
397 |
398 | ```bash
399 | > fd num_cpu
400 | > fd -I num_cpu
401 | target/debug/deps/libnum_cpus-f5ce7ef99006aa05.rlib
402 | ```
403 |
404 | To really search _all_ files and directories, simply combine the hidden and ignore features to show
405 | everything (`-HI`).
406 |
407 | ### Excluding specific files or directories
408 |
409 | Sometimes we want to ignore search results from a specific subdirectory. For example, we might
410 | want to search all hidden files and directories (`-H`) but exclude all matches from `.git`
411 | directories. We can use the `-E` (or `--exclude`) option for this. It takes an arbitrary glob
412 | pattern as an argument:
413 |
414 | ```bash
415 | > fd -H -E .git …
416 | ```
417 |
418 | We can also use this to skip mounted directories:
419 |
420 | ```bash
421 | > fd -E /mnt/external-drive …
422 | ```
423 |
424 | .. or to skip certain file types:
425 |
426 | ```bash
427 | > fd -E '*.bak' …
428 | ```
429 |
430 | To make exclude-patterns like these permanent, you can create a `.fdignore` file. They work like
431 | `.gitignore` files, but are specific to `fd`. For example:
432 |
433 | ```bash
434 | > cat ~/.fdignore
435 | /mnt/external-drive
436 | *.bak
437 | ```
438 |
439 | ### Using fd with `xargs` or `parallel`
440 |
441 | If we want to run a command on all search results, we can pipe the output to `xargs`:
442 |
443 | ```bash
444 | > fd -0 -e rs | xargs -0 wc -l
445 | ```
446 |
447 | Here, the `-0` option tells _fd_ to separate search results by the NULL character (instead of
448 | newlines). In the same way, the `-0` option of `xargs` tells it to read the input in this way.
449 |
450 | ### Integration with other programs
451 |
452 | #### Using fd with `fzf`
453 |
454 | You can use _fd_ to generate input for the command-line fuzzy finder [fzf](https://github.com/junegunn/fzf):
455 |
456 | ```bash
457 | export FZF_DEFAULT_COMMAND='fd --type file'
458 | export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
459 | ```
460 |
461 | Then, you can type `vim ` on your terminal to open fzf and search through the fd-results.
462 |
463 | Alternatively, you might like to follow symbolic links and include hidden files (but exclude `.git` folders):
464 |
465 | ```bash
466 | export FZF_DEFAULT_COMMAND='fd --type file --follow --hidden --exclude .git'
467 | ```
468 |
469 | You can even use fd's colored output inside fzf by setting:
470 |
471 | ```bash
472 | export FZF_DEFAULT_COMMAND="fd --type file --color=always"
473 | export FZF_DEFAULT_OPTS="--ansi"
474 | ```
475 |
476 | For more details, see the [Tips section](https://github.com/junegunn/fzf#tips) of the fzf README.
477 |
478 | #### Using fd with `emacs`
479 |
480 | The emacs package [find-file-in-project](https://github.com/technomancy/find-file-in-project) can
481 | use _fd_ to find files.
482 |
483 | After installing `find-file-in-project`, add the line `(setq ffip-use-rust-fd t)` to your
484 | `~/.emacs` or `~/.emacs.d/init.el` file.
485 |
486 | In emacs, run `M-x find-file-in-project-by-selected` to find matching files. Alternatively, run
487 | `M-x find-file-in-project` to list all available files in the project.
488 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # fd [![translate-svg]][translate-list]
2 |
3 | [translate-svg]: http://llever.com/translate.svg
4 | [translate-list]: https://github.com/chinanf-boy/chinese-translate-list
5 |
6 | 「 *fd*是一种简单ㄡ快速和用户友好的[*find*](https://www.gnu.org/software/findutils/)替代方案.」
7 |
8 | [中文](./readme.md) | [english](https://github.com/sharkdp/fd)
9 |
10 |
11 | ---
12 |
13 | ## 校对 ✅
14 |
15 |
16 |
17 |
18 |
19 | 翻译的原文 | 与日期 | 最新更新 | 更多
20 | ---|---|---|---
21 | [commit] | ⏰ 2018 8.20 | ![last] | [中文翻译][translate-list]
22 |
23 | [last]: https://img.shields.io/github/last-commit/sharkdp/fd.svg
24 | [commit]: https://github.com/sharkdp/fd/tree/2465cd139962c2f3a1d674846b6adc687daa72fa
25 |
26 |
27 |
28 | ### 贡献
29 |
30 | 欢迎 👏 勘误/校对/更新贡献 😊 [具体贡献请看](https://github.com/chinanf-boy/chinese-translate-list#贡献)
31 |
32 | ## 生活
33 |
34 | [If help, **buy** me coffee —— 营养跟不上了,给我来瓶营养快线吧! 💰](https://github.com/chinanf-boy/live-need-money)
35 |
36 | ---
37 |
38 | ### 目录
39 |
40 |
41 |
42 |
43 |
44 | - [fd](#fd)
45 | - [特征](#%E7%89%B9%E5%BE%81)
46 | - [演示](#%E6%BC%94%E7%A4%BA)
47 | - [基准](#%E5%9F%BA%E5%87%86)
48 | - [彩色输出](#%E5%BD%A9%E8%89%B2%E8%BE%93%E5%87%BA)
49 | - [并行命令执行](#%E5%B9%B6%E8%A1%8C%E5%91%BD%E4%BB%A4%E6%89%A7%E8%A1%8C)
50 | - [安装](#%E5%AE%89%E8%A3%85)
51 | - [Ubuntu](#ubuntu)
52 | - [Fedora](#fedora)
53 | - [Arch Linux](#arch-linux)
54 | - [Gentoo Linux](#gentoo-linux)
55 | - [openSUSE Linux](#opensuse-linux)
56 | - [Void Linux](#void-linux)
57 | - [macOS](#macos)
58 | - [Windows](#windows)
59 | - [NixOS / via Nix](#nixos--via-nix)
60 | - [FreeBSD](#freebsd)
61 | - [源码文件](#%E6%BA%90%E7%A0%81%E6%96%87%E4%BB%B6)
62 | - [二进制文件](#%E4%BA%8C%E8%BF%9B%E5%88%B6%E6%96%87%E4%BB%B6)
63 | - [开发](#%E5%BC%80%E5%8F%91)
64 | - [命令行选项](#%E5%91%BD%E4%BB%A4%E8%A1%8C%E9%80%89%E9%A1%B9)
65 | - [教程](#%E6%95%99%E7%A8%8B)
66 | - [简单搜索](#%E7%AE%80%E5%8D%95%E6%90%9C%E7%B4%A2)
67 | - [正则表达式搜索](#%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%90%9C%E7%B4%A2)
68 | - [指定根目录](#%E6%8C%87%E5%AE%9A%E6%A0%B9%E7%9B%AE%E5%BD%95)
69 | - [仅运行*fd*](#%E4%BB%85%E8%BF%90%E8%A1%8Cfd)
70 | - [搜索特定的文件扩展名](#%E6%90%9C%E7%B4%A2%E7%89%B9%E5%AE%9A%E7%9A%84%E6%96%87%E4%BB%B6%E6%89%A9%E5%B1%95%E5%90%8D)
71 | - [隐藏和忽略的文件](#%E9%9A%90%E8%97%8F%E5%92%8C%E5%BF%BD%E7%95%A5%E7%9A%84%E6%96%87%E4%BB%B6)
72 | - [排除特定文件或目录](#%E6%8E%92%E9%99%A4%E7%89%B9%E5%AE%9A%E6%96%87%E4%BB%B6%E6%88%96%E7%9B%AE%E5%BD%95)
73 | - [使用fd 带`xargs`或`parallel`](#%E4%BD%BF%E7%94%A8fd--%E5%B8%A6xargs%E6%88%96parallel)
74 | - [与其他程序的集成](#%E4%B8%8E%E5%85%B6%E4%BB%96%E7%A8%8B%E5%BA%8F%E7%9A%84%E9%9B%86%E6%88%90)
75 | - [使用fd与`fzf`](#%E4%BD%BF%E7%94%A8fd%E4%B8%8Efzf)
76 | - [使用fd与`emacs`](#%E4%BD%BF%E7%94%A8fd%E4%B8%8Eemacs)
77 |
78 |
79 |
80 |
81 | # fd
82 |
83 | [](https://travis-ci.org/sharkdp/fd)
84 | [](https://ci.appveyor.com/project/sharkdp/fd)
85 | [](https://crates.io/crates/fd-find)
86 |
87 | *fd*是一种简单ㄡ快速和用户友好的[*fd*](https://www.gnu.org/software/findutils/)替代方案.
88 |
89 | 虽然它不寻求复刻*find*所有强大的功能,但它提供了明智的 (自定的) [80%](https://en.wikipedia.org/wiki/Pareto_principle)的用例.
90 |
91 | ## 特征
92 |
93 | - 方便语法: `fd PATTERN`而不是`find -iname '*PATTERN*'`.
94 | - 彩色终端输出 (类似于*ls*)
95 | - 它是*快速的* (见[基准](#%E5%9F%BA%E5%87%86)下面) .
96 | - 聪明案例: 默认情况下,搜索不区分大小写. 如果模式包含大写字符[\*](http://vimdoc.sourceforge.net/htmldoc/options.html#'smartcase'), 则切换为区分大小写字符. .
97 | - 默认情况下,忽略隐藏的目录和文件.
98 | - 忽略匹配你`.gitignore`文件中的模式,默认情况.
99 | - 正则表达式.
100 | - Unicode感知.
101 | - 命令输入量*50%*优于[\*](https://github.com/ggreer/the_silver_searcher)`find`: -)
102 | - 用类似于GNU穿行的语法,执行并行命令.
103 |
104 | ## 演示
105 |
106 | 
107 |
108 | ## 基准
109 |
110 | 让我们搜索我的主文件夹的以`[0-9].jpg`为结束的文件. 它包含190个子目录和大约一百万个文件. 我使用[hyperfine](https://github.com/sharkdp/hyperfine)进行平均和统计分析. 下面的基准是用"warm"/预填充的磁盘缓存执行的 (对于"冷"磁盘缓存的结果显示出相同的趋势) .
111 |
112 | 让我们从`find`:
113 |
114 |
115 | Benchmark #1: find ~ -iregex '.*[0-9]\.jpg$'
116 |
117 | Time (mean ± σ): 7.236 s ± 0.090 s
118 |
119 | Range (min … max): 7.133 s … 7.385 s
120 |
121 | `find`如果不需要执行正则表达式搜索,则会更快得多:
122 |
123 | Benchmark #2: find ~ -iname '*[0-9].jpg'
124 |
125 | Time (mean ± σ): 3.914 s ± 0.027 s
126 |
127 | Range (min … max): 3.876 s … 3.964 s
128 |
129 | 现在让我们尝试同样的`fd`. 注意`fd` *总是*执行正则表达式搜索. 选项`--hidden`和`--no-ignore`需要自行决策, 下面的`fd`需要遍历隐藏文件夹和忽略的路径 (见下文) :
130 |
131 | Benchmark #3: fd -HI '.*[0-9]\.jpg$' ~
132 |
133 | Time (mean ± σ): 811.6 ms ± 26.9 ms
134 |
135 | Range (min … max): 786.0 ms … 870.7 ms
136 |
137 | 对于这个特殊的例子,`fd`大约比`find -iregex`快九倍,和大约比`find -iname`快五倍. 顺便说一下,两个工具都找到了完全相同的20880个文件: :smile: .
138 |
139 | 最后,让我们运行`fd`没有`--hidden`和`--no-ignore`选项 (当然,这会导致不同的搜索结果) . 如果*fd*不必遍历隐藏的和Git忽略的文件夹,它的数量级快了一个数量级:
140 |
141 | Benchmark #4: fd '[0-9]\.jpg$' ~
142 |
143 | Time (mean ± σ): 123.7 ms ± 6.0 ms
144 |
145 | Range (min … max): 118.8 ms … 140.0 ms
146 |
147 | **注释**这是在*一个特定的*机器上的*一个特定的*基准. 虽然我已经做了很多不同的测试 (并且发现了一致的结果) ,但是事情可能对你来说不同. 我鼓励每个人自己尝试测试. 在[这个仓库](https://github.com/sharkdp/fd-benchmarks)是所有用于对比的脚本.
148 |
149 | 关于*fd*的速度,主要的耗时在`regex`和`ignore`,还有[ripgrep](https://github.com/BurntSushi/ripgrep)箱子 (检查一下!) .
150 |
151 | ## 彩色输出
152 |
153 | `fd`可以通过扩展来帮输出着色,就像`ls`. 为了使这工作,环境变量[`LS_COLORS`](https://linux.die.net/man/5/dir_colors)必须设置. 通常,此变量的值由`dircolors`命令控制,它提供了一种方便的配置格式,来定义不同文件格式的颜色. 在大多数分配情况,`LS_COLORS`应该已经设置好了. 如果您正在寻找替代的,且更完整的 (以及更丰富多彩的) 变体,请参见[在这里](https://github.com/seebi/dircolors-solarized)或[在这里](https://github.com/trapd00r/LS_COLORS).
154 |
155 | ## 并行命令执行
156 |
157 | 如果`-x`/`--exec`选项与命令模板一起指定,将创建一个作业池,用于并行执行命令,每个发现的路径则作为输入. 生成命令的语法类似于GNU穿行的语法:
158 |
159 | - `{}`: 将被替换为搜索结果路径的占位符令牌 (`documents/images/party.jpg`)
160 | - `{.}`: 像`{}`,但没有文件扩展名 (`documents/images/party`)
161 | - `{/}`:占位符,将被搜索结果的基名替换 (占位符) . `party.jpg`)
162 | - `{//}`:使用已发现路径的父节点 (`documents/images`)
163 | - `{/.}`:使用BaseNeNe,将扩展名移除 (`party`)
164 |
165 | ```bash
166 | # 转换 所有 jpg 到 png :
167 | fd -e jpg -x convert {} {.}.png
168 |
169 | # Unpack all zip files (if no placeholder is given, the path is appended):
170 | fd -e zip -x unzip
171 |
172 | # Convert all flac files into opus files:
173 | fd -e flac -x ffmpeg -i {} -c:a libopus {.}.opus
174 |
175 | # Count the number of lines in Rust files (the command template can be terminated with ';'):
176 | fd -x wc -l \; -e rs
177 | ```
178 |
179 | ## 安装
180 |
181 | ### Ubuntu
182 |
183 | *以及其他基于Debian的Linux发行版.*
184 |
185 | 下载最新`.deb`包装从[releases页面](https://github.com/sharkdp/fd/releases)并通过以下方式安装:
186 |
187 | ```bash
188 | sudo dpkg -i fd_7.0.0_amd64.deb # adapt version number and architecture
189 | ```
190 |
191 | ### Fedora
192 |
193 | 从 FEDORA 28 开始,您可以从官方包装来源安装`fd`:
194 |
195 | ```bash
196 | dnf install fd-find
197 | ```
198 |
199 | 对于旧版本,您可以使用[Fedora copr](https://copr.fedorainfracloud.org/coprs/keefle/fd/)安装`fd`:
200 |
201 | ```bash
202 | dnf copr enable keefle/fd
203 | dnf install fd
204 | ```
205 |
206 | ### Arch Linux
207 |
208 | 你可以从官方回购安装[fd 软件包](https://www.archlinux.org/packages/community/x86_64/fd/):
209 |
210 | pacman -S fd
211 |
212 | ### Gentoo Linux
213 |
214 | 你可以从官方回购使用[fd 软件包](https://packages.gentoo.org/packages/sys-apps/fd):
215 |
216 | emerge -av fd
217 |
218 | ### openSUSE Linux
219 |
220 | 你可以从官方回购安装[fd 软件包](https://software.opensuse.org/package/fd):
221 |
222 | zypper in fd
223 |
224 | ### Void Linux
225 |
226 | 你可以安装`fd`通过xbps安装:
227 |
228 | xbps-install -S fd
229 |
230 | ### macOS
231 |
232 | 你可以安装`fd`具有[brew](http://braumeister.org/formula/fd):
233 |
234 | brew install fd
235 |
236 | 或与Mac port:
237 |
238 | sudo port install fd
239 |
240 | ### Windows
241 |
242 | 您可以从中 [releases页面](https://github.com/sharkdp/fd/releases),下载预构建的二进制文件.
243 |
244 | 或者,您可以安装`fd`通过[Scoop](http://scoop.sh):
245 |
246 | scoop install fd
247 |
248 | 或通过[Chocolatey](https://chocolatey.org):
249 |
250 | choco install fd
251 |
252 | ### NixOS / via Nix
253 |
254 | 你可以使用[NixOS 包管理](https://nixos.org/nix/)安装`fd`:
255 |
256 | nix-env -i fd
257 |
258 | ### FreeBSD
259 |
260 | 你可以安装`sysutils/fd`通过patmaster:
261 |
262 | portmaster sysutils/fd
263 |
264 | ### 源码文件
265 |
266 | 你可以通过rust的包管理[cargo](https://github.com/rust-lang/cargo)安装*fd*:
267 |
268 | cargo install fd-find
269 |
270 | 注意rust版本要*1.20.0*或以上.
271 |
272 | ### 二进制文件
273 |
274 | 这个[releases页面](https://github.com/sharkdp/fd/releases)包括Linux,MaOS和Windows的预编译二进制文件.
275 |
276 | ## 开发
277 |
278 | ```bash
279 | git clone https://github.com/sharkdp/fd
280 |
281 | # Build
282 | cd fd
283 | cargo build
284 |
285 | # Run unit tests and integration tests
286 | cargo test
287 |
288 | # Install
289 | cargo install
290 | ```
291 |
292 | ## 命令行选项
293 |
294 | USAGE:
295 | fd [FLAGS/OPTIONS] [] [...]
296 |
297 | FLAGS:
298 | -H, --hidden 搜索隐藏的文件和目录
299 | -I, --no-ignore 不要忽略 .(git | fd)ignore 文件匹配
300 | --no-ignore-vcs 不要忽略.gitignore文件的匹配
301 | -s, --case-sensitive 区分大小写的搜索(默认值:智能案例)
302 | -i, --ignore-case 不区分大小写的搜索(默认值:智能案例)
303 | -F, --fixed-strings 将模式视为文字字符串
304 | -a, --absolute-path 显示绝对路径而不是相对路径
305 | -L, --follow 遵循符号链接
306 | -p, --full-path 搜索完整路径(默认值:仅限 file-/dirname)
307 | -0, --print0 用null字符分隔结果
308 | -h, --help 打印帮助信息
309 | -V, --version 打印版本信息
310 |
311 | OPTIONS:
312 | -d, --max-depth 设置最大搜索深度(默认值:无)
313 | -t, --type ... 按类型过滤:文件(f),目录(d),符号链接(l),
314 | 可执行(x),空(e)
315 | -e, --extension ... 按文件扩展名过滤
316 | -x, --exec 为每个搜索结果执行命令
317 | -E, --exclude ... 排除与给定glob模式匹配的条目
318 | --ignore-file ... 以.gitignore格式添加自定义忽略文件
319 | -c, --color 何时使用颜色:never,*auto*, always
320 | -j, --threads 设置用于搜索和执行的线程数
321 | -S, --size ... 根据文件大小限制结果。
322 |
323 | ARGS:
324 | the search pattern, a regular expression (optional)
325 | ... the root directory for the filesystem search (optional)
326 |
327 | ## 教程
328 |
329 | 首先,为了获得所有可用的命令行选项的概述,您可以运行`fd -h`的简明帮助消息 (见上文) 或`fd --help`更详细的版本.
330 |
331 | ### 简单搜索
332 |
333 | *fd*设计用于查找文件系统中的条目. 你可以执行的最基本的搜索就是运行一个参数:搜索模式的*fd*. 例如,假设您想查找您的旧脚本 (包括`netflix`) :
334 |
335 | ```bash
336 | > fd netfl
337 | Software/python/imdb-ratings/netflix-details.py
338 | ```
339 |
340 | 如果只调用一个这样的参数,*fd*递归检索当前目录中, *包含*模式`netfl`的任何条目.
341 |
342 | ### 正则表达式搜索
343 |
344 | 搜索模式被视为正则表达式. 这里,我们搜索开始`x`并以`rc`结束的条目. :
345 |
346 | ```bash
347 | > cd /etc
348 | > fd '^x.*rc$'
349 | X11/xinit/xinitrc
350 | X11/xinit/xserverrc
351 | ```
352 |
353 | ### 指定根目录
354 |
355 | 如果我们想搜索一个特定的目录,它可以作为第二个参数*fd*:
356 |
357 | ```bash
358 | > fd passwd /etc
359 | /etc/default/passwd
360 | /etc/pam.d/passwd
361 | /etc/passwd
362 | ```
363 |
364 | ### 仅运行*fd*
365 |
366 | *fd*可以不带参数调用. 这是非常有用的,以便快速地查看当前目录中的所有条目,递归地 (类似于`ls -R`) :
367 |
368 | ```bash
369 | > cd fd/tests
370 | > fd
371 | testenv
372 | testenv/mod.rs
373 | tests.rs
374 | ```
375 |
376 | ### 搜索特定的文件扩展名
377 |
378 | 通常,我们对特定类型的所有文件感兴趣. 这可以用`-e` (或) `--extension`选择权. 在这里,我们搜索FD仓库中的所有md文件:
379 |
380 | ```bash
381 | > cd fd
382 | > fd -e md
383 | CONTRIBUTING.md
384 | README.md
385 | ```
386 |
387 | 这个`-e`选项可以与搜索模式结合使用:
388 |
389 | ```bash
390 | > fd -e rs mod
391 | src/fshelper/mod.rs
392 | src/lscolors/mod.rs
393 | tests/testenv/mod.rs
394 | ```
395 |
396 | ### 隐藏和忽略的文件
397 |
398 | 默认情况下,*fd*不搜索隐藏目录,不在搜索结果中显示隐藏文件. 若要禁用此行为,我们可以使用`-H` (或) `--hidden`选项:
399 |
400 | ```bash
401 | > fd pre-commit
402 | > fd -H pre-commit
403 | .git/hooks/pre-commit.sample
404 | ```
405 |
406 | 如果我们在一个Git存储库 (或者包括Git存储库) 中工作,*fd*不搜索`.gitignore`文件中匹配模式 (并且不显示文件) . 若要禁用此行为,我们可以使用`-I` (或) `--no-ignore`选项:
407 |
408 | ```bash
409 | > fd num_cpu
410 | > fd -I num_cpu
411 | target/debug/deps/libnum_cpus-f5ce7ef99006aa05.rlib
412 | ```
413 |
414 | 真正搜索*全部的*文件和目录,简单地组合隐藏和忽略的特性来显示一切 (`-HI`)
415 |
416 | ### 排除特定文件或目录
417 |
418 | 有时我们希望忽略来自特定子目录的搜索结果. 例如,我们可能要搜索所有隐藏的文件和目录 (`-H`,但仍会排除所有`.git`目录. 我们可以使用`-E` (或) `--exclude`选择此选项. 它以任意的模式作为一个参数:
419 |
420 | ```bash
421 | > fd -H -E .git …
422 | ```
423 |
424 | 我们也可以用这个来跳过安装的目录:
425 |
426 | ```bash
427 | > fd -E /mnt/external-drive …
428 | ```
429 |
430 | 或跳过某些文件类型:
431 |
432 | ```bash
433 | > fd -E '*.bak' …
434 | ```
435 |
436 | 为了让这些模式永久不变,你可以创建一个`.fdignore`文件. 他们工作得很像`.gitignore`文件. 例如:
437 |
438 | ```bash
439 | > cat ~/.fdignore
440 | /mnt/external-drive
441 | *.bak
442 | ```
443 |
444 | ### 使用fd 带`xargs`或`parallel`
445 |
446 | 如果我们想在所有搜索结果上运行命令,我们可以将输出管`xargs`:
447 |
448 | ```bash
449 | > fd -0 -e rs | xargs -0 wc -l
450 | ```
451 |
452 | 这里,`-0`选项告诉*fd*用空字符 (而不是换行符) 分隔搜索结果. 以同样的方式,`xargs`的`-0`选项同样告诉它以这种方式读取输入.
453 |
454 | ### 与其他程序的集成
455 |
456 | #### 使用fd与`fzf`
457 |
458 | 你可以使用*fd*生成[fzf](https://github.com/junegunn/fzf)命令行模糊查找器的输入:
459 |
460 | ```bash
461 | export FZF_DEFAULT_COMMAND='fd --type file'
462 | export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
463 | ```
464 |
465 | 然后,您可以键入`vim `在你的终端打开FZF,也即是fd的搜索结果.
466 |
467 | 或者,您可能喜欢遵循符号链接并包含隐藏文件 (但不包括`.git`文件夹) :
468 |
469 | ```bash
470 | export FZF_DEFAULT_COMMAND='fd --type file --follow --hidden --exclude .git'
471 | ```
472 |
473 | 你甚至可以通过设置fzf内的fd的颜色输出:
474 |
475 | ```bash
476 | export FZF_DEFAULT_COMMAND="fd --type file --color=always"
477 | export FZF_DEFAULT_OPTS="--ansi"
478 | ```
479 |
480 | 有关详细信息,请参见 *fzf* reamde文件的[提示部分](https://github.com/junegunn/fzf#tips).
481 |
482 | #### 使用fd与`emacs`
483 |
484 | Emacs封装了[find-file-in-project](https://github.com/technomancy/find-file-in-project)包, 这可以使用*fd*查找文件.
485 |
486 | 安装`find-file-in-project`后,添加行`(setq ffip-use-rust-fd t)`在你的`~/.emacs`或`~/.emacs.d/init.el`文件中.
487 |
488 | 在Emacs中,运行`M-x find-file-in-project-by-selected`查找匹配文件. 或者,运行`M-x find-file-in-project`列出项目中所有可用的文件.
489 |
--------------------------------------------------------------------------------