Skip to main content
xargs is a powerful GNU utility that transforms piped data into arguments for another command. Instead of reading from stdin and writing to stdout like typical pipelines, xargs gathers items and appends them as parameters—enabling more flexible shell scripting and one-liners.

Table of Contents

  1. Piping Basics
  2. How xargs Works
  3. Common Use Cases
  4. Handling Special Characters
  5. Additional Resources

Piping Basics

Most shell utilities can read from stdin or files. For example, to count words:
You can redirect or pipe interchangeably:
By contrast, xargs acts like a “bucket”—it collects output from a previous command and then invokes another command, passing those collected items as arguments.
The image illustrates the concept of using the xargs command, showing a "Previously Piped Command" leading to a "Command" represented by a bucket icon.

How xargs Works

Assume file.txt contains:
Piping to echo without xargs preserves newlines in the input stream but not in output:
Under the hood, xargs:
  1. Reads whitespace (spaces, tabs, newlines) by default.
  2. Constructs a single command line by concatenating all items.
  3. Executes that command:

Common Use Cases

Supplying Arguments to Commands

Commands like rm, ls, mkdir or even custom scripts require positional arguments. Instead of writing loops, xargs can automate this:
The image illustrates the concept of using the xargs command, showing a structure with "Command" and "Value" sections, and examples like rm, ls, echo, and mkdir.

Creating Multiple Directories

Generate directories from a whitespace-separated list:

Table of Handy xargs Examples


Handling Special Characters

By default, xargs splits on any whitespace. Filenames containing spaces or special characters may break. Use -0 with NUL-separated data (e.g., find . -print0 \| xargs -0) to handle arbitrary names safely.
The image is a diagram explaining the xargs command, indicating it functions like a plain echo, receives input and positions, and is used by other commands.

Additional Resources


Watch Video