Updates for the tutorial

[skip ci]
This commit is contained in:
sharkdp 2017-10-22 19:16:49 +02:00 committed by David Peter
parent d32fe0241e
commit 5863a5e706
1 changed files with 79 additions and 173 deletions

252
README.md
View File

@ -194,194 +194,100 @@ ARGS:
## Tutorial ## Tutorial
First, to see all command line options, you can get `fd`'s help text by running: First, to get an overview of all available command line options, you can either run
``` `fd -h` for a concise help message (see above) or `fd --help` for a more detailed
fd --help version.
```
`fd` can be called with no arguments, it'll then show the content of the actual directory ### Simple search
recursively (like `ls -R`):
```
> cd fd
> fd
appveyor.yml
build.rs
Cargo.lock
Cargo.toml
LICENSE
README.md
src
src/app.rs
src/fshelper
src/fshelper/mod.rs
src/lscolors
src/lscolors/mod.rs
src/main.rs
tests
tests/testenv
tests/testenv/mod.rs
tests/tests.rs
```
If we work in a directory that is a Git repository (or includes several Git repositories), `fd` *fd* is designed to find entries in your filesystem. The most basic search you can perform is to
does not search folders (and does not show files) that match the `.gitignore` pattern. For instance, run *fd* with a single argument: the search pattern. For example, assume that you want to find an
see above example, and note that .gitignore is the following: old script of yours (the name included `netflix`):
``` ``` bash
target/ > fd netfl
**/*.rs.bk Software/python/imdb-ratings/netflix-details.py
``` ```
If called with just a single argument like this, *fd* searches the current directory recursively
for any entries that *contain* the pattern `netfl`.
To disable this behavior, we can use the `-I` (or `--ignore`) option: ### Regular expression search
```
> fd -I
Cargo.lock
Cargo.toml
LICENSE
README.md
appveyor.yml
build.rs
src
src\app.rs
src\fshelper
src\fshelper\mod.rs
src\lscolors
src\lscolors\mod.rs
src\main.rs
target
target\debug
target\debug\build
target\debug\build\fd-find-11fb469ceafee9ce
target\debug\build\fd-find-11fb469ceafee9ce\out
target\debug\build\fd-find-11fb469ceafee9ce\out\_fd
target\debug\build\fd-find-11fb469ceafee9ce\out\_fd.ps1
target\debug\build\fd-find-11fb469ceafee9ce\out\fd.bash-completion
target\debug\build\fd-find-11fb469ceafee9ce\out\fd.fish
target\debug\build\fd-find-11fb469ceafee9ce\output
target\debug\build\fd-find-8682b98943903a5b
target\debug\build\fd-find-8682b98943903a5b\build-script-build.exe
target\debug\build\fd-find-8682b98943903a5b\build_script_build-8682b98943903a5b.exe
target\debug\build\fd-find-8682b98943903a5b\build_script_build-8682b98943903a5b.pdb
target\debug\build\kernel32-sys-7568a971f6718c45
target\debug\build\kernel32-sys-7568a971f6718c45\out
target\debug\build\kernel32-sys-7568a971f6718c45\output
target\debug\build\kernel32-sys-d715e52d58016295
target\debug\build\kernel32-sys-d715e52d58016295\build-script-build.exe
target\debug\build\kernel32-sys-d715e52d58016295\build_script_build-d715e52d58016295.exe
target\debug\build\kernel32-sys-d715e52d58016295\build_script_build-d715e52d58016295.pdb
target\debug\deps
(some content omitted...)
target\debug\examples
target\debug\fd.exe
target\debug\incremental
target\debug\native
tests
tests\testenv
tests\testenv\mod.rs
tests\tests.rs
```
If `fd` is called with a single argument (the search pattern), it will perform a recursive search The search pattern is treated as a regular expression. Here, we search for entries that start
through the current directory. To search for all files that include the string "access" in the with `x` and end with `rc`:
/var/log directory, we can simply run: ``` bash
```
> cd /var/log
> fd access
cups/access_log
cups/access_log.1
cups/access_log.2
some_program/user_access
```
The search pattern is treated as a regular expression. To show only entries that start with "access",
we can simply run:
```
> fd '^access'
cups/access_log
cups/access_log.1
cups/access_log.2
```
Note that `fd` does not show hidden files (like `.access_control`) by default. To change this, we can use
the `-H` (or `--hidden`) option:
```
> fd -H access
cups/access_log
cups/access_log.1
cups/access_log.2
some_program/user_access
.access_control
```
If we are interested in showing the results from a particular directory, we can specify the root of
the search as a second argument:
```
> cd /etc > cd /etc
> fd firewall iptables > fd '^x.*rc$'
simple_firewall.rules X11/xinit/xinitrc
// TODO: review this with router content X11/xinit/xserverrc
``` ```
If we work in a directory that is a Git repository (or includes several Git repositories), `fd` ### Specifying the root directory
does not search folders (and does not show files) that match the `.gitignore` pattern. For example,
imagine we had a `.gitignore` file with the following content: If we want so search a specific directory, it can be given as a second argument to *fd*:
``` ``` bash
*.sh > fd passwd /etc
``` /etc/default/passwd
In this case, `fd` would not show any files that end in `.sh`. To disable this behavior, we can /etc/pam.d/passwd
use the `-I` (or `--ignore`) option: /etc/passwd
```
> fd -I me
sub_dir/more_dir/even_further_down/not_me.sh
``` ```
To really search *all* files and directories, we can combine the hidden and ignore features to show ### Running *fd* without any arguments
everything (`-HI`):
``` *fd* can be called with no arguments. This is very useful to get a quick overview of all entries
fd -HI 'not|here' in the current directory, recursively (similar to `ls -R`):
not_file ``` bash
sub_dir/.here_be_tests > cd fd/tests
sub_dir/more_dir/.not_here > fd
sub_dir/more_dir/even_further_down/not_me.sh testenv
sub_dir/more_dir/not_file testenv/mod.rs
tests.rs
``` ```
Searching for a file extension is easy too, using the `-e` (or `--file-extensions`) switch for file ### Searching for a particular file extension
extensions:
``` Often, we are interested in all files of a particular type. This can be done with the `-e` (or
> fd -e sh `--extension`) option. Here, we search for all Markdown files in the fd repository:
sub_dir/more_dir/even_further_down/not_me.sh ``` bash
> cd fd
> fd -e md
CONTRIBUTING.md
README.md
``` ```
Next, we can even use a pattern in combination with `-e` to search for a regex pattern over the The `-e` option can be used in combination with a search pattern:
files that end in the specified extension. ``` bash
``` > fd -e rs mod
> fd -e txt test src/fshelper/mod.rs
fd_examples/desub_dir/old_test.txt src/lscolors/mod.rs
fd_examples/sub_dir/new_test.txt tests/testenv/mod.rs
``` ```
What if we wanted to run a command for each of the search results? We can use `xargs` to do that: ### Hidden and ignored files
`fd -0 'test' | xargs -0 -I {} cp {} {}.new` By default, *fd* does not search hidden directories and does not show hidden files in the
search results. To disable this behavior, we can use the `-H` (or `--hidden`) option:
In this example there are a couple things to take note: ``` bash
- First we are telling `fd` we want a null character to seperate the files `-0`, this is > fd pre-commit
important when passing to `xargs`. > fd -H pre-commit
- Second, we are piping the output to `xargs` and telling this program to expect input null .git/hooks/pre-commit.sample
terminated with `-0` (the same syntax that `fd` was built with).
- Then for fun we are using `-I` to replace a string `{}` and lauching `cp` to copy the file `{}`
to a file ending in `{}.new`.
`fd` can also show us the absolute path vs. the full path with `-a` (`--absolute-path`):
``` ```
> fd -a new
/Users/fd_user/fd_examples/sub_dir/more_dir/even_further_down/test_seven.new If we work in a directory that is a Git repository (or includes Git repositories), *fd* does not
/Users/fd_user/fd_examples/sub_dir/more_dir/even_further_down/testing_eight.new search folders (and does not show files) that match one of the `.gitignore` patterns. To disable
/Users/fd_user/fd_examples/sub_dir/more_dir/test_file_six.new this behavior, we can use the `-I` (or `--ignore`) option:
/Users/fd_user/fd_examples/sub_dir/test_file_five.new ``` bash
/Users/fd_user/fd_examples/sub_dir/test_file_four.new > fd num_cpu
/Users/fd_user/fd_examples/sub_dir/test_file_three.new > fd -I num_cpu
/Users/fd_user/fd_examples/test_file_one.new target/debug/deps/libnum_cpus-f5ce7ef99006aa05.rlib
/Users/fd_user/fd_examples/test_file_two.new
/Users/fd_user/fd_examples/test_one.new
/Users/fd_user/fd_examples/this_is_a_test.new
``` ```
To really search *all* files and directories, simply combine the hidden and ignore features to show
everything (`-HI`).
### Using fd with `xargs` or `parallel`
If we want to run a command on all search results, we can pipe the output to `xargs`:
``` bash
> fd -0 -e rs | xargs -0 wc -l
```
Here, the `-0` option tells *fd* to separate search results by the NULL character (instead of .
newlines) In the same way, the `-0` option of `xargs` tells it to read the input in this way .