Updated tutorial with real-world examples

Initial work by @TitiAlone

Closes #73

[skip ci]
This commit is contained in:
sharkdp 2017-10-15 22:27:02 +02:00 committed by David Peter
parent e9cf8af911
commit d32fe0241e
1 changed files with 127 additions and 94 deletions

221
README.md
View File

@ -199,113 +199,126 @@ First, to see all command line options, you can get `fd`'s help text by running:
fd --help fd --help
``` ```
For the sake of this tutorial, let's assume we have a directory with the following file structure: `fd` can be called with no arguments, it'll then show the content of the actual directory
recursively (like `ls -R`):
``` ```
fd_examples > cd fd
├── .gitignore > fd
├── desub_dir appveyor.yml
│   └── old_test.txt build.rs
├── not_file Cargo.lock
├── sub_dir Cargo.toml
│   ├── .here_be_tests LICENSE
│   ├── more_dir README.md
│   │   ├── .not_here src
│   │   ├── even_further_down src/app.rs
│   │   │   ├── not_me.sh src/fshelper
│   │   │   ├── test_seven src/fshelper/mod.rs
│   │   │   └── testing_eight src/lscolors
│   │   ├── not_file -> /Users/fd_user/Desktop/fd_examples/not_file src/lscolors/mod.rs
│   │   └── test_file_six src/main.rs
│   ├── new_test.txt tests
│   ├── test_file_five tests/testenv
│   ├── test_file_four tests/testenv/mod.rs
│   └── test_file_three tests/tests.rs
├── test_file_one ```
├── test_file_two
├── test_one If we work in a directory that is a Git repository (or includes several Git repositories), `fd`
└── this_is_a_test does not search folders (and does not show files) that match the `.gitignore` pattern. For instance,
see above example, and note that .gitignore is the following:
```
target/
**/*.rs.bk
```
To disable this behavior, we can use the `-I` (or `--ignore`) option:
```
> 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 If `fd` is called with a single argument (the search pattern), it will perform a recursive search
through the current directory. To search for all files that include the string "test", we can through the current directory. To search for all files that include the string "access" in the
simply run: /var/log directory, we can simply run:
``` ```
> fd test > cd /var/log
sub_dir/more_dir/even_further_down/test_seven > fd access
sub_dir/more_dir/even_further_down/testing_eight cups/access_log
sub_dir/more_dir/test_file_six cups/access_log.1
sub_dir/test_file_five cups/access_log.2
sub_dir/test_file_three some_program/user_access
sub_dir/test_four
test_file_one
test_file_two
test_one
this_is_a_test
``` ```
The search pattern is treated as a regular expression. To show only entries that start with "test", The search pattern is treated as a regular expression. To show only entries that start with "access",
we can call: we can simply run:
``` ```
> fd '^test' > fd '^access'
sub_dir/more_dir/even_further_down/test_seven cups/access_log
sub_dir/more_dir/even_further_down/testing_eight cups/access_log.1
sub_dir/more_dir/test_file_six cups/access_log.2
sub_dir/test_file_five
sub_dir/test_file_three
sub_dir/test_four
test_file_one
test_file_two
test_one
``` ```
Note that `fd` does not show hidden files (`.here_be_tests`) by default. To change this, we can use Note that `fd` does not show hidden files (like `.access_control`) by default. To change this, we can use
the `-H` (or `--hidden`) option: the `-H` (or `--hidden`) option:
``` ```
> fd -H test > fd -H access
sub_dir/.here_be_tests cups/access_log
sub_dir/more_dir/even_further_down/test_seven cups/access_log.1
sub_dir/more_dir/even_further_down/testing_eight cups/access_log.2
sub_dir/more_dir/test_file_six some_program/user_access
sub_dir/test_file_five .access_control
sub_dir/test_file_four
sub_dir/test_file_three
test_file_one
test_file_two
test_one
this_is_a_test
``` ```
If we are interested in showing the results from a particular directory, we can specify the root of If we are interested in showing the results from a particular directory, we can specify the root of
the search as a second argument: the search as a second argument:
``` ```
> fd test sub_dir > cd /etc
sub_dir/more_dir/even_further_down/test_seven > fd firewall iptables
sub_dir/more_dir/even_further_down/testing_eight simple_firewall.rules
sub_dir/more_dir/test_file_six // TODO: review this with router content
sub_dir/test_file_five
sub_dir/test_file_three
sub_dir/test_four
```
If we don't give *any* arguments to `fd`, it simply shows all entries in the current directory,
recursively (like `ls -R`):
```
> fd
not_file
sub_dir
sub_dir/more_dir
sub_dir/more_dir/even_further_down
sub_dir/more_dir/even_further_down/test_seven
sub_dir/more_dir/even_further_down/testing_eight
sub_dir/more_dir/not_file
sub_dir/more_dir/test_file_six
sub_dir/test_file_five
sub_dir/test_file_three
sub_dir/test_four
test_file_one
test_file_two
test_one
this_is_a_test
``` ```
If we work in a directory that is a Git repository (or includes several Git repositories), `fd` If we work in a directory that is a Git repository (or includes several Git repositories), `fd`
@ -332,7 +345,7 @@ sub_dir/more_dir/even_further_down/not_me.sh
sub_dir/more_dir/not_file sub_dir/more_dir/not_file
``` ```
Searching for a file extension is easy too, using the `-e` (or `--extension`) switch for file Searching for a file extension is easy too, using the `-e` (or `--file-extensions`) switch for file
extensions: extensions:
``` ```
> fd -e sh > fd -e sh
@ -347,8 +360,28 @@ fd_examples/desub_dir/old_test.txt
fd_examples/sub_dir/new_test.txt fd_examples/sub_dir/new_test.txt
``` ```
If we want to run a command for each of the search results, we can use the `-0` option to pipe What if we wanted to run a command for each of the search results? We can use `xargs` to do that:
the output to `xargs`: `fd -0 'test' | xargs -0 -I {} cp {} {}.new`
In this example there are a couple things to take note:
- First we are telling `fd` we want a null character to seperate the files `-0`, this is
important when passing to `xargs`.
- Second, we are piping the output to `xargs` and telling this program to expect input null
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 -0 'test' | xargs -0 wc -l > fd -a new
/Users/fd_user/fd_examples/sub_dir/more_dir/even_further_down/test_seven.new
/Users/fd_user/fd_examples/sub_dir/more_dir/even_further_down/testing_eight.new
/Users/fd_user/fd_examples/sub_dir/more_dir/test_file_six.new
/Users/fd_user/fd_examples/sub_dir/test_file_five.new
/Users/fd_user/fd_examples/sub_dir/test_file_four.new
/Users/fd_user/fd_examples/sub_dir/test_file_three.new
/Users/fd_user/fd_examples/test_file_one.new
/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
``` ```