Sep 26, 2023 5 min read

Xargs Command in Linux

In this tutorial, we'll go through the basics of using the xargs command that can create and execute commands using standard input.

Xargs Command in Linux
Table of Contents

Introduction

You can use the xargs utility to create and execute commands using standard input. It's most commonly used in conjunction with other instructions via piping.

You can pass standard input as an argument to command-line programs like mkdir and rm using xargs.

In this tutorial, we'll go through the basics of using the xargs command. We will also address a few FAQs on Xargs Command in Linux.

How to Use Linux xargs Command

xargs reads arguments from standard input, separated by blank spaces or newlines, and executes the specified command with the arguments from the input. If no command is specified, /bin/echo is used by default.

The xargs command has the following syntax:

xargs [OPTIONS] [COMMAND [initial-arguments]]

The most basic example of utilizing xargs would be to use a pipe to provide multiple strings separated by whitespace to xargs and then run a command with those strings as arguments.

echo "file1 file2 file3" | xargs touch

In the preceding example, the standard input is piped to xargs, and the touch command is executed for each argument, resulting in three files. It's the same as if you were going to run:

touch file1 file2 file3

How to View the Command and Prompt the User

Use the -t (--verbose) option to print the command on the terminal before running it:

echo  "file1 file2 file3" | xargs -t touch
Output

touch file1 file2 file3

Use the -p (--interactive) option to get a prompt whether to run each command before executing it:

echo  "file1 file2 file3" | xargs -p touch

To confirm and start the command, type y or Y:

Output

touch file1 file2 file3 ?...y

When running damaging commands, this option comes in handy.

How to Limit the Number of Arguments

The system's limit determines the number of parameters provided to the command by default.

The -n (--max-args) option sets the maximum number of arguments that can be submitted to a command. xargs executes the provided command as many times as required until all arguments have been exhausted.

The number of parameters read from the standard input is limited to one in the following example.

echo  "file1 file2 file3" |  xargs -n 1 -t touch

The touch command is executed separately for each argument, as shown in the verbose output below:


touch file1
touch file2
touch file3

How to Run Multiple Commands

Use the -I option with xargs to run several commands. It works by adding a replace-str option after the -I option, and replacing all instances of the replace-str with the input supplied to xargs.

The following xargs example will perform two commands: first, it will use touch to create files, and then it will use the ls command to list the files:

echo "file1 file2 file3" | xargs -t -I % sh -c '{ touch %; ls -l %; }'
Output

-rw-r--r-- 1 linuxize users 0 May  6 11:54 file1
-rw-r--r-- 1 linuxize users 0 May  6 11:54 file2
-rw-r--r-- 1 linuxize users 0 May  6 11:54 file3

Percent % is a popular replacement for replace-str. You can, however, use another placeholder, such as ARGS:

echo "file1 file2 file3" | xargs -t -I ARGS sh -c '{ touch ARGS; ls -l ARGS; }'

How to Specify a Delimiter

Set a custom delimiter using the -d (--delimiter) option, which can be a single character or an escape sequence starting with \.

We'll use ; as a delimiter in the following example:

echo  "file1;file2;file3" | xargs -d \; -t touch
Output

touch file1 file2 file3

How to Read Items from File

Instead of using standard input, the xargs command can read items from a file. Use the -a (--arg-file) option followed by the file name to accomplish this.

The xargs command will read the ips.txt file and ping each IP address in the following example.

8.8.8.8
1.1.1.1

We're also running xargs with the -L 1 option, which tells it to read one line at a time. If this option is not used, xargs will use a single ping command to ping all IPs.

xargs -t -L 1 -a ips.txt ping -c 1
Output

ping -c 1 8.8.8.8 
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=50 time=68.1 ms

...
ping -c 1 1.1.1.1 
PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
64 bytes from 1.1.1.1: icmp_seq=1 ttl=59 time=21.4 ms

Using xargs with find

The most common use of xargs is in conjunction with the find command. Find can be used to find certain files, and then xargs can be used to perform operations on those files.

Always use the find -print0 option, which causes find to print the whole file name followed by a null character, to avoid problems with file names that contain newlines or other special characters. With the -0, (--null) option, xargs may appropriately understand this output.

Find will print the entire names of all files in the /var/www/.cache directory, and xargs will send the file paths to the rm command in the following example:

find /var/www/.cache -type f -print0 | xargs -0 rm -f

Using xargs to Trim Whitespace Characters

The xargs command can also be used to eliminate whitespace from both sides of a string. Simply feed the string to the xargs command, and it will cut it for you:

echo "  Long line " | xargs
Output

Long line

When comparing strings in shell scripts, this can be handy.

#!/bin/bash

VAR1=" Vegastack "
VAR2="Vegastack"

if [[ "$VAR1" == "$VAR2" ]]; then
    echo "Strings are equal."
else
    echo "Strings are not equal."
fi

## Using xargs to trim VAR1
if [[ $(echo "$VAR1" | xargs) == "$VAR2" ]]; then
    echo "Strings are equal."
else
    echo "Strings are not equal."
fi
Output

Strings are not equal.
Strings are equal.

FAQs to Xargs Command in Linux

How does the xargs command handle input items? 

The xargs command takes each item from the input (usually whitespace-separated) and appends it to the command as arguments. It executes the command multiple times if necessary, splitting the input items into manageable batches.

Can you provide an example of using the xargs command? 

Certainly! An example of using xargs would be: find . -name "*.txt" | xargs grep "hello". This command finds all text files in the current directory and its subdirectories, then passes them as input to grep to search for the word "hello".

How does the xargs command handle whitespace and special characters in input items? 

By default, xargs treats whitespace, single quotes, and double quotes as delimiters. It handles special characters and escapes them appropriately when passing them to the command for execution.

How can I limit the number of items processed at a time by xargs? 

You can limit the number of items processed at a time using the -n option followed by a number. For example, xargs -n 5 command executes command on groups of up to 5 input items at a time.

Can the xargs command execute commands in parallel? 

Yes, the xargs command can execute commands in parallel using the -P option followed by a number. For example, xargs -P 4 command runs command in parallel with a maximum of 4 concurrent processes.

How can I specify a custom delimiter for input items with xargs? 

You can specify a custom delimiter using the -d option followed by the desired delimiter character. For example, echo "item1;item2;item3" | xargs -d ';' command executes command on each item separated by semicolons.

Is there a dry-run mode for the xargs command? 

Yes, the xargs command supports a dry-run mode using the -t or --verbose option. This mode shows the commands that would be executed without actually executing them.

Conclusion

On Linux, xargs is a command-line utility that allows you to create and execute commands.

Read the xargs man page for more information about each xargs option.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.