Skip to content

Commit 2278836

Browse files
committed
docs: clarify --exec positional argument handling
1 parent 7027d45 commit 2278836

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ fd -e h -e cpp -x clang-format -i
189189
Note how the `-i` option to `clang-format` can be passed as a separate argument. This is why
190190
we put the `-x` option last.
191191

192+
Any positional arguments after `-x` belong to the command template, not to `fd` itself. If you
193+
also want to pass a pattern or search path, put `-x` last:
194+
``` bash
195+
fd pattern path -x echo
196+
```
197+
192198
Find all `test_*.py` files and open them in your favorite editor:
193199
``` bash
194200
fd -g 'test_*.py' -X vim
@@ -228,6 +234,9 @@ fd -tf -x md5sum > file_checksums.txt
228234
The `-x` and `-X` options take a *command template* as a series of arguments (instead of a single string).
229235
If you want to add additional options to `fd` after the command template, you can terminate it with a `\;`.
230236

237+
For example, `fd -x echo \; pattern path` treats `pattern path` as `fd` arguments instead of
238+
passing them to `echo`. In practice, it is often clearer to write `fd pattern path -x echo`.
239+
231240
The syntax for generating commands is similar to that of [GNU Parallel](https://www.gnu.org/software/parallel/):
232241

233242
- `{}`: A placeholder token that will be replaced with the path of the search result

src/cli.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,8 @@ impl clap::Args for Exec {
872872
"Execute a command for each search result in parallel (use --threads=1 for sequential command execution). \
873873
There is no guarantee of the order commands are executed in, and the order should not be depended upon. \
874874
All positional arguments following --exec are considered to be arguments to the command - not to fd. \
875-
It is therefore recommended to place the '-x'/'--exec' option last.\n\
875+
It is therefore recommended to place the '-x'/'--exec' option last. \
876+
Use '\\;' to terminate the command template if you need to continue passing fd arguments afterwards.\n\
876877
The following placeholders are substituted before the command is executed:\n \
877878
'{}': path (of the current search result)\n \
878879
'{/}': basename\n \
@@ -887,6 +888,8 @@ impl clap::Args for Exec {
887888
fd -e zip -x unzip\n\n \
888889
- find *.h and *.cpp files and run \"clang-format -i ..\" for each of them:\n\n \
889890
fd -e h -e cpp -x clang-format -i\n\n \
891+
- search within `src/` and echo each match (place `-x` last):\n\n \
892+
fd . src -x echo\n\n \
890893
- Convert all *.jpg files to *.png files:\n\n \
891894
fd -e jpg -x convert {} {.}.png\
892895
",

0 commit comments

Comments
 (0)