Parameters and Usage Examples
The xargs
command provides options to customize the way it constructs and executes commands.
Option Parameter
command | xargs [Options] command
-n
: Specify the maximum number of parameters per row.
-P
: Specifies the maximum number of processes to run in parallel.
-I
: Replaces occurrences of the specified string with input values.
Example Usage
1. Limiting Arguments per Command (-n)
The -n
option limits the number of arguments passed to each command.
Input:
cat /home/labex/project/example.txt | xargs -n 1 echo
Output:
apple
orange
banana
2. Running Commands in Parallel (-P, -I)
In this example, the -P 2
option allows xargs
to run commands in parallel, and -I {}
specifies a placeholder for the input. Each line is processed independently in parallel.
Input:
cat /home/labex/project/example.txt | xargs -P 2 -I {} echo "Fruit: {}"
Output:
Fruit: apple
Fruit: orange
Fruit: banana