2014-03-12 17:25:30 +00:00
# xargs
2016-10-17 23:02:05 +01:00
> Execute a command with piped arguments coming from another command, a file, etc.
2019-12-24 19:58:08 +00:00
> The input is treated as a single block of text and split into separate pieces on spaces, tabs, newlines and end-of-file.
2021-10-04 13:32:27 +01:00
> More information: <https://pubs.opengroup.org/onlinepubs/9699919799/utilities/xargs.html>.
2014-03-12 17:25:30 +00:00
2019-12-24 19:58:08 +00:00
- Run a command using the input data as arguments:
2014-03-12 17:25:30 +00:00
2016-10-17 23:02:05 +01:00
`{{arguments_source}} | xargs {{command}}`
2014-03-12 17:25:30 +00:00
2019-12-24 19:58:08 +00:00
- Run multiple chained commands on the input data:
`{{arguments_source}} | xargs sh -c "{{command1}} && {{command2}} | {{command3}}"`
- Delete all files with a `.backup` extension (`-print0` uses a null character to split file names, and `-0` uses it as delimiter):
2016-05-16 13:47:00 +01:00
2018-05-18 21:22:49 +01:00
`find . -name {{'*.backup'}} -print0 | xargs -0 rm -v`
2014-03-12 17:25:30 +00:00
2016-10-17 23:02:05 +01:00
- Execute the command once for each input line, replacing any occurrences of the placeholder (here marked as `_` ) with the input line:
2014-03-12 17:25:30 +00:00
2016-10-17 23:02:05 +01:00
`{{arguments_source}} | xargs -I _ {{command}} _ {{optional_extra_arguments}}`
2019-02-09 13:09:48 +00:00
2019-02-24 15:47:41 +00:00
- Parallel runs of up to `max-procs` processes at a time; the default is 1. If `max-procs` is 0, xargs will run as many processes as possible at a time:
2019-02-09 13:09:48 +00:00
`{{arguments_source}} | xargs -P {{max-procs}} {{command}}`