Advanced Bash Scripting

Streams

Xargs

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:

echo "How many words are in this text?" | wc -w
# 7

You can redirect or pipe interchangeably:

sort file.txt
sort < file.txt

cat file.txt
cat < file.txt

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:

file
content
to demonstrate
xargs functionality

Piping to echo without xargs preserves newlines in the input stream but not in output:

cat file.txt | xargs echo
# file content to demonstrate xargs functionality

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:
echo file content to demonstrate xargs functionality

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`.

# Prepend a custom message to file.txt contents
cat file.txt \
  | xargs echo "The contents of file.txt passed by xargs are:"
# The contents of file.txt passed by xargs are: file content to demonstrate xargs functionality

Creating Multiple Directories

Generate directories from a whitespace-separated list:

echo "dir1 dir2 dir3" \
  | xargs mkdir
ls
# dir1  dir2  dir3

Table of Handy xargs Examples

Use CaseCommand Example
Remove log filesfind . -name '*.log' | xargs rm -f
Create directoriesecho "a b c" | xargs mkdir
Parallel SSH sessionscat hosts.txt | xargs -P4 -I{} ssh {} hostname

Handling Special Characters

Warning

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

Watch video content

Previous
Exit code
Next
Eval