xargs

Build and run command lines from input.

What it does

xargs reads items from standard input and turns them into arguments for another command. It is commonly used when one command produces a list of items and another command needs those items as command-line arguments.

How it works (mechanical)

Quick Start

# Echo items as arguments
printf "one\ntwo\nthree\n" | xargs echo

# Remove files listed in a file
cat filelist.txt | xargs rm

# Use find with xargs
find . -name "*.log" | xargs ls -lh

Core Options (Most Useful Flags)

-n 1        Use one input item per command
-I {}       Replace {} in the command with each input item
-0          Read null-separated input (safe for spaces/newlines)
-d '\n'     Use newline as delimiter (GNU xargs)
-p          Prompt before running each command
-t          Print command before executing it
-r          Do nothing if input is empty (GNU xargs)
-P 4        Run up to 4 commands in parallel

Why xargs matters

Many commands do not read filenames or arguments from standard input in the way people expect. xargs bridges that gap by taking piped input and converting it into proper command arguments.

12 Practical Examples

# 1) Basic argument building
printf "apple\nbanana\npear\n" | xargs echo
# 2) One item per command
printf "a\nb\nc\n" | xargs -n 1 echo
# 3) Safer with filenames containing spaces (GNU/Linux common pattern)
find . -type f -print0 | xargs -0 ls -lh
# 4) Delete files found by find
find . -type f -name "*.tmp" -print0 | xargs -0 rm
# 5) Use replacement string
printf "file1\nfile2\n" | xargs -I {} echo "Processing {}"
# 6) Search inside files found by find
find . -type f -name "*.txt" -print0 | xargs -0 grep -n "ERROR"
# 7) Copy many files to another directory
find . -type f -name "*.conf" -print0 | xargs -0 -I {} cp "{}" /tmp/config-backup/
# 8) Preview commands before running
printf "a\nb\nc\n" | xargs -t echo
# 9) Prompt before execution
printf "old1.log\nold2.log\n" | xargs -p rm
# 10) Run in parallel
printf "1\n2\n3\n4\n" | xargs -n 1 -P 4 echo "Job"
# 11) Use newline as delimiter
printf "one\ntwo\nthree\n" | xargs -d '\n' echo
# 12) Build tar input list
find . -type f -name "*.txt" -print0 | xargs -0 tar -rvf archive.tar

Very Useful Real-World Patterns

# Find logs, then grep inside them
find /var/log -type f -name "*.log" -print0 | xargs -0 grep -H "error"
# Remove old backup files safely
find /backups -type f -name "*.bak" -print0 | xargs -0 rm
# Count lines across many files
find . -type f -name "*.py" -print0 | xargs -0 wc -l
# Show disk usage for matched files
find . -type f -name "*.iso" -print0 | xargs -0 du -h

Notes & Gotchas

Safety Pattern

# SAFEST workflow:
# 1) preview what you found
find . -type f -name "*.tmp" -print0 | xargs -0 ls -l

# 2) only then run the destructive command
find . -type f -name "*.tmp" -print0 | xargs -0 rm

Preview first, act second. That habit avoids accidents.

Historical Context

xargs became important in Unix because command-line length is limited, and many utilities needed a way to accept large lists of input items efficiently. It remains a classic tool for building pipelines that act on many files or arguments.

Modern Equivalent

Some modern tools reduce the need for xargs by supporting recursive search or direct execution, and find -exec ... + can often replace it. Still, xargs remains one of the clearest and most flexible tools for argument-building pipelines.

Related Commands