How to use the Xargs command in Linux

Source: Internet
Author: User

Have you ever encountered a situation where you need to do the same for multiple files over and over again? If there is, then you will certainly have a deep feeling how boring and inefficient. Fortunately there is a simple way to use the Xargs command in a UNIX-based operating system to solve this annoyance. With this command you can efficiently process multiple files, saving you time and effort. In this tutorial, you can learn how to perform command or script operations on multiple files at once, without worrying about the daunting task of dealing with countless logs or data files alone.

The Xargs command has two main points. First, you must list the target files. Second, you must specify a command or script to be executed for each file.

This tutorial will cover three scenarios where the Xargs command is used to process files distributed in different directories:

    1. Calculate the number of rows for all files
    2. Prints the first line of the specified file
    3. Execute a custom script for each file

Take a look at this directory called Xargstest (with the tree command and the-I and-f options to show the structure of the trees so that you can avoid indentation and each file will have a full path):

$ tree-if xargstest/

The contents of these six documents are as follows:

This Xargstest directory, along with the subdirectories and files it contains, will be used in the example below. Scenario 1: Calculate the number of rows for all files as mentioned earlier, the first point of using the Xargs command is a list of files used to run the command or script. We can use the Find command to identify and list the target files. Option-name ' file?? ' A file that has the name "file" and follows two characters in the Xargstest directory is specified. This search is recursive by default, meaning that the Find command searches for matching files in Xargstest and its subdirectories.
$ find xargstest/-name ' file?? '
xargstest/dir3/file3bxargstest/dir3/file3axargstest/dir1/file1axargstest/dir1/file1bxargstest/dir2/ File2bxargstest/dir2/file2a

We can send the results by pipe to the sort command to have the file names sorted in order:

$ find xargstest/-name ' file?? ' | Sort
xargstest/dir1/file1axargstest/dir1/file1bxargstest/dir2/file2axargstest/dir2/file2bxargstest/dir3/ File3axargstest/dir3/file3b

Then we need a second element, which is the command that needs to be executed. We use the WC command with the-l option to calculate the number of newline characters each file contains (it will be printed in front of each line of the output):

$ find xargstest/-name ' file?? ' | Sort | Xargs wc-l
1 xargstest/dir1/file1a 2 xargstest/dir1/file1b 3 XARGSTEST/DIR2/FILE2A 4 xargstest/dir2/file2b 5 Xargstest/dir3/file3A 6 Xargstest/dir3/file3b21 Total

As you can see, the wc-l command is not executed manually once for each file, and the Xargs command allows you to do all of the work in one step. Tasks that previously seemed impossible to accomplish, such as processing hundreds of files alone, can now be done fairly easily.

Scenario 2: Print the first line of the specified file

Now that you have some basis for using the Xargs command, you are free to choose what commands to execute. Sometimes, you might want to perform operations on only a subset of files and ignore the others. In this case, you can use the-name option of the Find command as well as the wildcard character (matching any single character) to select a specific file and export it to the Xargs command through the pipeline. For example, if you want to print a file that ends with a "B" character and omit the first line of a file ending with "A", you can use the following combination of find, Xargs, and head commands (HEAD-N1 prints the first line of a file):

$ find xargstest/-name ' file? B ' | Sort | Xargs head-n1
==> xargstest/dir1/file1b <==one==> xargstest/dir2/file2b <==one==> xargstest/dir3/file3b <==one

You will see that only files ending with "B" are processed, and all files ending with "A" are ignored.

Scenario 3: Executing a custom script for each file

Finally, you might want to execute a custom script for some files (such as bash, Python, or Perl). To do this, just simply replace the WC and head commands in the previous example with your custom script name:

$ find xargstest/-name ' file?? ' | Xargs myscript.sh
The custom script myscript.sh needs to be written to accept a file name as an argument and process the file. The above command invokes the script separately for each file found by the Find command.

Note that the file name in the example above does not contain spaces. Generally speaking, it is much more comfortable to operate a file name without spaces in a Linux environment. If you really need to deal with a file with a space in the name, the command above will not work and needs to be handled a little bit to make it acceptable. This can be achieved through the-PRINT0 option of the Find command (which prints the full file name to the standard output and ends with a null character), as well as the-0 option of the Xargs command (it will be implemented as a null character as the end-of-string tag), as in the following example:

$ find xargstest/-name ' file* '-print0 | xargs-0 myscript.sh

Note that the parameters followed by the-name option have been changed to ' file* ', meaning that all files that end with "file" and can be any character will be selected.

After reading this tutorial you should understand the role of the Xargs command and how to apply it to your work. Soon you will have time to enjoy the efficiency of this command without spending your time on repetitive tasks. For more detailed information and more options, you can enter the ' Man xargs ' command in the terminal to view Xargs's documentation.

    • This article from: Hobby Linux Technology Network
    • This article links:

How to use the Xargs command in Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.