xargs: command not found

The xargs command reads from standard input and executes a command for each argument provided. Each argument must be separated by blanks. The pipe operator is used to make the output of the first command the input for the second command. The xargs command is commonly used with the find command to operate on each result that is found within the file or directory search.

Syntax

The general syntax of the xargs command is:

# command [options] [arguments] | xargs [options] {command}

Let’s say you want to delete all of the files in the /foo directory that have a .pdf extension. You can use xargs to automate the process:

# find /foo -type f -name "*.pdf" | xargs rm

The find command searches for all files in /foo that have a .pdf extension, then pipes the result to the xargs command. Because the results are delimited by a space, the xargs command will execute the rm command for each file in the results— removing all PDF files in the directory.

The xargs command has various options as shown below:

Option Description
-I {replacement string} Consider each line in the standard input as a single argument.
-L {number of lines} Read a specified number of lines from the standard input and concatenate them into one long string.
-p Prompt the user before each command.
-n {number of arguments} Read the maximum number of arguments from the standard input and insert them at the end of the command template.
-E {end of string} Represent the end of the standard input.
-t Write each command to the standard error output before executing the command.
-s {max size} Set the maximum allowable size of an argument list to a specified number of characters.

If you encounter the below error while running the xargs command:

xargs: command not found

you may try installing the below package as per your choice of distribution:

OS Distribution Command
OS X brew install findutils
Debian apt-get install findutils
Ubuntu apt-get install findutils
Alpine apk add findutils
Arch Linux pacman -S findutils
Kali Linux apt-get install findutils
Fedora dnf install findutils-1
Raspbian apt-get install findutils

xargs Command Examples

1. Run a command using the input data as arguments:

# arguments_source | xargs command

2. Run multiple chained commands on the input data:

# arguments_source | xargs sh -c "command1 && command2 | command3"

3. Delete all files with a `.backup` extension (`-print0` uses a null character to split file names, and `-0` uses it as delimiter):

# find . -name '*.backup' -print0 | xargs -0 rm -v

4. Execute the command once for each input line, replacing any occurrences of the placeholder (here marked as `_`) with the input line:

# arguments_source | xargs -I _ command _ optional_extra_arguments

5. Parallel runs of up to `max-procs` processes at a time; the default is 1. If `max-procs` is 0, xargs will run as many processes as possible at a time:

# arguments_source | xargs -P max-procs command
Related Post