Bash
xargs
xargs commands for building command lines from input.
25 commands
Windows
MacOS
Linux
#command-building
#pipeline
Basic Usage
Pass input as arguments
echo "a b c" | xargs echo
Delete files found by find
find . -name "*.log" | xargs rm
Download URLs from a file
cat urls.txt | xargs wget
Pass one argument at a time
echo "1 2 3" | xargs -n 1 echo
Count lines in all text files
ls *.txt | xargs wc -l
Delimiter Control
Use colon as delimiter
echo "a:b:c" | xargs -d ':'
Handle filenames with spaces
find . -name "*.log" -print0 | xargs -0 rm
Use newline as delimiter
cat list.txt | xargs -d '\n' echo
Use null byte delimiter
printf "a\0b\0c" | xargs -0 echo
Parallel Execution
Download 4 files in parallel
cat urls.txt | xargs -P 4 -n 1 wget
Convert in parallel
find . -name "*.png" | xargs -P 8 -I {} convert {} {}.jpg
Run 5 jobs at once
seq 10 | xargs -P 5 -I {} sh -c 'echo start {}; sleep 1'
Parallel SSH commands
cat hosts.txt | xargs -P 10 -I {} ssh {} uptime
Confirmation
Prompt before executing each command
echo "*.bak" | xargs -p rm
Print each command before executing
find . -name "*.tmp" | xargs -t rm
Interactive confirmation mode
echo "test" | xargs -p echo
Common Patterns
Search in found files
find . -name "*.js" | xargs grep "TODO"
Run command for each user
cut -d: -f1 /etc/passwd | xargs -I {} id {}
Group into lines of 10
seq 1 100 | xargs -n 10 echo
Archive old files
find . -type f -mtime +30 | xargs -I {} mv {} /archive/
Archive changed files
git diff --name-only | xargs tar czf patch.tar.gz
Show header of each CSV
ls *.csv | xargs -I {} sh -c 'head -1 "{}"'
Quick Commands
Safely delete files with spaces in names
find . -name "*.log" -print0 | xargs -0 rm
Download files in parallel with 4 workers
cat urls.txt | xargs -P 4 -n 1 wget
Search for pattern in found files
find . -name "*.js" | xargs grep "TODO"