xargs Patterns
xargs patterns I’ve actually used. Every entry has a date and context.
2026-03-XX: Safe Bulk File Operations
Problem: Delete files found by find without breaking on special characters
Context: Cleaning up old backup files
The Fix:
# WRONG: breaks on spaces and special chars
find . -name "*.bak" | xargs rm
# RIGHT: null-delimited (safest)
find . -name "*.bak" -print0 | xargs -0 rm
# ALSO RIGHT: quote placeholder
find . -name "*.bak" | xargs -I {} rm "{}"
Rule: Always use -print0 | xargs -0 for filenames. No exceptions.
Worklog: Various
2026-03-XX: Parallel Processing
Problem: Compress many files in parallel
Context: Batch log compression
The Fix:
# Compress with 4 parallel processes
find . -name "*.log" -print0 | xargs -0 -P 4 -n 1 gzip
# -P 4 = 4 parallel processes
# -n 1 = one file per process
# -0 = null delimiter (handles ANY filename)
Rule: Use -P for parallel processing, -n to control batch size.
Worklog: Various
2026-03-XX: Confirmation Before Execute
Problem: Delete files but confirm each one
Context: Careful cleanup operations
The Fix:
# Prompt before each execution
find . -name "*.bak" -print0 | xargs -0 -p rm
# -p = prompt (interactive)
Worklog: Various
2026-03-XX: Complex Commands with sh -c
Problem: Run multiple commands per input file
Context: File processing pipeline
The Fix:
# Multiple operations per file
find . -name "*.log" | xargs -I {} sh -c 'echo "Processing: $1"; wc -l "$1"; gzip "$1"' _ {}
# The _ is a placeholder for $0, {} becomes $1
# Quotes around $1 handle spaces in filenames
Rule: Use sh -c '…' _ {} for complex per-file operations.
Worklog: Various
2026-03-XX: Grep Multiple Files in Parallel
Problem: Search many files quickly
Context: Codebase search
The Fix:
# Parallel grep
find . -name "*.adoc" -print0 | xargs -0 -P 4 grep -l "TODO"
# -l = files with matches only
# -P 4 = 4 parallel grep processes
Worklog: Various