Reads items from standard input (stdin) and builds command-line arguments from them.
stdin → argument list → command execution- 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
xargscommand 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 5Echo 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 5specify 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 secondsIf maxprocs is set to 0, xargs will run as many processes as possible.
- Regular xargs splits on whitespace.
- If filenames contain spaces, it's dangerous.
- Correct way:
find . -name "*.log" -print0 | xargs -0 rm
- Here:
-print0separates results with null character-0tells xargs to split on null