Linux xargs Command

32

The xargs command is a command-line tool used to read data from standard input and later runs a command based on the standard input. It is a useful tool in file management, especially when used with other commands such as mkdir, grep, rm etc. In this tutorial, we are going to learn how to use xargs on Linux.

Basic syntax

The basic syntax when using the xargs command is as follows:

$ command1 | xargs command2

To pass the stdout  to xargs, we use a pipe (|) symbol.

Xargs can also be used with several options, and in that case, it takes the syntax:

$ command1 | xargs [options] command2

Using xargs with the find command

Xargs command is commonly used with the find command.  It is used to search for specific files and perform further processing on those files using xargs. The syntax for combining xargs with find is:

$ find [directory] -name “[search-term]” -type f | xargs [command]

When using xargs with find, it does not include files that contain special characters in their names. It is advisable to use the find -print0 option to include such files.

In the example below,  we will use the find command to search for all the files with the .txt extension and then delete them. Let’s first list using the ls command.

Now, let’s invoke the find command and xargs command to locate all the text files (.txt) and delete them.

$ find /home/james  -name “*.txt” -type f -print0 | xargs rm -f

Using xargs with grep

In addition, the xargs command can be used with the grep to search for a string in a list of files specified. Use the syntax below:

$ find. -name ‘search-term’ | xargs grep ‘string-to-find-in-files’

In the example below,  we first searched for files with the .txt extension and then piped them to xargs, which then executed the grep command. We can observe that it searched for the string ‘sample.’

Run Multiple commands with Xargs command

To run multiple commands with xargs, use the -I option. The syntax used is:

[command] | xargs -I % sh -c ‘[command-1] %; [command-2] %’

Let’s take a look at the example below; we will run two commands; first, we will create three files using the touch command and then list them using the ls command.

Read Items From File

Xargs is used to read the standard input.  To read the items in a file, use the xargs command along with the  -a option.

View commands executed by xargs

To see the commands executed by xargs in standard output, use the -t option. The syntax is:

$ [command] | xargs -t [command]

In the example below, notice that xargs executed the touch command on the string provided by an echo.

Limit Output per Line

You can control the number of arguments passed to the xargs command at a given time.   Use the -n option to specify the number of arguments you are limiting xargs to.

$ [command-providing-input] | xargs –n [number] [command]

In the following example, xargs splits the string from the echo command in. to three parts. It then executes echo for each part.

Remove Blank Spaces in String

xargs can be used as a tool to remove blank spaces from a string.  Pipe the string to the xargs command. Use the syntax below:

In the example below, after xargs is executed, the command is string is printed without any spaces before and after the double quotation marks

$ echo ”  xargs command on Linux “ | xargs

List Number of Lines/Words/Characters in Each File

xargs can be used as a tool to display a list with the number of lines, words and characters in files. Use the wc command ( word count ) after xargs to display line, word, and character count of specific files

In the example below,  we used wc to list the number of characters in the files bearing  the string “file”:

Specify the Delimiter

To change the delimiter from the default,  use the -d command option followed by a character you wish to use as the new delimiter.  The syntax is shown below:

$ [command-] | xargs -d [new-delimiter] | xargs [command]

In the image below,  we change our delimiter * and then apply mkdir to each command.

Copying  Files  to Multiple Directories

Xargs can be used to  copy files to several directories. Use the syntax below:

$ echo [directory1] [directory2] | xargs -n 1 cp -v [file]

Conclusion

Xargs is a powerful command-line utility for everyday use.  For more detailed information, read the  HYPERLINK “http://man7.org/linux/man-pages/man1/xargs.1.html”xargs man page.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More