Back to the command reference

xargs

ShellAdvanced In simulator In real Linux

Build command lines from standard input

Name: xargs = extended arguments

You can practise it right away in the simulator, and use it in real Linux mode too.

Description

xargs reads items from standard input (separated by whitespace by default) and runs a command with them as arguments. It bridges commands that take arguments rather than standard input, splitting into several runs when the list is too long.

When file names may contain spaces, pair find -print0 with xargs -0.

Syntax

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

Common options

-n / --max-args NUMMax arguments per command
-L / --max-lines NUMUse NUM input lines per command
-I {}Replace {} with each item
-0 / --nullItems separated by NUL (with find -print0)
-d / --delimiter DELIMSplit the input at DELIM (-d '\n': one item per line, spaces kept)
-P / --max-procs NUMRun up to NUM commands at once (in parallel)
-r / --no-run-if-emptyDo not run the command when there is no input
-t / --verboseShow each command before running it
-p / --interactivePrompt before running

Examples

  • $ find . -name "*.tmp" | xargs rm

    Delete every file found

    Explain
  • $ cat urls.txt | xargs -n 1 curl -O

    Download each URL in a list

    Explain
  • $ ls *.jpg | xargs -I {} cp {} backup/

    Copy each file into backup/

    Explain

Related