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
Provenance
- Commit
080e8a9- Tree
1aec57e- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
3b55edf
1 file changed+58 added−118 removed
modified:README.md+58 added-118 removed
163 unmodified lines164 <pattern> the search pattern, a regular expression (optional)165 <path> the root directory for the filesystem search (optional)166 ```167 168-## Examples168+## Tutorial169 170-First to get `fd`'s help run: 171-170+First, to see all command line options, you can get `fd`'s help text by running:171 ```172 fd --help173 ```174 176-Let's assume we have some files we need to search through like so: 177-175+For the sake of this tutorial, let's assume we have a directory with the following file structure:176 ```177 fd_examples178 ├── .gitignore179 ├── desub_dir18 unmodified lines198 ├── test_one199 └── this_is_a_test200 ```201 204-Let's do a recursive search for anything that has the name test in it (`fd` will start in the current directory by default).205-206-`fd test`207-208-This will return: 209-202+If `fd` is called with a single argument (the search pattern), it will perform a recursive search203+through the current directory. To search for all files that include the string "test", we can204+simply run:205 ```206+> fd test207 sub_dir/more_dir/even_further_down/test_seven208 sub_dir/more_dir/even_further_down/testing_eight209 sub_dir/more_dir/test_file_six210 sub_dir/test_file_five4 unmodified lines215 test_one216 this_is_a_test217 ```218 223-Note: that `fd` does not show hidden files (`.here_be_tests`) by default to change this we can use the `-H` (or `--hidden`) option.224-225-`fd -H'test'`226-227-There they all are:228-219+The search pattern is treated as a regular expression. To show only entries that start with "test",220+we can simply run:221 ```230-sub_dir/.here_be_tests222+> fd '^test'223 sub_dir/more_dir/even_further_down/test_seven224 sub_dir/more_dir/even_further_down/testing_eight225 sub_dir/more_dir/test_file_six226 sub_dir/test_file_five235-sub_dir/test_file_four227 sub_dir/test_file_three228+sub_dir/test_four229 test_file_one230 test_file_two231 test_one240-this_is_a_test232 ```233 243-What 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. 244-245-`fd '^test'`246-247-Giving us: 248-234+Note that `fd` does not show hidden files (`.here_be_tests`) by default. To change this, we can use235+the `-H` (or `--hidden`) option:236 ```237+> fd -H test238+sub_dir/.here_be_tests239 sub_dir/more_dir/even_further_down/test_seven240 sub_dir/more_dir/even_further_down/testing_eight241 sub_dir/more_dir/test_file_six242 sub_dir/test_file_five243+sub_dir/test_file_four244 sub_dir/test_file_three255-sub_dir/test_four245 test_file_one246 test_file_two247 test_one248+this_is_a_test249 ```250 261-However, 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.262-263-`fd test ~/fd_examples/sub_dir/`264-251+If we are interested in showing the results from a particular directory, we can specify the root of252+the 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_four255+sub_dir/more_dir/even_further_down/test_seven256+sub_dir/more_dir/even_further_down/testing_eight257+sub_dir/more_dir/test_file_six258+sub_dir/test_file_five259+sub_dir/test_file_three260+sub_dir/test_four261 ```262 274-If we don't give `fd` an argument it will recursively search the current directory for all files (like `ls -R`): 275-263+If we don't give *any* arguments to `fd`, it simply shows all entries in the current directory,264+recursively (like `ls -R`):265 ```266+> fd267 not_file268 sub_dir269 sub_dir/more_dir270 sub_dir/more_dir/even_further_down9 unmodified lines280 test_one281 this_is_a_test282 ```283 294-`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:295-284+If we work in a directory that is a Git repository (or includes several Git repositories), `fd`285+does not search folders (and does not show files) that match the `.gitignore` pattern. For example,286+imagine we had a `.gitignore` file with the following content:287 ```288 *.sh289 ```299-300-`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.301-302-`fd -I me`303-290+In this case, `fd` would not show any files that end in `.sh`. To disable this behavior, we can291+use the `-I` (or `--ignore`) option:292 ```293+> fd -I me294 sub_dir/more_dir/even_further_down/not_me.sh295 ```296 308-Of course, we can combine the hidden and ignore features to show all files (`-HI`).309-310-`fd -HI ~/fd_examples 'not|here'`311-297+To really search *all* files and directories, we can combine the hidden and ignore features to show298+everything (`-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_file300+fd -HI 'not|here'301+not_file302+sub_dir/.here_be_tests303+sub_dir/more_dir/.not_here304+sub_dir/more_dir/even_further_down/not_me.sh305+sub_dir/more_dir/not_file306 ```307 320-Searching for a file extension is easy too, using the `-e` (or `--file-extensions`) switch for file extensions. 321-322-`fd -e sh`323-308+Searching for a file extension is easy too, using the `-e` (or `--file-extensions`) switch for file309+extensions:310 ```311+> fd -e sh312 sub_dir/more_dir/even_further_down/not_me.sh313 ```314 328-Next, 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.329-330-`fd -e txt test`331-315+Next, we can even use a pattern in combination with `-e` to search for a regex pattern over the316+files that end in the specified extension.317 ```318+> fd -e txt test319 fd_examples/desub_dir/old_test.txt320 fd_examples/sub_dir/new_test.txt321 ```322 337-What if we wanted to run some complicated bash follow on to the files? `xargs` can help us with that. 338-323+What 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`325 326 In 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`.333 346-That gives us: 347-334+`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-```380-381-`fd` can also show us the absolute path vs. the full path with `-a` (`--absolute-path`). 382-383-`fd -a 'new'`384-385-```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.new3 unmodified lines344 /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 ```397-398-We can also limit a search by searching for files within a specific path using `-p` (`--full-path`).399-400-`fd -p 'dir.*txt' ./fd_examples/`401-402-Here we are looking for any substring of "dir" followed by "txt" in the root folder of "fd_examples". Giving us:403-404-```405-fd_examples/desub_dir/old_test.txt406-fd_examples/sub_dir/new_test.txt407-```