| by Arround The Web | No comments

How to Use the Bash xargs Commands

“xargs is a bash command that allows the execution of multiple commands in the same line. Whether the commands take parameters or arguments as input, they can run in the same line when combined with xargs. With xargs, you can build and execute multiple commands through the standard input, as xargs can take an input and convert it to a command argument to be used by the next command.”

To help you understand xargs and how you can use it, this article will give multiple examples of using the xargs command. Take a look!

How to Use xargs Command

How you use the xargs command depends on what you want to achieve. If you use xargs with no input, it will echo anything that you type on the terminal once you press CTRL + D

Handling Files and Directories Using xargs

If you want to find all files with a given extension, you can use xargs. Let’s have an example of using ls with xargs for text files.

$ find . -name '*.txt' -type f | xargs ls

 

In the above command, we are using the find command to search for files in the current directory that have a .txt extension.

Here’s the output

If you want to delete all the files we found using the command above, you can use the command below.

$ find . -name '*.txt' -type f | xargs rm -f

 

The -t flag is for verbose.

Suppose you wanted to create multiple directories where you are adding the input from the standard input. We could combine mkdir with echo, as in the example below.

$ echo ‘linux1 tech2 dir3 dir2’ | xargs mkdir

 

If you wanted to remove all directories in the current directory, you could use xargs, as shown below.

$ find . -type d | xargs rm -r

 

Xargs Reading Input From a File

Using the -a flag, you can instruct xargs to read items from a file, as in the case below.

With the same example, we can demonstrate how to combine multiple commands using xargs. For that, use the -I flag.

For instance, we could use the command below to read the contents of a file and create a directory using every string in the file.

$ cat 1.txt | xargs -I % mkdir %

 

You could take the command further to echo the file’s contents while creating the directory by adding the echo command, as shown below.

$ cat 1.txt | xargs -I % sh -c 'echo %; mkdir %'

 

Using xargs With grep

The grep command is used for matching patterns, and when you want to search for a given string in various files, you can use xargs to combine find with grep, where the output of find will get converted to the input to be used by grep.

Here’s an example where we find all text files containing the string “linuxhint.”

$ find . -name '*.txt' | xargs grep 'linuxhint'

 

Limiting Output Per Line

Suppose you want to read the content of a file. You could limit the number of outputs for each line using the -n option.

Let’s have an example of printing numbers in a given range using the seq command and limit the output per line to 4

$ seq 1 30 | xargs -n 4

 

Specifying the Delimiter

By default, xargs assumes a blank space as the delimiter. However, if you need to use another delimiter, you must use the -d command followed by the delimiter.

Here’s an example that uses a colon as the delimiter when working with a file to read its contents and create directories using the strings in the file.

Here are the contents of our file.

Now, let’s run the command below.

$ cat names.txt | xargs -d: | xargs -t mkdir

 

The output would be:

The Null Delimiter

If we tried to use xargs with files with blank space in the names, it would bring incorrect results as xargs uses blank space as its delimiter. In such a case, we must use the -0 flag, which signals xargs to use null delimiter. Also, we need the -print0 flag, which is used by find to indicate a null-separated list.

For instance, suppose we had the image files below.

If we tried to delete all the files using the command below, xargs would only print the image files with no space in their name, as shown below.

$ find . -name '*.png' -type f | xargs rm -f

 

To overcome this, we need to use the -0 flag and the -print0.

$ find . -name '*.png' -type f -print0| xargs -0 rm -f

 

We can verify that we have successfully deleted all the files.

Conclusion

This guide has covered the bash xargs command in-depth, giving examples of how to use it to achieve various aspects. Referencing the presented examples, you can implement xargs on your end to perform various tasks. Also, check the man page for xargs to get more usage details.

Share Button

Source: linuxhint.com

Leave a Reply