Linux xargs command examples

Filed Under: Ubuntu

The xargs command in Linux/UNIX reads items from the standard input, delimited by blanks or newlines, and executes the command one or more times with any initial arguments.

If no command is provided as an argument to xargs, then the default command that the tool executes is an echo.

Blank lines on the standard input are ignored. The items those xargs read from standard input can be protected with double or single quotes or a backslash.

The xargs command is extremely powerful, especially when combined with commands like grep or echo. In this tutorial, we will cover a few basic usages of xargs command to get you started with it.

Linux xargs Command Syntax

The syntax of xargs command is:


xargs [options] [command [initial-arguments]]

You can view all the options and arguments by invoking the command man xargs from the terminal. The following section list a few examples of args command.

Linux xargs Simple Example

The xargs command without any command echoes the arguments in the standard input. In the following example output from ls command is piped into the xargs command and the xargs command without any command echo the file list in the standard input.


# ls | xargs
Linux xargs command examples

Xargs Example 1.a

The following example shows how the output of echo command is piped to xargs and the xargs command uses the piped argument to create folders using mkdir.


# echo 'one two three' | xargs mkdir
Xargs Example 1.b

Xargs Example 1.b

Limit Number of Arguments

The number of arguments passed to the xargs is defined by your system’s limit. The -n switch limits the number of arguments to be passed to a given command. The xargs command will run continuously with n number of arguments until all arguments are exhausted.

The following example limits the number of arguments read from standard input to 2. Xargs command will go on reading two arguments at a time until it completes reading all of them. Further, the -t option will print the command in the standard output before executing it.


# echo "file1 file2 file3 file4 file5" | xargs -n 2 -t touch
Xargs Example 2

Xargs Example 2

Enable User Prompt before execution

The xargs with -p option will prompt user before execution of command. The following example shows xargs command prompts the user for either for ‘Y’ or ‘N’ before continuing with creating folders with each argument.


# echo Hello World | xargs -p mkdir
# echo one two three | xargs -p -n 1 mkdir
Xargs Example 3

Xargs Example 3

Delete files with find and xargs command

One of the most common usages of xargs is to use it with the find command. Consider a situation where you want to remove or rename a bunch of files after running through find command. Here both find and xargs command can be used together on files those are matching certain attributes and then delete or rename them at one go.

For example, suppose you want to delete all the files in the /tmp folder those are created within the last five minutes. To do that using xargs, first, find out all the files those are matching with your chosen criteria by using find command and then pipe the output to the xargs command which in turn will use rm command to remove them.


# find /tmp/* -cmin -5 
# find /tmp/* -cmin -5 | xargs -t rm
# find /tmp/* -cmin -5 
Xargs Example 4

Xargs Example 4

Search for pattern using xargs

Another powerful usages of Xargs command is to search for a pattern in a list of files returned by another unix command like ls or find. The following example combines find and xargs command wherein the find command returns all the text(.txt) files from current working directory. The result i.e. filenames are then piped to xargs command where we search for the word ‘file’ using grep "file". If the pattern is present in the files then the lines containing the pattern will be printed in the terminal along with the filename:


# find . -name "*.txt" | xargs grep "file"
Xargs Example 5

Xargs Example 5

Run multiple commands with xargs

So far we have seen how xargs command uses piped arguments to do a various task like renaming a batch of files, searching for a pattern and so on. While doing these, it uses a single command after the piped operation. It is also possible to run multiple commands along with xargs.

For example, if you want to create multiple files and list them at the same time using xargs command then run xargs command with -I switch followed by defining a replacement string. All occurrences of replacement string are then replaced with the argument passed to xargs.


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

Xargs Example 6

Executing commands in parallel

In some situation, you might be using xargs to invoke a compute-intensive command for each argument. By taking advantage of your multi-core machine, it is possible to invoke the command in parallel. The -P switch exactly does this by defining the maximum number of processes. By default, the maximum number of process is 1.

For example, the following xargs command prints the number 1 to 30 by taking 5 arguments at a time and most notably run up to 8 processes at a time.


# printf %s\\n {1..30} | xargs -n 5 -P 8
Xargs Example 7

Xargs Example 7

Accept input from a file

So far we have seen inputs for xargs command are read either from standard input or from the output of other commands through pipe operation. However, it is also possible for xargs command to read input from a file directly. The -a switch allows you to define a file for inputs needed by xargs.

The following xargs command read input from a file(input.txt) and then creates files using touch command.


# cat input.txt
one.txt
two.txt
# xargs -a input.txt touch
Linux xargs command examples

Xargs Example 8

Summary

That’s all! Although we have covered a few basic usages of XARGS command you can now try out more advanced features of XARGS to build a powerful command. Hope you have liked the post and thanks for reading it.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages