How to Use the xargs Command on Linux

Share
  • August 1, 2019

Fatmawati Achmad Zaenuri/Shutterstock

Need to string some Linux commands together, but one of them doesn’t accept piped input? xargs can take the output from one command and send it to another command as parameters.

All of the standard Linux utilities have three data streams associated with them. They are the standard input stream (stdin), the standard output stream (stdout), and the standard error stream (stderr).

These streams work with text. We send input (stdin) to a command using text, and the response (stdout) is written to the terminal window as text. Error messages are also written to the terminal window as text (stderr).

One of the great features of Linux and Unix-like operating systems is the ability to pipe the stdout output from one command into the stdin input of a second command. The first command doesn’t care that its output is not going to a terminal window, and the second command doesn’t care that its input isn’t coming from a keyboard.

Although all of the Linux commands have the three standard streams, not all of them accept another command’s stdout as input to their stdin. That means you can’t pipe input to them.

xargs is a command for building execution pipelines using the standard data streams. By using xargs we can make commands such as echo, rm, and mkdir accept standard input as arguments.

The xargs Command

xargs will accept piped input. It can also accept input from a file. xargs uses that input as parameters for the commands we’ve told it to work with. If we do not tell xargs to work with a specific command it will default to use echo.

We can use that to demonstrate how xargs will always generate a single line of output, even from multi-line input.

If we use the -1 (list one file per line) option with ls, we get a single column of filenames.

ls -1 ./*.sh

Read the remaining 71 paragraphs

Source : How to Use the xargs Command on Linux