xargs – A Filter for Passing Arguments to Other Commands

The xargs command is a filter that is used to pass arguments to other commands. It reads items from standard input, separated by whitespace or newlines, and executes a command for each item. It is particularly useful when dealing with commands that do not accept input from standard input.

Overview

The syntax for using xargs is as follows:

command | xargs [options] command

In this syntax, command is the command that will be executed for each item read by xargs. The first command before the pipe symbol (|) generates a list of items that will be passed to xargs.

For example, to delete all files in the current directory that end with .txt, you can use the following command:

ls *.txt | xargs rm

This will list all files in the current directory that end with .txt, and pass each file name to the rm command using xargs.

Another example is to find all files in the current directory and its subdirectories that contain the word “example” and delete them:

find . -type f -name "*example*" | xargs rm

This will find all files in the current directory and its subdirectories that contain the word “example”, and pass each file name to the rm command using xargs.

Specific Use Cases

  • Copying or moving files: You can use xargs to copy or move a list of files to another directory. For example, to copy all .txt files in the current directory to a directory called backup, you can use the following command:
    ls *.txt | xargs -I {} cp {} backup/
    

    In this command, -I {} tells xargs to replace {} with each item read from standard input.

  • Running commands in parallel: You can use xargs with the -P option to run commands in parallel. For example, to compress all .txt files in the current directory using gzip in parallel, you can use the following command:
    ls *.txt | xargs -P 4 gzip
    

    In this command, -P 4 tells xargs to run up to 4 instances of gzip in parallel.

Options

The following table lists the available options for the xargs command:

Option Description
-0 Use null characters as the delimiter instead of whitespace or newlines.
-a file Read items from file instead of standard input.
-d delimiter Use delimiter as the item delimiter instead of whitespace or newlines.
-E end Stop processing items after encountering end.
-I replace-str Replace occurrences of replace-str with each item read from standard input.
-L max-lines Use at most max-lines non-blank input lines per command line.
-n max-args Use at most max-args items per command line.
-P max-procs Run up to max-procs instances of the command in parallel.
-r Do not run the command if there are no items to process.
-t Print the command to be executed to standard error before executing it.
-x Exit if the command exits with a non-zero status.

Troubleshooting Tips

  • Command not found: If you receive an error message that the command is not found, make sure that the command is installed on your system and that it is in your PATH environment variable.
  • Argument list too long: If you receive an error message that the argument list is too long, you can use the -n option to limit the number of arguments passed to each command.
  • Incorrect output: If the output of the command is not what you expected, make sure that the items being passed to the command are correct. You can use the -t option to print the command to be executed before executing it.

Notes

  • xargs can be used with any command that accepts arguments from standard input.
  • When using xargs with commands that accept options, make sure to use the -- option to separate the options from the arguments. For example, to pass the -r option to the rm command in the previous examples, you can use the following command:
    ls *.txt | xargs -- rm -r