Updated tutorial with real-world examples
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Initial work by @TitiAlone Closes #73 [skip ci]
authored by sharkdp · committed by David Peter · parent e9cf8af · Browse files · 1 file·+127 added−94 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+127 added−94 removed
- other:+127 added−94 removed
Provenance
- Commit
d32fe02- Tree
e558d2f- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
e9cf8af
1 file changed+127 added−94 removed
modified:README.md+127 added-94 removed
197 unmodified lines198 ```199 fd --help200 ```201 202-For the sake of this tutorial, let's assume we have a directory with the following file structure:202+`fd` can be called with no arguments, it'll then show the content of the actual directory203+recursively (like `ls -R`):204 ```204-fd_examples205-├── .gitignore206-├── desub_dir207-│ └── old_test.txt208-├── not_file209-├── sub_dir210-│ ├── .here_be_tests211-│ ├── more_dir212-│ │ ├── .not_here213-│ │ ├── even_further_down214-│ │ │ ├── not_me.sh215-│ │ │ ├── test_seven216-│ │ │ └── testing_eight217-│ │ ├── not_file -> /Users/fd_user/Desktop/fd_examples/not_file218-│ │ └── test_file_six219-│ ├── new_test.txt220-│ ├── test_file_five221-│ ├── test_file_four222-│ └── test_file_three223-├── test_file_one224-├── test_file_two225-├── test_one226-└── this_is_a_test205+> cd fd206+> fd207+appveyor.yml208+build.rs209+Cargo.lock210+Cargo.toml211+LICENSE212+README.md213+src214+src/app.rs215+src/fshelper216+src/fshelper/mod.rs217+src/lscolors218+src/lscolors/mod.rs219+src/main.rs220+tests221+tests/testenv222+tests/testenv/mod.rs223+tests/tests.rs224 ```225 226+If we work in a directory that is a Git repository (or includes several Git repositories), `fd`227+does not search folders (and does not show files) that match the `.gitignore` pattern. For instance,228+see above example, and note that .gitignore is the following:229+```230+target/231+**/*.rs.bk232+```233+234+To disable this behavior, we can use the `-I` (or `--ignore`) option:235+```236+> fd -I237+Cargo.lock238+Cargo.toml239+LICENSE240+README.md241+appveyor.yml242+build.rs243+src244+src\app.rs245+src\fshelper246+src\fshelper\mod.rs247+src\lscolors248+src\lscolors\mod.rs249+src\main.rs250+target251+target\debug252+target\debug\build253+target\debug\build\fd-find-11fb469ceafee9ce254+target\debug\build\fd-find-11fb469ceafee9ce\out255+target\debug\build\fd-find-11fb469ceafee9ce\out\_fd256+target\debug\build\fd-find-11fb469ceafee9ce\out\_fd.ps1257+target\debug\build\fd-find-11fb469ceafee9ce\out\fd.bash-completion258+target\debug\build\fd-find-11fb469ceafee9ce\out\fd.fish259+target\debug\build\fd-find-11fb469ceafee9ce\output260+target\debug\build\fd-find-8682b98943903a5b261+target\debug\build\fd-find-8682b98943903a5b\build-script-build.exe262+target\debug\build\fd-find-8682b98943903a5b\build_script_build-8682b98943903a5b.exe263+target\debug\build\fd-find-8682b98943903a5b\build_script_build-8682b98943903a5b.pdb264+target\debug\build\kernel32-sys-7568a971f6718c45265+target\debug\build\kernel32-sys-7568a971f6718c45\out266+target\debug\build\kernel32-sys-7568a971f6718c45\output267+target\debug\build\kernel32-sys-d715e52d58016295268+target\debug\build\kernel32-sys-d715e52d58016295\build-script-build.exe269+target\debug\build\kernel32-sys-d715e52d58016295\build_script_build-d715e52d58016295.exe270+target\debug\build\kernel32-sys-d715e52d58016295\build_script_build-d715e52d58016295.pdb271+target\debug\deps272+(some content omitted...)273+target\debug\examples274+target\debug\fd.exe275+target\debug\incremental276+target\debug\native277+tests278+tests\testenv279+tests\testenv\mod.rs280+tests\tests.rs281+```282+283 If `fd` is called with a single argument (the search pattern), it will perform a recursive search230-through the current directory. To search for all files that include the string "test", we can231-simply run:284+through the current directory. To search for all files that include the string "access" in the285+/var/log directory, we can simply run:286 ```233-> fd test234-sub_dir/more_dir/even_further_down/test_seven235-sub_dir/more_dir/even_further_down/testing_eight236-sub_dir/more_dir/test_file_six237-sub_dir/test_file_five238-sub_dir/test_file_three239-sub_dir/test_four240-test_file_one241-test_file_two242-test_one243-this_is_a_test287+> cd /var/log288+> fd access289+cups/access_log290+cups/access_log.1291+cups/access_log.2292+some_program/user_access293 ```294 246-The search pattern is treated as a regular expression. To show only entries that start with "test",247-we can call:295+The search pattern is treated as a regular expression. To show only entries that start with "access",296+we can simply run:297 ```249-> fd '^test'250-sub_dir/more_dir/even_further_down/test_seven251-sub_dir/more_dir/even_further_down/testing_eight252-sub_dir/more_dir/test_file_six253-sub_dir/test_file_five254-sub_dir/test_file_three255-sub_dir/test_four256-test_file_one257-test_file_two258-test_one298+> fd '^access'299+cups/access_log300+cups/access_log.1301+cups/access_log.2302 ```303 261-Note that `fd` does not show hidden files (`.here_be_tests`) by default. To change this, we can use304+Note that `fd` does not show hidden files (like `.access_control`) by default. To change this, we can use305 the `-H` (or `--hidden`) option:306 ```264-> fd -H test265-sub_dir/.here_be_tests266-sub_dir/more_dir/even_further_down/test_seven267-sub_dir/more_dir/even_further_down/testing_eight268-sub_dir/more_dir/test_file_six269-sub_dir/test_file_five270-sub_dir/test_file_four271-sub_dir/test_file_three272-test_file_one273-test_file_two274-test_one275-this_is_a_test307+> fd -H access308+cups/access_log309+cups/access_log.1310+cups/access_log.2311+some_program/user_access312+.access_control313 ```314 315 If we are interested in showing the results from a particular directory, we can specify the root of316 the search as a second argument:317 ```281-> fd test sub_dir282-sub_dir/more_dir/even_further_down/test_seven283-sub_dir/more_dir/even_further_down/testing_eight284-sub_dir/more_dir/test_file_six285-sub_dir/test_file_five286-sub_dir/test_file_three287-sub_dir/test_four318+> cd /etc319+> fd firewall iptables320+simple_firewall.rules321+// TODO: review this with router content322 ```323 290-If we don't give *any* arguments to `fd`, it simply shows all entries in the current directory,291-recursively (like `ls -R`):292-```293-> fd294-not_file295-sub_dir296-sub_dir/more_dir297-sub_dir/more_dir/even_further_down298-sub_dir/more_dir/even_further_down/test_seven299-sub_dir/more_dir/even_further_down/testing_eight300-sub_dir/more_dir/not_file301-sub_dir/more_dir/test_file_six302-sub_dir/test_file_five303-sub_dir/test_file_three304-sub_dir/test_four305-test_file_one306-test_file_two307-test_one308-this_is_a_test309-```310-324 If we work in a directory that is a Git repository (or includes several Git repositories), `fd`325 does not search folders (and does not show files) that match the `.gitignore` pattern. For example,326 imagine we had a `.gitignore` file with the following content:327 ```16 unmodified lines344 sub_dir/more_dir/even_further_down/not_me.sh345 sub_dir/more_dir/not_file346 ```347 335-Searching for a file extension is easy too, using the `-e` (or `--extension`) switch for file348+Searching for a file extension is easy too, using the `-e` (or `--file-extensions`) switch for file349 extensions:350 ```351 > fd -e sh352 sub_dir/more_dir/even_further_down/not_me.sh6 unmodified lines359 fd_examples/desub_dir/old_test.txt360 fd_examples/sub_dir/new_test.txt361 ```362 350-If we want to run a command for each of the search results, we can use the `-0` option to pipe351-the output to `xargs`:363+What if we wanted to run a command for each of the search results? We can use `xargs` to do that:364+`fd -0 'test' | xargs -0 -I {} cp {} {}.new`365+366+In this example there are a couple things to take note:367+ - First we are telling `fd` we want a null character to seperate the files `-0`, this is368+ important when passing to `xargs`.369+ - Second, we are piping the output to `xargs` and telling this program to expect input null370+ terminated with `-0` (the same syntax that `fd` was built with).371+ - Then for fun we are using `-I` to replace a string `{}` and lauching `cp` to copy the file `{}`372+ to a file ending in `{}.new`.373+374+`fd` can also show us the absolute path vs. the full path with `-a` (`--absolute-path`):375 ```353-> fd -0 'test' | xargs -0 wc -l376+> fd -a new377+/Users/fd_user/fd_examples/sub_dir/more_dir/even_further_down/test_seven.new378+/Users/fd_user/fd_examples/sub_dir/more_dir/even_further_down/testing_eight.new379+/Users/fd_user/fd_examples/sub_dir/more_dir/test_file_six.new380+/Users/fd_user/fd_examples/sub_dir/test_file_five.new381+/Users/fd_user/fd_examples/sub_dir/test_file_four.new382+/Users/fd_user/fd_examples/sub_dir/test_file_three.new383+/Users/fd_user/fd_examples/test_file_one.new384+/Users/fd_user/fd_examples/test_file_two.new385+/Users/fd_user/fd_examples/test_one.new386+/Users/fd_user/fd_examples/this_is_a_test.new387 ```