xargs, pipes and exec:
First off, exec is nothing like the first two. exec does a variety of things from executing a command without returning(the shell is replaced by the new command), as well as opening/closing files.
pipes transfer the output of one program into the input of another. Without the pipe or redirection, they'd be trying to read from your keyboard and trying to print to your screen, but you can feed them data from any source and send their output anywhere you like. Shell utilities are intended to be flexible that way.
Code:
# useless example echo asdf | cat
'echo' prints to standard output, 'cat' reads from standard input, the pipe joins them together so 'asdf' is read by cat and printed.
But what about this:
Code:
# This won't print anything echo something | echo
echo does not read from standard input, it takes commandline parameters and nothing but parameters. So how do we translate from pipe into that?
This is what xargs is for.
Code:
echo a b c d | xargs command
is equivalent to command a b c d. Whenever you need to translate from a pipe or file into commandline arguments, xargs serves.
Code:
# This prints 'something' into xargs' input, causing xargs to run 'echo something'. echo something | xargs echo
Let's consider an example of exec:
$ find . -name "*.txt" -exec ls -l {} \;
The above command will create multiple process and generate the result. Suppose there are 3 text files named file1.txt, file2.txt, & file3.txt. The above command will start below 3 commands:
ls -l file1.txt
ls -l file2.txt
ls -l file3.txt
To restrict to 1 thread or 1 process, you can use xargs or +symbol. For example:
$ find . -name "*.txt" | xargs ls -l
or
$ find . -name "*.txt" -exec ls -l {} \+
However, you can mention the parallelism in xargs with -P option. The above command will result in:
ls -l file1.txt file2.txt file3.txt
Only 1 thread.
Credit: unix.com