Windows
Linux
PowerShell Equivalents
Text Processing
Windows PowerShell ForEach-Object equivalent in Linux
Consumes a list or stream and runs another command for each item.
Windows
→
PowerShell ForEach-Object
Linux equivalents
xargs COMMAND
xargs COMMAND
xargs COMMAND
Overview
What this command does
Consumes a list or stream and runs another command for each item.
Migration tip:
xargs -0 safely handles filenames containing spaces and newlines when paired with null-delimited input such as find -print0.Practical tasks
Common use cases
Run a command for each input item
Consumes a list or stream and runs another command for each item.
printf "%s\n" one two | xargs -n1 echo
Process one item at a time
find . -name "*.tmp" -print0 | xargs -0 rm -v
Process filenames safely
while IFS= read -r line; do printf "%s\n" "$line"; done < file.txt
Use a shell loop
Ready to run
Copyable Linux commands
Review paths, device names, package names and permissions before running any command.
printf "%s\n" one two | xargs -n1 echo
find . -name "*.tmp" -print0 | xargs -0 rm -v
while IFS= read -r line; do printf "%s\n" "$line"; done < file.txt
Walkthrough
Examples with explanations
printf "%s\n" one two | xargs -n1 echo # run a command for each item
find . -name "*.tmp" -print0 | xargs -0 rm -v # handle filenames safely
while IFS= read -r line; do printf "%s\n" "$line"; done < file.txt # process a file line by line
Reference
Common options and variations
| Command or option | Use |
|---|---|
printf "%s\n" one two | xargs -n1 echo | Process one item at a time |
find . -name "*.tmp" -print0 | xargs -0 rm -v | Process filenames safely |
while IFS= read -r line; do printf "%s\n" "$line"; done < file.txt | Use a shell loop |
Distro differences
Debian/Ubuntu vs Fedora vs Arch
This command is the same on Debian/Ubuntu, Fedora and Arch Linux when the relevant tool is installed.
| Distribution | Package manager / base | Equivalent command | Difference to notice |
|---|---|---|---|
| Debian/Ubuntu | same command | xargs COMMAND |
Same command on this distribution. |
| Fedora | same command | xargs COMMAND |
Same command on this distribution. |
| Arch | same command | xargs COMMAND |
Same command on this distribution. |
Important
What to watch out for
Plain xargs splits on whitespace. Use null-delimited input for arbitrary filenames.
Keep learning