In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "the tutorial of using xargs command in Linux system". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the tutorial on using xargs commands in Linux system.
The xargs command has two main points. First, you must list the target file. Second, you must specify the commands or scripts to be executed for each file.
This tutorial covers three application scenarios, where the xargs command is used to work with files distributed in different directories:
Calculate the number of lines in all files
Prints the first line of the specified file
Execute a custom script for each file
Take a look at the following directory called xargstest (the directory tree structure is shown with the tree command and the-I and-f options, which avoids indentation and each file has 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 following example.
Scenario 1: calculate the number of lines 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 file. Option-name 'file??' Specifies that files in the xargstest directory whose names begin with "file" and follow two arbitrary characters are matched. This search is recursive by default, meaning that the find command searches for matching files in xargstest and its subdirectories.
The code is as follows:
$find xargstest/-name 'file??'
The code is as follows:
Xargstest/dir3/file3B
Xargstest/dir3/file3A
Xargstest/dir1/file1A
Xargstest/dir1/file1B
Xargstest/dir2/file2B
Xargstest/dir2/file2A
We can pipe the results to the sort command to arrange the file names in order:
The code is as follows:
$find xargstest/-name 'file??' | sort
The code is as follows:
Xargstest/dir1/file1A
Xargstest/dir1/file1B
Xargstest/dir2/file2A
Xargstest/dir2/file2B
Xargstest/dir3/file3A
Xargstest/dir3/file3B
Then we need the 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 contained in each file (which will be printed before each line of the output):
The code is as follows:
$find xargstest/-name 'file??' | sort | xargs wc-l
The code is as follows:
1 xargstest/dir1/file1A
2 xargstest/dir1/file1B
3 xargstest/dir2/file2A
4 xargstest/dir2/file2B
5 xargstest/dir3/file3A
6 xargstest/dir3/file3B
21 total
As you can see, instead of manually executing the wc-l command on each file, the xargs command allows you to do everything in one step. Tasks that seemed impossible before, such as working with hundreds of files separately, can now be done fairly easily.
Scenario 2: print the first line of the specified file
Now that you have some basis for using xargs commands, you are free to choose which commands to execute. Sometimes, you may want to perform operations on only some files and ignore others. In this case, you can use the-name option of the find command as well as? Wildcards (which match any single character) select a specific file and pipe it to the xargs command. For example, if you want to print a file that ends with a "B" character and ignores the first line of a file that ends with "A", you can use the following combination of find, xargs, and head commands (head-N1 prints the first line of a file):
The code is as follows:
$find xargstest/-name 'file?B' | sort | xargs head-N1
The code is as follows:
= = > xargstest/dir1/file1B xargstest/dir2/file2B xargstest/dir3/file3B
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.