Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

xargs

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

Level
DevOps tool
Group
Text and search
Needs root
No
Package
findutils

What it does

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.

How to write it

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

Options 11

Most used

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.

Examples 11

$ run as a normal user# run as root (or with 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

Watch out

  • 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 to remember

Exit codes

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.

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man xargs