The xargs Utility
Learn about the xargs utility in detail.
We'll cover the following...
xargs
We can use a pipeline and get the -exec
action behavior. We should apply the xargs
utility for that.
Let’s go through an example. Let’s suppose that we want to find a pattern in the contents of the found files. In this case, the grep
utility should receive file paths, but not the plain text. We can apply the pipeline and xargs
to solve this task. The solution looks like this:
find ~ -type f | xargs grep "bash"
This command cannot handle files whose names contain spaces and line breaks. We will learn how to solve this problem in later lessons.
Run the commands discussed in this lesson in the terminal below.
The xargs
utility constructs a command. The utility takes two things as the input: parameters and text data from the input stream. The parameters come in the first place in the constructed command. Then, all data from the input stream follows.
Let’s come back to our example. Let’s suppose that the find
utility finds the ~/.bashrc
file. The pipeline passes the file path to the xargs grep "bash"
call. The xargs
utility receives two parameters in this call: grep
and “bash.”
Therefore, it constructs the command that starts with these two words. The result is grep "bash"
.
Then, ...