Skip to content

Mastering xargs: High-Performance Parallel Command Execution

Master xargs for ultra-fast parallel execution in Linux. Includes real-world examples for file handling, downloads, batching, and high-speed network scanning.


Show limits on command-line length

xargs --show-limits

Delete files with whitespace in filename (e.g. hello 2026)

find . -name "*.c" -print0|xargs -0 rm -rf

Print command along with output

cat ~/.bashrc|xargs -t abcd    

Prompt before execution

echo {0..20} |xargs -p -n 20

Display 3 columns per line

echo {0..20}| xargs -n 3

Prompt commands before running commands

ls|xargs -L1 -p head

Set tab as delimiter (default:space)

xargs -d\t

Run 5 commands at same time

seq 20 | xargs -a - -I{} -P5 bash -c 'echo {}; sleep 1"

Run 10 curl commands in Parallel via xargs

NUM="10";seq ${NUM}|time xargs -I % -n1 -P${NUM} curl -sL ifconfig.co

Similiar to ls -1, list files line by line

ls |xargs -n1

Similiar to ls -1, list files line by line in two seperated columns

ls |xargs -n2

Deleting Files Found by find

find . -name "*.log" -print0 | xargs -0 rm -f

Running a command in batches

seq 1 100 | xargs -n 10 echo

Parallel Downloads using curl

cat urls.txt | xargs -n 1 -P 4 curl -O   

Prompting for Confirmation

find . -type f -name "*.bak" | xargs -p rm

Always use -print0 with find and -0 with xargs

find . -name "*.log" -print0 | xargs -0 rm

Rename Files by Changing Extensions

find . -name "*.txt" | xargs -I {} sh -c 'mv "$1" "${1%.txt}.md"' _ {}

Read Input from a File Directly

  • This reads each line of list.txt and passes it to rm.
xargs -a list.txt rm

Archiving Files

cat file_list.txt | xargs tar -czf archive.tar.gz

find Command with -exec

  • This directly removes all temporary files without needing to pipe to xargs.
find . -name "*.tmp" -exec rm {} \;

Parallel Execution with xargs

cat urls.txt | xargs -n 1 -P 4 wget

Run Multiple Commands with xargs

echo "file1 file2" | xargs -I {} sh -c 'cp {} backup/ && echo {} copied'

Using xargs with find and Null Terminator

find . -type f -name "*.jpg" -print0 | xargs -0 rm

Copy Multiple Files to a Directory

  • -I {} tells xargs to replace each input line with {} in the command.
cat filelist.txt | xargs -I {} cp {} /backup/

Handling filenames with spaces

  • Sometimes, filenames may contain spaces or special characters. In such cases, use the -0 option in conjunction with find to ensure proper handling:
find . -name '*.tmp' -print0 | xargs -0 rm

Portscanning

Probably the fastest network scanner on earth

seq 1 254 | xargs -P 254 -I{} bash -c 'function ping_status() { 
  network="192.168.1.$1"; 
  if ping -c 1 -W 0.5 $network &> /dev/null; then 
    printf "IPV4: %-13s is \e[1;32mup\e[0m\n" "$network"; 
  fi 
}; ping_status {}'

Slower portscanner but still faster and a shorter oneliner for port scanning local network

seq 1 254 | xargs -P 254 -I{} ping -c1 -n -W1 192.168.1.{} \
  | awk '/bytes from/ {gsub(/:$/, "", $4); printf "IPV4: %-13s is \033[1;32mup\033[0m\n", $4}'

Fast portscanner without any colors

time seq 1 254 | xargs -P 254 -I{} sh -c '
ip="192.168.1.{}"
ping -c1 -n -W 0.5 "$ip" >/dev/null 2>&1 && echo "$ip"
'

Another version of portscanning local network

time seq 1 254 | xargs -P 254 -I{} sh -c '
ip="192.168.1.{}"
ping -c1 -n -W 0.5 "$ip" >/dev/null 2>&1 && echo "$ip"
' | sort -V | awk '{printf "IPV4: %-13s is \033[1;32mup\033[0m\n",$1}'

Slower but we now using printf-loop rather then awk

time seq 1 254 | xargs -P 254 -I{} sh -c '
ip="192.168.1.{}"
ping -c1 -n -W 0.5 "$ip" >/dev/null 2>&1 && echo "$ip"
' | while IFS= read -r ip; do
  printf "IPV4: %-13s is \033[1;32mup\033[0m\n" "$ip"
done