Skip to content
Myr

Updates for the tutorial

Before adoption: this commit came in with the history the project adopted, so no landing records it.

[skip ci]

authored by sharkdp · committed by David Peter · parent d32fe02 · Browse files · 1 file+74 added−168 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+74 added−168 removed

  • other:+74 added−168 removed

1 file+74 added−168 removed

modified:README.md+74 added-168 removed
192 unmodified lines
193```194195## Tutorial196197First, to see all command line options, you can get `fd`'s help text by running:198```199fd --help200```197First, to get an overview of all available command line options, you can either run198`fd -h` for a concise help message (see above) or `fd --help` for a more detailed199version.200202`fd` can be called with no arguments, it'll then show the content of the actual directory203recursively (like `ls -R`):204```205> 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```201### Simple search202226If 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:203*fd* is designed to find entries in your filesystem. The most basic search you can perform is to204run *fd* with a single argument: the search pattern. For example, assume that you want to find an205old script of yours (the name included `netflix`):206``` bash207> fd netfl208Software/python/imdb-ratings/netflix-details.py209```230target/231**/*.rs.bk232```210If called with just a single argument like this, *fd* searches the current directory recursively211for any entries that *contain* the pattern `netfl`.212234To 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```213### Regular expression search214283If `fd` is called with a single argument (the search pattern), it will perform a recursive search284through the current directory. To search for all files that include the string "access" in the285/var/log directory, we can simply run:215The search pattern is treated as a regular expression. Here, we search for entries that start216with `x` and end with `rc`:217``` bash218> cd /etc219> fd '^x.*rc$'220X11/xinit/xinitrc221X11/xinit/xserverrc222```287> cd /var/log288> fd access289cups/access_log290cups/access_log.1291cups/access_log.2292some_program/user_access293```223295The search pattern is treated as a regular expression. To show only entries that start with "access",296we can simply run:297```298> fd '^access'299cups/access_log300cups/access_log.1301cups/access_log.2302```224### Specifying the root directory225304Note that `fd` does not show hidden files (like `.access_control`) by default. To change this, we can use305the `-H` (or `--hidden`) option:226If we want so search a specific directory, it can be given as a second argument to *fd*:227``` bash228> fd passwd /etc229/etc/default/passwd230/etc/pam.d/passwd231/etc/passwd232```307> fd -H access308cups/access_log309cups/access_log.1310cups/access_log.2311some_program/user_access312.access_control313```233315If we are interested in showing the results from a particular directory, we can specify the root of316the search as a second argument:234### Running *fd* without any arguments235236*fd* can be called with no arguments. This is very useful to get a quick overview of all entries237in the current directory, recursively (similar to `ls -R`):238``` bash239> cd fd/tests240> fd241testenv242testenv/mod.rs243tests.rs244```318> cd /etc319> fd firewall iptables320simple_firewall.rules321// TODO: review this with router content322```245324If 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:246### Searching for a particular file extension247248Often, we are interested in all files of a particular type. This can be done with the `-e` (or249`--extension`) option. Here, we search for all Markdown files in the fd repository:250``` bash251> cd fd252> fd -e md253CONTRIBUTING.md254README.md255```328*.sh329```330In this case, `fd` would not show any files that end in `.sh`. To disable this behavior, we can331use the `-I` (or `--ignore`) option:332```333> fd -I me334sub_dir/more_dir/even_further_down/not_me.sh335```256337To really search *all* files and directories, we can combine the hidden and ignore features to show338everything (`-HI`):257The `-e` option can be used in combination with a search pattern:258``` bash259> fd -e rs mod260src/fshelper/mod.rs261src/lscolors/mod.rs262tests/testenv/mod.rs263```340fd -HI 'not|here'341not_file342sub_dir/.here_be_tests343sub_dir/more_dir/.not_here344sub_dir/more_dir/even_further_down/not_me.sh345sub_dir/more_dir/not_file346```264348Searching for a file extension is easy too, using the `-e` (or `--file-extensions`) switch for file349extensions:265### Hidden and ignored files266By default, *fd* does not search hidden directories and does not show hidden files in the267search results. To disable this behavior, we can use the `-H` (or `--hidden`) option:268``` bash269> fd pre-commit270> fd -H pre-commit271.git/hooks/pre-commit.sample272```351> fd -e sh352sub_dir/more_dir/even_further_down/not_me.sh353```273355Next, we can even use a pattern in combination with `-e` to search for a regex pattern over the356files that end in the specified extension.274If we work in a directory that is a Git repository (or includes Git repositories), *fd* does not275search folders (and does not show files) that match one of the `.gitignore` patterns. To disable276this behavior, we can use the `-I` (or `--ignore`) option:277``` bash278> fd num_cpu279> fd -I num_cpu280target/debug/deps/libnum_cpus-f5ce7ef99006aa05.rlib281```358> fd -e txt test359fd_examples/desub_dir/old_test.txt360fd_examples/sub_dir/new_test.txt361```282363What 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`283To really search *all* files and directories, simply combine the hidden and ignore features to show284everything (`-HI`).285366In 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`.286### Using fd with `xargs` or `parallel`287374`fd` can also show us the absolute path vs. the full path with `-a` (`--absolute-path`):288If we want to run a command on all search results, we can pipe the output to `xargs`:289``` bash290> fd -0 -e rs | xargs -0 wc -l291```376> 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```292Here, the `-0` option tells *fd* to separate search results by the NULL character (instead of .293newlines) In the same way, the `-0` option of `xargs` tells it to read the input in this way .