xargs is a command that reads items from the standard input, delimited by blanks (protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.

xargs is a very useful command that can be used to read the output of a command and then run a command using that output as an argument. For example, you can use xargs to read the output of the ls command and then run the rm command to remove all the files listed by ls.

Piping to xargs in Linux allows you to build and execute command lines from standard input. Here are some reasons why you would use xargs and what it allows you to do:

  1. Handling Large Output: When a command produces a large amount of output that needs to be passed as arguments to another command, xargs can take the output and convert it into arguments.

  2. Executing Commands on Multiple Files: If you have a list of files or directories and you want to run a command on each of them, xargs can take the list and execute the command on each item.

  3. Efficiency: xargs is more efficient than running a command in a loop because it can pass many arguments at once, reducing the number of times the command is executed.

  4. Handling Special Characters and Spaces: xargs can handle spaces and special characters in filenames or arguments, which can be tricky with other methods.

Combining Commands: It allows you to combine multiple commands in a pipeline seamlessly.

In this tutorial, we will cover the basics of xargs and show you how to use it with examples.

How xargs Works

The xargs command reads items from the standard input and executes a command with those items as arguments. By default, xargs reads items separated by blanks or newlines. It then passes those items as arguments to the specified command.

The basic syntax of the xargs command is as follows:

command | xargs [options] [command]

Here is a breakdown of the command and options:

  • command: The command whose output will be read by xargs.
  • options: Optional flags that modify the behavior of xargs.
  • command: The command that will be executed with the items read from the standard input.

When you run a command and pipe the output to xargs, xargs reads the output and executes the specified command with the output as arguments. If no command is specified, xargs will use the /bin/echo command by default.

Using xargs with Examples

Let’s look at some examples of how to use xargs in Linux.

Example 1: Using xargs with echo

The simplest use of xargs is to read items from the standard input and echo them back. In this example, we will use the echo command to print the items read by xargs:

echo "one two three" | xargs

Output:

one two three

In this example, xargs reads the items “one”, “two”, and “three” from the standard input and passes them as arguments to the echo command, which prints them to the standard output.

Example 2: Using xargs with ls and rm

One common use case for xargs is to read the output of the ls command and then run another command on each item. In this example, we will use xargs to remove all the files listed by ls:

ls | xargs rm

This command will remove all the files in the current directory. Be careful when using the rm command with xargs, as it will delete files without prompting for confirmation.

Example 3: Using xargs with find

Another common use case for xargs is to run a command on a list of files or directories. In this example, we will use the find command to list all the files in the current directory and then run the ls command on each file:

find . -type f | xargs ls

This command will list all the files in the current directory using the ls command.

Example 4: Using xargs with grep

You can also use xargs with the grep command to search for a pattern in a list of files. In this example, we will use xargs to search for the word “example” in all the files in the current directory:

ls | xargs grep "example"

This command will search for the word “example” in all the files in the current directory.

Example 5: Using xargs with multiple commands

You can also use xargs to run multiple commands in a pipeline. In this example, we will use xargs to find all the files in the current directory and then run the wc -l command to count the number of lines in each file:

find . -type f | xargs wc -l

This command will count the number of lines in each file in the current directory.

Example 6: Using xargs with custom delimiters

By default, xargs reads items separated by blanks or newlines. You can use the -d option to specify a custom delimiter. In this example, we will use xargs to read items separated by commas:

echo "one,two,three" | xargs -d, echo

Output:

one two three

In this example, xargs reads the items “one”, “two”, and “three” separated by commas and passes them as arguments to the echo command, which prints them to the standard output.

Example 7: Using xargs with the -I option

You can use the -I option to specify a placeholder for the items read by xargs. In this example, we will use xargs to read a list of files and then run the ls command on each file:

ls | xargs -I {} ls -l {}

This command will run the ls -l command on each file in the current directory.

Example 8: Using xargs with the -P option

You can use the -P option to specify the maximum number of processes that xargs can run in parallel. In this example, we will use xargs to run the sleep command on a list of numbers in parallel:

 
echo "1 2 3 4 5" | xargs -P 2 -n 1 -I {} sleep {} &

This command will run the sleep command on each number in the list in parallel with a maximum of 2 processes at a time.

Conclusion

xargs is a powerful command-line utility that allows you to read items from the standard input and execute a command with those items as arguments. It is a versatile tool that can be used in many different scenarios to process and manipulate data efficiently.