Windows PowerShell ForEach-Object equivalent in Linux
Consumes a list or stream and runs another command for each item.
PowerShell ForEach-Object
xargs COMMAND
xargs COMMAND
xargs COMMAND
Overview
What this command does
On Windows, "PowerShell ForEach-Object" is used to run a command for each input item. On Linux, the closest practical mapping shown here is "xargs COMMAND". Consumes a list or stream and runs another command for each item. This reference is grouped under PowerShell Equivalents → Text Processing.
Windows → Linux context
What changes when you use this on Linux?
Equivalent task, different tool
Windows PowerShell ForEach-Object maps to xargs COMMAND for this task. Treat this as a workflow mapping rather than a character-for-character syntax translation.
Portable across the compared distros
The Debian/Ubuntu, Fedora and Arch rows use the same command in this mapping. Package availability or defaults can still vary, so check the installed tool version when an option behaves differently.
Command-specific caution
Plain xargs splits on whitespace. Use null-delimited input for arbitrary filenames.
For the broader concept behind this mapping, read PowerShell vs Bash.
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
Debian/Ubuntu, Fedora and Arch all use "xargs COMMAND" for this mapping. The command syntax shown here is portable across the three rows, although installed versions and system defaults can still vary.
| 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
Keep learning