Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

xargs

Turn lines from a pipe into arguments of a command — and run it in batches or in parallel.

Niveau
DevOps-tool
Groep
Tekst en zoeken
Root nodig
Nee
Pakket
findutils

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

Many commands do not read standard input; they need arguments. xargs reads words or lines from the pipe and puts them on the command line of another command. With -P it runs many copies at the same time — a simple way to make scripts faster.

Use -0 together with find -print0 so file names with spaces work.

Zo schrijf je het

COMMAND | xargs [OPTIONS] [COMMAND [ARG ...]]

Opties 11

Meest gebruikt

Options

-0, --null
Items end with a NUL character (use with find -print0).
-I {}
Replace {} in the command with each item; one run per item.
-n N
At most N arguments per run.
-P N
Run N processes at the same time (0 = as many as possible).
-L N
At most N lines per run.
-d DELIM
Items end with DELIM (for example -d '\n').
-r, --no-run-if-empty
Do not run the command when there is no input.
-t, --verbose
Print each command before it runs.
-p, --interactive
Ask before each run.
-a FILE
Read the items from FILE.
--max-chars N
Limit the length of the command line.

Voorbeelden 11

$ als gewone gebruiker# als root (of met sudo)

  1. 1Delete old log files safely (spaces in names work)

    find /var/log/app -name '*.log' -mtime +14 -print0 | xargs -0 -r rm -f
  2. 2One command per item with {}

    cat hosts.txt | xargs -I {} ssh {} 'uptime'
  3. 3Pull many images in parallel (4 at a time)

    cat images.txt | xargs -P 4 -n 1 docker pull
  4. 4Delete all Evicted pods

    kubectl get pods -A --field-selector=status.phase=Failed -o jsonpath='{range .items[*]}{.metadata.namespace} {.metadata.name}{"\n"}{end}' | xargs -r -n 2 sh -c 'kubectl delete pod -n "$0" "$1"'
  5. 5grep in the files that find found

    find . -name '*.yaml' -print0 | xargs -0 grep -l 'image: nginx'
  6. 6See the commands before they run

    echo web1 web2 | xargs -t -n 1 ping -c 1
  7. 7Check many URLs fast

    xargs -P 8 -I {} curl -s -o /dev/null -w '%{http_code} {}\n' {} < urls.txt
  8. 8Remove merged Git branches

    git branch --merged main | grep -v 'main$' | xargs -r git branch -d
  9. 9Archive files in batches of 100

    find data -type f -print0 | xargs -0 -n 100 tar -rf data.tar
  10. 10Lines (not words) as items

    printf 'my file.txt\nother file.txt\n' | xargs -d '\n' ls -l
  11. 11Ask before each delete

    ls *.tmp | xargs -p rm

Let op

  • Without -0/-d '\n' xargs splits on spaces and quotes: "my file" becomes two items.
  • Without -r GNU xargs runs the command once with no arguments when the input is empty — dangerous with rm.
  • Output of parallel runs (-P) can mix; write each result to its own file if order matters.

Tips om te onthouden

Exitcodes

0All runs exited 0.
123A run exited with 1–125.
124A run exited with 255.
125A run was killed by a signal.
126The command cannot run.
127The command was not found.

Volledige handleiding

Deze pagina legt het commando uit in eenvoudige woorden. De originele handleiding heeft elk detail.

In je terminal: man xargs