← All commands
Topic hub PowerShell Equivalents
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
Debian/Ubuntu same command
xargs COMMAND
Fedora same command
xargs COMMAND
Arch same 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 optionUse
printf "%s\n" one two | xargs -n1 echoProcess one item at a time
find . -name "*.tmp" -print0 | xargs -0 rm -vProcess filenames safely
while IFS= read -r line; do printf "%s\n" "$line"; done < file.txtUse 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.

DistributionPackage manager / baseEquivalent commandDifference 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

Related PowerShell Equivalents commands