How to Use xargs commands in Linux
Have you ever performed the same operation on multiple files over and over again? If you have, you will surely feel bored and inefficient. Fortunately, there is a simple way to solve this problem by using the xargs command in Unix-based operating systems. With this command, you can effectively process multiple files, saving you time and energy. In this tutorial, you can learn how to execute commands or scripts on multiple files at a time, so you no longer have to worry about the daunting tasks like processing countless logs or data files separately.
The xargs command has two key points. First, you must list the target files. Second, you must specify the commands or scripts to be executed for each file.
This tutorial involves three application scenarios. The xargs command is used to process files distributed in different directories:
- Calculate the number of rows of all files
- Print the first line of the specified file
- Execute a custom script for each file
See the following directory named xarstest (the tree structure is displayed using the tree Command and the-I and-f options to avoid indentation and each file has a complete path ):
- $ Tree-if xarstest/
The contents of these six files are as follows:
The xarstest directory and Its subdirectories and files are used in the following example.
Scenario 1: calculate the number of rows of all files
As mentioned earlier, the first point of using xargs commands is a list of files used to run commands or scripts. We can use the find command to determine and list the target file. Option-name'file ?? 'Specifies the files in the xarstest directory whose names start with "file" and follow two arbitrary characters. By default, this search is recursive, meaning that the find command searches for matched files in the xarstest and its subdirectories.
- $ Find xarstest/-name 'file ?? '
- Xarstest/dir3/file3B
- Xarstest/dir3/file3A
- Xarstest/dir1/file1A
- Xarstest/dir1/file1B
- Xarstest/dir2/file2B
- Xarstest/dir2/file2A
We can send the result to the sort command through the pipeline to arrange the file names in order:
- $ Find xarstest/-name 'file ?? '| Sort
- Xarstest/dir1/file1A
- Xarstest/dir1/file1B
- Xarstest/dir2/file2A
- Xarstest/dir2/file2B
- Xarstest/dir3/file3A
- Xarstest/dir3/file3B
Then we need the second element: the command to be executed. We use the wc command with the-l option to calculate the number of line breaks contained in each file (printed before each line of output ):
- $ Find xarstest/-name 'file ?? '| Sort | xargs wc-l
- 1 xarstest/dir1/file1A
- 2 xarstest/dir1/file1B
- 3 xarstest/dir2/file2A
- 4 xarstest/dir2/file2B
- 5 xarstest/dir3/file3A
- 6 xarstest/dir3/file3B
- 21 total
As you can see, you do not need to manually execute the wc-l command for each file. The xargs command allows you to complete all operations in one step. Tasks that seem unable to be completed before, such as processing hundreds of files separately, can now be done quite easily.
Scenario 2: print the first line of the specified file
Now that you have some basics of using xargs commands, you can choose what commands to execute. Sometimes, you may want to only perform operations on some files and ignore others. In this case, you can use the-name option of the find command and? Wildcard (matching any single character) to select a specific file and output it to the xargs command through the pipeline. For example, if you want to print A file ending with "B" and ignore the first line of the file ending with ", you can use the following command combination of find, xargs, and head (head-n1 prints the first line of a file ):
- $ Find xarstest/-name 'file? B '| sort | xargs head-n1
- ==> Xarstest/dir1/file1B <=
- One
-
- ==> Xarstest/dir2/file2B <=
- One
-
- ==> Xarstest/dir3/file3B <=
- One
You will see that only files ending with "B" will be processed, and all files ending with "A" will be ignored.
Scenario 3: execute a custom script for each file
Finally, you may want to execute a custom script (such as Bash, Python, or Perl) on some files ). To do this, simply replace the wc and head commands in the previous example with your Custom Script Name:
- $ Find xarstest/-name 'file ?? '| Xargs myscript. sh
The Custom Script myscript. sh must be written to accept a file name as a parameter and process the file. The above command calls the script for each file found by the find command.
Note that the file name in the above example does not contain spaces. Generally, it is much more comfortable to operate a file name without spaces in a Linux environment. If you really need to process a file with spaces in the name, the above command will not be available, you need to handle it a little bit to make it acceptable. This can be done using the-print0 option of the find command (it prints the complete file name to the standard output and ends with an empty character ), and the-0 option of the xargs command (it will end with a null character as a string), as shown in the following example:
- $ Find xarstest/-name 'file * '-print0 | xargs-0 myscript. sh
Note that the parameter followed by the-name option has been changed to 'file * ', which means all files starting with "file" and ending with any character will be selected.
Summary
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 brought about by this command, instead of spending your time on repetitive tasks. For more details and more options, you can enter the 'man xargs 'command in the terminal to view the xargs documentation.
This article permanently updates the link address: