Skip to content
Myr

First update for tutorial

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

authored by sharkdp · committed by sharkdp · parent 3b55edf · Browse files · 1 file+58 added−118 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+58 added−118 removed

  • other:+58 added−118 removed

1 file+58 added−118 removed

modified:README.md+58 added-118 removed
163 unmodified lines
164 <pattern> the search pattern, a regular expression (optional)165 <path> the root directory for the filesystem search (optional)166```167168## Examples168## Tutorial169170First to get `fd`'s help run: 171170First, to see all command line options, you can get `fd`'s help text by running:171```172fd --help173```174176Let's assume we have some files we need to search through like so: 177175For the sake of this tutorial, let's assume we have a directory with the following file structure:176```177fd_examples178├── .gitignore179├── desub_dir
18 unmodified lines
198├── test_one199└── this_is_a_test200```201204Let's do a recursive search for anything that has the name test in it (`fd` will start in the current directory by default).205206`fd test`207208This will return: 209202If `fd` is called with a single argument (the search pattern), it will perform a recursive search203through the current directory. To search for all files that include the string "test", we can204simply run:205```206> fd test207sub_dir/more_dir/even_further_down/test_seven208sub_dir/more_dir/even_further_down/testing_eight209sub_dir/more_dir/test_file_six210sub_dir/test_file_five
4 unmodified lines
215test_one216this_is_a_test217```218223Note: that `fd` does not show hidden files (`.here_be_tests`) by default to change this we can use the `-H` (or `--hidden`) option.224225`fd -H'test'`226227There they all are:228219The search pattern is treated as a regular expression. To show only entries that start with "test",220we can simply run:221```230sub_dir/.here_be_tests222> fd '^test'223sub_dir/more_dir/even_further_down/test_seven224sub_dir/more_dir/even_further_down/testing_eight225sub_dir/more_dir/test_file_six226sub_dir/test_file_five235sub_dir/test_file_four227sub_dir/test_file_three228sub_dir/test_four229test_file_one230test_file_two231test_one240this_is_a_test232```233243What if we wanted to find only when the file began with `test`? Well, `fd` does regex searches (by default) so using the regex indicator for beginning of line `^` will get us what we want. 244245`fd '^test'`246247Giving us: 248234Note that `fd` does not show hidden files (`.here_be_tests`) by default. To change this, we can use235the `-H` (or `--hidden`) option:236```237> fd -H test238sub_dir/.here_be_tests239sub_dir/more_dir/even_further_down/test_seven240sub_dir/more_dir/even_further_down/testing_eight241sub_dir/more_dir/test_file_six242sub_dir/test_file_five243sub_dir/test_file_four244sub_dir/test_file_three255sub_dir/test_four245test_file_one246test_file_two247test_one248this_is_a_test249```250261However, we really only wanted to see the filenames that contain `test` in the `fd_examples/sub_dir` folder? This can be done from anywhere in the file structure by giving it the path.262263`fd test ~/fd_examples/sub_dir/`264251If we are interested in showing the results from a particular directory, we can specify the root of252the search as a second argument:253```254> fd test sub_dir266/Users/fd_user/fd_examples/sub_dir/more_dir/even_further_down/test_seven267/Users/fd_user/fd_examples/sub_dir/more_dir/even_further_down/testing_eight268/Users/fd_user/fd_examples/sub_dir/more_dir/test_file_six269/Users/fd_user/fd_examples/sub_dir/test_file_five270/Users/fd_user/fd_examples/sub_dir/test_file_three271/Users/fd_user/fd_examples/sub_dir/test_four255sub_dir/more_dir/even_further_down/test_seven256sub_dir/more_dir/even_further_down/testing_eight257sub_dir/more_dir/test_file_six258sub_dir/test_file_five259sub_dir/test_file_three260sub_dir/test_four261```262274If we don't give `fd` an argument it will recursively search the current directory for all files (like `ls -R`): 275263If we don't give *any* arguments to `fd`, it simply shows all entries in the current directory,264recursively (like `ls -R`):265```266> fd267not_file268sub_dir269sub_dir/more_dir270sub_dir/more_dir/even_further_down
9 unmodified lines
280test_one281this_is_a_test282```283294`fd` is magic, it will look for a `.gitignore` file and treat the rules inside it as rules in the search pattern. So if we have a `.gitignore` file like:295284If we work in a directory that is a Git repository (or includes several Git repositories), `fd`285does not search folders (and does not show files) that match the `.gitignore` pattern. For example,286imagine we had a `.gitignore` file with the following content:287```288*.sh289```299300`fd` will then never look for any files that end in `.sh`. We can tell `fd` to ignore `.gitignore` files with `-I` (or `--ignore`) to temporarliy stop that from happening.301302`fd -I me`303290In this case, `fd` would not show any files that end in `.sh`. To disable this behavior, we can291use the `-I` (or `--ignore`) option:292```293> fd -I me294sub_dir/more_dir/even_further_down/not_me.sh295```296308Of course, we can combine the hidden and ignore features to show all files (`-HI`).309310`fd -HI ~/fd_examples 'not|here'`311297To really search *all* files and directories, we can combine the hidden and ignore features to show298everything (`-HI`):299```313/Users/fd_user/fd_examples/not_file314/Users/fd_user/fd_examples/sub_dir/.here_be_tests315/Users/fd_user/fd_examples/sub_dir/more_dir/.not_here316/Users/fd_user/fd_examples/sub_dir/more_dir/even_further_down/not_me.sh317/Users/fd_user/fd_examples/sub_dir/more_dir/not_file300fd -HI 'not|here'301not_file302sub_dir/.here_be_tests303sub_dir/more_dir/.not_here304sub_dir/more_dir/even_further_down/not_me.sh305sub_dir/more_dir/not_file306```307320Searching for a file extension is easy too, using the `-e` (or `--file-extensions`) switch for file extensions. 321322`fd -e sh`323308Searching for a file extension is easy too, using the `-e` (or `--file-extensions`) switch for file309extensions:310```311> fd -e sh312sub_dir/more_dir/even_further_down/not_me.sh313```314328Next, we can even use a pattern in combination with `-e` to search for a regex pattern over the files that end in the specified extension.329330`fd -e txt test`331315Next, we can even use a pattern in combination with `-e` to search for a regex pattern over the316files that end in the specified extension.317```318> fd -e txt test319fd_examples/desub_dir/old_test.txt320fd_examples/sub_dir/new_test.txt321```322337What if we wanted to run some complicated bash follow on to the files? `xargs` can help us with that. 338323What if we wanted to run a command for each of the search results? We can use `xargs` to do that:324`fd -0 'test' | xargs -0 -I {} cp {} {}.new`325326In this example there are a couple things to take note:342 - First we are telling `fd` we want a null character to seperate the files `-0`, this is important when passing to `xargs`.343 - 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).344 - Then for fun we are using `-I` to replace a string `{}` and lauching `cp` to copy the file `{}` to a file ending in `{}.new`.327 - First we are telling `fd` we want a null character to seperate the files `-0`, this is328 important when passing to `xargs`.329 - Second, we are piping the output to `xargs` and telling this program to expect input null330 terminated with `-0` (the same syntax that `fd` was built with).331 - Then for fun we are using `-I` to replace a string `{}` and lauching `cp` to copy the file `{}`332 to a file ending in `{}.new`.333346That gives us: 347334`fd` can also show us the absolute path vs. the full path with `-a` (`--absolute-path`):335```349.350├── .gitignore351├── not_file352├── sub_dir353│   ├── .here_be_tests354│   ├── more_dir355│   │   ├── .not_here356│   │   ├── even_further_down357│   │   │   ├── not_me.sh358│   │   │   ├── test_seven359│   │   │   ├── test_seven.new360│   │   │   ├── testing_eight361│   │   │   └── testing_eight.new362│   │   ├── not_file -> /Users/fd_user/fd_examples/not_file363│   │   ├── test_file_six364│   │   └── test_file_six.new365│   ├── test_file_five366│   ├── test_file_five.new367│   ├── test_file_four368│   ├── test_file_four.new369│   ├── test_file_three370│   └── test_file_three.new371├── test_file_one372├── test_file_one.new373├── test_file_two374├── test_file_two.new375├── test_one376├── test_one.new377├── this_is_a_test378└── this_is_a_test.new379```380381`fd` can also show us the absolute path vs. the full path with `-a` (`--absolute-path`). 382383`fd -a 'new'`384385```336> fd -a new337/Users/fd_user/fd_examples/sub_dir/more_dir/even_further_down/test_seven.new338/Users/fd_user/fd_examples/sub_dir/more_dir/even_further_down/testing_eight.new339/Users/fd_user/fd_examples/sub_dir/more_dir/test_file_six.new340/Users/fd_user/fd_examples/sub_dir/test_file_five.new
3 unmodified lines
344/Users/fd_user/fd_examples/test_file_two.new345/Users/fd_user/fd_examples/test_one.new346/Users/fd_user/fd_examples/this_is_a_test.new347```397398We can also limit a search by searching for files within a specific path using `-p` (`--full-path`).399400`fd -p 'dir.*txt' ./fd_examples/`401402Here we are looking for any substring of "dir" followed by "txt" in the root folder of "fd_examples". Giving us:403404```405fd_examples/desub_dir/old_test.txt406fd_examples/sub_dir/new_test.txt407```