Where to start First steps track · 15 stages
Linux Labs

xargs — building arguments from a stream

cheat sheet · GNU findutils https://lab.dimsanych.ru/en/wiki/xargs/

Cheat sheet

  • find . -name '*.x' -print0 | xargs -0 rm safe (spaces)
  • ... | xargs -I{} cp {} /dst/ substitute {}
  • ... | xargs -P4 -n1 cmd in parallel
  • ... | xargs -rt cmd skip if empty + show it
  • ... | xargs -n1 one argument at a time

Options

Without -0, xargs splits the input on spaces and newlines — a file name with a space turns into two separate arguments. The safe path is a NUL delimiter on both sides of the pipeline: find -print0 (or grep -Z) together with xargs -0. -I {} implicitly limits the call to one argument at a time, like -n1, because you can't substitute several values into one placeholder at once.

bash
... | xargs -n1 cmd               # one argument per call
... | xargs -I{} cp {} /backup/   # substitute {} (often -I implies -n1)
... | xargs -P4 -n1 cmd           # in parallel, 4 processes
cat urls.txt | xargs -P8 -I{} curl -sO {}   # download 8 in parallel
... | xargs -rt cmd               # skip if empty + show the command
Loading…