Terminal GuideTerminal Guide

xargs Command Guide

xargs builds and executes commands from standard input. Learn how to use it effectively in command pipelines.

6 min readLast updated: 2024
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Basic

cmd | xargsPass as arguments
cmd | xargs echoSingle line output
cmd | xargs -n 1One arg per cmd

Options

-I {}Placeholder
-0Null delimiter
-P 4Parallel (4 procs)

Common

find . | xargs rmDelete found files
find . -print0 | xargs -0Handle spaces
cat list | xargs -I {} cp {} dir/Copy each

Debug

-tPrint commands
-pPrompt before run
--no-run-if-emptySkip if empty

Downloadable Image Preview

Failed to generate preview

Basic Usage

xargs reads items from standard input and executes a command with those items as arguments.

bash
# Basic syntax
command | xargs another_command

# Example: remove files listed in a text file
cat files.txt | xargs rm

By default, xargs passes all input as arguments to the command:

bash
echo "file1 file2 file3" | xargs ls -l
# Equivalent to: ls -l file1 file2 file3

Common Options

xargs Options

-I {}Replace {} with input item
-n NUse at most N arguments per command
-P NRun up to N processes in parallel
-0Input items are null-terminated
-pPrompt before executing each command
-tPrint command before executing
-d delimUse custom delimiter

Placeholder Replacement (-I)

Use -I to specify where input should be placed.

bash
# Rename files with prefix
ls *.txt | xargs -I {} mv {} backup_{}

# Create directories from file
cat dirs.txt | xargs -I {} mkdir -p {}

# Download files from URL list
cat urls.txt | xargs -I {} curl -O {}
Tip
-I {} is commonly used, but you can use any placeholder string, like -I @.

Limiting Arguments (-n)

Control how many arguments are passed to each command invocation.

bash
# Process one file at a time
echo "file1 file2 file3" | xargs -n 1 ls -l

# Process two at a time
echo "a b c d e f" | xargs -n 2 echo

Parallel Execution (-P)

Run commands in parallel for faster processing.

bash
# Compress files using 4 parallel processes
find . -name "*.log" | xargs -P 4 -I {} gzip {}

# Download multiple files in parallel
cat urls.txt | xargs -P 10 -I {} curl -O {}
Info
Use -P 0 to run as many processes as possible (use with caution).

Handling Special Characters (-0)

Handle filenames with spaces or special characters safely.

bash
# With find -print0 for safe handling
find . -name "*.txt" -print0 | xargs -0 rm

# Process files with spaces in names
find . -type f -print0 | xargs -0 -I {} cp {} backup/
Warning
Always use -0 with find -print0 when filenames might contain spaces or special characters.

Practical Examples

Find and delete old files

bash
find /tmp -type f -mtime +7 -print0 | xargs -0 rm -f

Search for pattern in files

bash
find . -name "*.js" | xargs grep "console.log"

Count lines in multiple files

bash
find . -name "*.py" | xargs wc -l

Change permissions recursively

bash
find . -type f -name "*.sh" | xargs chmod +x

Batch convert images

bash
ls *.png | xargs -I {} convert {} -resize 50% resized_{}

Kill processes by name

bash
pgrep -f "process_name" | xargs kill

Git: remove untracked files

bash
git ls-files --others --exclude-standard | xargs rm

Archive files listed in a text file

bash
cat files.txt | xargs tar -cvzf archive.tar.gz

Debugging with -t and -p

bash
# Show commands before execution
find . -name "*.tmp" | xargs -t rm

# Prompt before each execution
find . -name "*.bak" | xargs -p rm

xargs vs exec in find

Both can execute commands on found files, but they work differently.

bash
# Using find -exec (one process per file)
find . -name "*.txt" -exec rm {} \;

# Using xargs (batches files, more efficient)
find . -name "*.txt" | xargs rm

# Using find -exec + (batches like xargs)
find . -name "*.txt" -exec rm {} +
Tip
xargs is generally more efficient than find -exec ... \; because it batches arguments. Use find -exec ... + for similar batching behavior.

Summary

xargs is essential for building powerful command pipelines. Key takeaways:

  • Use -I {} for placeholder replacement
  • Use -n N to limit arguments per command
  • Use -P N for parallel execution
  • Use -0 with find -print0 for special characters
  • Use -t or -p for debugging

Related Articles