- some shell commands can not take stdin and use that as a parameter, to do this you need another program to help with this and that is what
xargs
command does. - xargs allows to take standard input and pass that along as an argument into another command.
- stdin -> argument
$ seq 5 | echo # echo can not take stdin as parameter
$ seq 5 | xargs echo # xargs can help
1 2 3 4 5
Echo the command to be executed to standard error immediately before it is executed.
$ seq 5 | xargs -t echo
echo 1 2 3 4 5
1 2 3 4 5
specify a placeholder, basically an alias for the arguments.
$ ls | xargs -I {} echo /home/dt/{}
/home/dt/Applications
/home/dt/Desktop
/home/dt/Documents
/home/dt/Downloads
...
you can specify anything as the placeholder, as the alias
$ ls | xargs -I zzz echo /home/dt/zzz
/home/dt/Applications
/home/dt/Desktop
/home/dt/Documents
/home/dt/Downloads
...
the replstr of alias has max-length limit, default is 255, it the length of replstr is greater than its limit, the alias
will not match, to solve this problem, you may specify the max-lenght
xargs -S 10240 -I {} ...
Set the maximum number of arguments taken from standard input for each invocation of utility.
$ ls | xargs
Applications Desktop Documents Downloads Google Drive Library Movies Music Pictures Public go
$ ls | xargs -n 1
Applications
Desktop
Documents
Downloads
...
Parallel mode: run at most maxprocs
invocations of utility at once. If maxprocs
is set to 0, xargs will run as many processes as possible.
$ seq 5 | xargs -n 1 -P 2 bash -c 'echo $0;sleep 1'
# execution will took 3 seconds
If maxprocs
is set to 0, xargs will run as many processes as possible.