xargs Command Guide
xargs builds and executes commands from standard input. Learn how to use it effectively in command pipelines.
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
cmd | xargsPass as argumentscmd | xargs echoSingle line outputcmd | xargs -n 1One arg per cmdOptions
-I {}Placeholder-0Null delimiter-P 4Parallel (4 procs)Common
find . | xargs rmDelete found filesfind . -print0 | xargs -0Handle spacescat list | xargs -I {} cp {} dir/Copy eachDebug
-tPrint commands-pPrompt before run--no-run-if-emptySkip if emptyDownloadable Image Preview
Basic Usage
xargs reads items from standard input and executes a command with those items as arguments.
# Basic syntax
command | xargs another_command
# Example: remove files listed in a text file
cat files.txt | xargs rmBy default, xargs passes all input as arguments to the command:
echo "file1 file2 file3" | xargs ls -l
# Equivalent to: ls -l file1 file2 file3Common Options
xargs Options
| -I {} | Replace {} with input item |
| -n N | Use at most N arguments per command |
| -P N | Run up to N processes in parallel |
| -0 | Input items are null-terminated |
| -p | Prompt before executing each command |
| -t | Print command before executing |
| -d delim | Use custom delimiter |
Placeholder Replacement (-I)
Use -I to specify where input should be placed.
# 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 {}-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.
# 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 echoParallel Execution (-P)
Run commands in parallel for faster processing.
# 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 {}-P 0 to run as many processes as possible (use with caution).Handling Special Characters (-0)
Handle filenames with spaces or special characters safely.
# 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/-0 with find -print0 when filenames might contain spaces or special characters.Practical Examples
Find and delete old files
find /tmp -type f -mtime +7 -print0 | xargs -0 rm -fSearch for pattern in files
find . -name "*.js" | xargs grep "console.log"Count lines in multiple files
find . -name "*.py" | xargs wc -lChange permissions recursively
find . -type f -name "*.sh" | xargs chmod +xBatch convert images
ls *.png | xargs -I {} convert {} -resize 50% resized_{}Kill processes by name
pgrep -f "process_name" | xargs killGit: remove untracked files
git ls-files --others --exclude-standard | xargs rmArchive files listed in a text file
cat files.txt | xargs tar -cvzf archive.tar.gzDebugging with -t and -p
# Show commands before execution
find . -name "*.tmp" | xargs -t rm
# Prompt before each execution
find . -name "*.bak" | xargs -p rmxargs vs exec in find
Both can execute commands on found files, but they work differently.
# 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 {} +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 Nto limit arguments per command - Use
-P Nfor parallel execution - Use
-0withfind -print0for special characters - Use
-tor-pfor debugging
Related Articles
find Command Guide
Locate files and directories
grep Command: Practical Examples & Usage Guide
Master grep with practical examples for developers. Learn pattern matching, regex, and real-world use cases.
sed Command Guide
Stream editor for text transformation