awk examples
On Linux, xargs takes the output of the previous stage in a pipeline as standard input and uses it to run the next command.
Input comes in as $N
echo a | xargs bash -c 'echo $0' # $0 is the first input
# a
echo a b | xargs bash -c 'echo $0 $1' # $1 is the second input
# a b
To feed a txt file into xargs one line at a time and run a command, use -L 1.
ls > ls.txt # create ls.txt
cat ls.txt | xargs -L 1 bash -c 'echo $0' # ls.txt # print ls.txt one line at a time with echo
# Applications
# Desktop
# Documents
# Downloads
# Library
# Movies
# Music
# OneDrive
# Pictures
# Public
# build
# dev
# file-of-keys
# ls.txt
If you want to pass a variable into the command that xargs runs
test=test # set the variable test
echo 1 | xargs -P8 -n1000 bash -c 'echo $0 '$test''
# 1 test
Miscellaneous
What -P8 means: run 8 processes to handle this command.
What -n1000 means: take up to 1000 arguments from standard input for each command execution.
References
Manual: https://man7.org/linux/man-pages/man1/xargs.1.html
Useful examples: https://shapeshed.com/unix-xargs/ https://phoenixnap.com/kb/xargs-command https://www.thegeekstuff.com/2013/12/xargs-examples/
20211026
Leave a comment