Skip to content
Myr

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

1 file+127 added−94 removed

modified:README.md+127 added-94 removed
197 unmodified lines
198```199fd --help200```201202For 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 directory203recursively (like `ls -R`):204```204fd_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> fd207appveyor.yml208build.rs209Cargo.lock210Cargo.toml211LICENSE212README.md213src214src/app.rs215src/fshelper216src/fshelper/mod.rs217src/lscolors218src/lscolors/mod.rs219src/main.rs220tests221tests/testenv222tests/testenv/mod.rs223tests/tests.rs224```225226If we work in a directory that is a Git repository (or includes several Git repositories), `fd`227does not search folders (and does not show files) that match the `.gitignore` pattern. For instance,228see above example, and note that .gitignore is the following:229```230target/231**/*.rs.bk232```233234To disable this behavior, we can use the `-I` (or `--ignore`) option:235```236> fd -I237Cargo.lock238Cargo.toml239LICENSE240README.md241appveyor.yml242build.rs243src244src\app.rs245src\fshelper246src\fshelper\mod.rs247src\lscolors248src\lscolors\mod.rs249src\main.rs250target251target\debug252target\debug\build253target\debug\build\fd-find-11fb469ceafee9ce254target\debug\build\fd-find-11fb469ceafee9ce\out255target\debug\build\fd-find-11fb469ceafee9ce\out\_fd256target\debug\build\fd-find-11fb469ceafee9ce\out\_fd.ps1257target\debug\build\fd-find-11fb469ceafee9ce\out\fd.bash-completion258target\debug\build\fd-find-11fb469ceafee9ce\out\fd.fish259target\debug\build\fd-find-11fb469ceafee9ce\output260target\debug\build\fd-find-8682b98943903a5b261target\debug\build\fd-find-8682b98943903a5b\build-script-build.exe262target\debug\build\fd-find-8682b98943903a5b\build_script_build-8682b98943903a5b.exe263target\debug\build\fd-find-8682b98943903a5b\build_script_build-8682b98943903a5b.pdb264target\debug\build\kernel32-sys-7568a971f6718c45265target\debug\build\kernel32-sys-7568a971f6718c45\out266target\debug\build\kernel32-sys-7568a971f6718c45\output267target\debug\build\kernel32-sys-d715e52d58016295268target\debug\build\kernel32-sys-d715e52d58016295\build-script-build.exe269target\debug\build\kernel32-sys-d715e52d58016295\build_script_build-d715e52d58016295.exe270target\debug\build\kernel32-sys-d715e52d58016295\build_script_build-d715e52d58016295.pdb271target\debug\deps272(some content omitted...)273target\debug\examples274target\debug\fd.exe275target\debug\incremental276target\debug\native277tests278tests\testenv279tests\testenv\mod.rs280tests\tests.rs281```282283If `fd` is called with a single argument (the search pattern), it will perform a recursive search230through the current directory. To search for all files that include the string "test", we can231simply run:284through 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 test234sub_dir/more_dir/even_further_down/test_seven235sub_dir/more_dir/even_further_down/testing_eight236sub_dir/more_dir/test_file_six237sub_dir/test_file_five238sub_dir/test_file_three239sub_dir/test_four240test_file_one241test_file_two242test_one243this_is_a_test287> cd /var/log288> fd access289cups/access_log290cups/access_log.1291cups/access_log.2292some_program/user_access293```294246The search pattern is treated as a regular expression. To show only entries that start with "test",247we can call:295The search pattern is treated as a regular expression. To show only entries that start with "access",296we can simply run:297```249> fd '^test'250sub_dir/more_dir/even_further_down/test_seven251sub_dir/more_dir/even_further_down/testing_eight252sub_dir/more_dir/test_file_six253sub_dir/test_file_five254sub_dir/test_file_three255sub_dir/test_four256test_file_one257test_file_two258test_one298> fd '^access'299cups/access_log300cups/access_log.1301cups/access_log.2302```303261Note that `fd` does not show hidden files (`.here_be_tests`) by default. To change this, we can use304Note that `fd` does not show hidden files (like `.access_control`) by default. To change this, we can use305the `-H` (or `--hidden`) option:306```264> fd -H test265sub_dir/.here_be_tests266sub_dir/more_dir/even_further_down/test_seven267sub_dir/more_dir/even_further_down/testing_eight268sub_dir/more_dir/test_file_six269sub_dir/test_file_five270sub_dir/test_file_four271sub_dir/test_file_three272test_file_one273test_file_two274test_one275this_is_a_test307> fd -H access308cups/access_log309cups/access_log.1310cups/access_log.2311some_program/user_access312.access_control313```314315If we are interested in showing the results from a particular directory, we can specify the root of316the search as a second argument:317```281> fd test sub_dir282sub_dir/more_dir/even_further_down/test_seven283sub_dir/more_dir/even_further_down/testing_eight284sub_dir/more_dir/test_file_six285sub_dir/test_file_five286sub_dir/test_file_three287sub_dir/test_four318> cd /etc319> fd firewall iptables320simple_firewall.rules321// TODO: review this with router content322```323290If we don't give *any* arguments to `fd`, it simply shows all entries in the current directory,291recursively (like `ls -R`):292```293> fd294not_file295sub_dir296sub_dir/more_dir297sub_dir/more_dir/even_further_down298sub_dir/more_dir/even_further_down/test_seven299sub_dir/more_dir/even_further_down/testing_eight300sub_dir/more_dir/not_file301sub_dir/more_dir/test_file_six302sub_dir/test_file_five303sub_dir/test_file_three304sub_dir/test_four305test_file_one306test_file_two307test_one308this_is_a_test309```310324If we work in a directory that is a Git repository (or includes several Git repositories), `fd`325does not search folders (and does not show files) that match the `.gitignore` pattern. For example,326imagine we had a `.gitignore` file with the following content:327```
16 unmodified lines
344sub_dir/more_dir/even_further_down/not_me.sh345sub_dir/more_dir/not_file346```347335Searching for a file extension is easy too, using the `-e` (or `--extension`) switch for file348Searching for a file extension is easy too, using the `-e` (or `--file-extensions`) switch for file349extensions:350```351> fd -e sh352sub_dir/more_dir/even_further_down/not_me.sh
6 unmodified lines
359fd_examples/desub_dir/old_test.txt360fd_examples/sub_dir/new_test.txt361```362350If we want to run a command for each of the search results, we can use the `-0` option to pipe351the output to `xargs`:363What 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`365366In 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`.373374`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```