Skip to main content

Command referencexargs

xargs command (Linux / bash / sh / zsh)

Linuxbashshzshtextintermediate

What it does

Build and run command invocations from standard input. When people search for the xargs command linux examples, they usually want xargs's everyday flags, safe examples, and how it differs from related tools. Below you'll find syntax, options, and copyable examples for Bash/sh/zsh.

Build and run command invocations from standard input.

Syntax

xargs [OPTION]... [COMMAND [ARG...]]

Common options

Flag / optionMeaning
-0Read NUL-delimited items.
-n NUse at most N input items per invocation.
-P NRun up to N processes in parallel.
-I TOKENReplace a token with each input item.

Practical examples

Safe file removal

find . -name '*.tmp' -print0 | xargs -0 rm

Handles filenames containing whitespace.

Batch arguments

printf '%s
' a b c | xargs -n 2 echo

Runs echo with batches of two items.

Parallel checks

printf '%s
' host1 host2 | xargs -n 1 -P 2 ping -c 1

Checks two hosts concurrently.

Common mistakes

  • Using newline-delimited input for arbitrary filenames instead of `-print0` and `-0`.
  • Running a destructive command without first printing and reviewing generated invocations.

FAQ

What does xargs do in Linux?

Build and run command invocations from standard input.

How do I use xargs from the terminal?

Start with `xargs [OPTION]... [COMMAND [ARG...]]`, then choose only the options needed for the task.

What is a practical xargs example?

Try `find . -name '*.tmp' -print0 | xargs -0 rm` — Handles filenames containing whitespace.

Can I use xargs in a shell script?

Yes. xargs works in Bash, sh, and Zsh, but scripts should handle quoting, errors, and command output explicitly.

Browse the full Linux and Windows command reference to search more bash, CMD, PowerShell, Conda, and GitHub CLI commands.