Skip to main content

xargs

The xargs command reads items from standard input (such as output from another command) and executes a specified command using those items as arguments. It helps bridge commands that produce lists with commands that expect arguments.

Basic Syntax

xargs [OPTION]... COMMAND [INITIAL-ARGS]...

Common Options

OptionDescription
-0, --nullInput items are terminated by a null character instead of whitespace (useful with find -print0).
-n MAX-ARGSUse at most MAX-ARGS arguments per command line.
-I REPLACE-STRReplace occurrences of REPLACE-STR in the initial arguments with names read from standard input.
-p, --interactivePrompt the user about whether to run each command line.

Real-world Examples

  1. Delete all .tmp files found by find:

    find . -name "*.tmp" | xargs rm
  2. Handle filenames with spaces safely:

    find . -name "*.log" -print0 | xargs -0 rm
  3. Pass arguments to a specific position using -I:

    cat urls.txt | xargs -I {} wget {}
  4. Process files in batches of 3:

    ls | xargs -n 3 echo "Batch:"