|
| 1 | +# xargs Default Command Is echo |
| 2 | + |
| 3 | +With the `fd` command, all the found files are output line by line like so. |
| 4 | + |
| 5 | +```bash |
| 6 | +❯ fd '.*.md' --max-depth=1 |
| 7 | +CONTRIBUTING.md |
| 8 | +README.md |
| 9 | +TODO.md |
| 10 | +built-in-commands.md |
| 11 | +newsletter.md |
| 12 | +tailwind.md |
| 13 | +``` |
| 14 | + |
| 15 | +I can normalize the whitespace, squishing everything to be separated by single |
| 16 | +spaces by piping it to `xargs`. This is equivalent to call `xargs` with `echo`. |
| 17 | +That is because `echo` is the default command that `xargs` uses when another |
| 18 | +command isn't given. |
| 19 | + |
| 20 | +```bash |
| 21 | +❯ fd '.*.md' --max-depth=1 | xargs |
| 22 | +CONTRIBUTING.md README.md TODO.md built-in-commands.md newsletter.md tailwind.md |
| 23 | + |
| 24 | +❯ fd '.*.md' --max-depth=1 | xargs echo |
| 25 | +CONTRIBUTING.md README.md TODO.md built-in-commands.md newsletter.md tailwind.md |
| 26 | +``` |
| 27 | + |
| 28 | +We can see further evidence of that by using the `-n` flag with `2` to have it |
| 29 | +process results two at a time. In either case, each pair of file names is |
| 30 | +echo'd before the next two are processed. |
| 31 | + |
| 32 | +```bash |
| 33 | +❯ fd '.*.md' --max-depth=1 | xargs -n2 |
| 34 | +CONTRIBUTING.md README.md |
| 35 | +TODO.md built-in-commands.md |
| 36 | +newsletter.md tailwind.md |
| 37 | + |
| 38 | +❯ fd '.*.md' --max-depth=1 | xargs -n2 echo |
| 39 | +CONTRIBUTING.md README.md |
| 40 | +TODO.md built-in-commands.md |
| 41 | +newsletter.md tailwind.md |
| 42 | +``` |
| 43 | + |
| 44 | +See `man xargs` for more details, as well as this [excellent |
| 45 | +article](https://abhinavg.net/2022/06/04/xargs-spaces/). |
0 commit comments