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--
In this issue, Xiaobian will bring you about how to understand the Xargs command under Linux. The article is rich in content and analyzes and narrates from a professional perspective. After reading this article, I hope you can gain something.
xargs is often overlooked by a command, some of its usage may not be familiar to many people, in fact, it is a powerful command, especially in the combination of pipeline batch processing
syntax
xargs syntax format is as follows
xargs [OPTION]... COMMAND
role
The most important function of xargs is to convert standard input into multiple command-line parameters with spaces or newlines as separators. When used in combination with pipeline commands, xargs can be used.
The following examples illustrate
[root@ecs-centos-7 tmp]# echo "11 22 33" |xargs mkdir [root@ecs-centos-7 tmp]# ll Total Usage 16 drwxr-xr-x 2 root 4096 January 20 00:03 11 drwxr-xr-x 2 root 4096 January 20 00:03 22 drwxr-xr-x 2 root 4096 January 20 00:03 33
In the example above, the echo "11 22 33" command on the left side of the pipe takes its output as input to the xargs mkdir command on the right side of the pipe, and the command on the right side of the pipe divides the input on the left into multiple command-line arguments with spaces or line separators.
So echo "11 22 33"| The xargs mkdir command is equivalent to three commands
mkdir 11 mkdir 22 mkdir 33
option descriptions
Option description-d specifies the delimiter of input parameters, the default is to use spaces or newlines as separators-p prints the command to be executed, and asks the user whether to execute-t prints the final command to be executed and executes it without user confirmation-0 indicates to use null as separators-n how many parameters in a line are used as command line parameters at a time-I specifies alternative strings for each command line parameter, An intermediate variable like-r that stores parameter items does not execute commands when the input does not include any non-spaces
Example 1 xargs without any options
The xargs command is mostly used with pipes, but it can also be used alone, in which case xargs is equivalent to the echo command, which outputs user input to the screen via the echo command.
[root@ecs-centos-7 ~]# xargs hello world ! ( ctrl + d ) hello world ! [root@ecs-centos-7 ~]#
In the example above, after typing xargs and Enter, we begin to accept user input, and the user enters hello world ! After that, press ctrl + d to end the input. After that, the user's input will be output on the screen.
Example 2 -d Option
The-d option specifies the delimiter for the xargs command
[root@ecs-centos-7 ~]# echo -n "a#b#c" | xargs echo a#b#c [root@ecs-centos-7 ~]# echo -n "a#b#c" | xargs -d "#" echo a b c
In the above example, the delimiter is specified as # , so "a#b#c" is converted into three command-line parameters: a b c
Example 3 -p option
The-p option prints out the command to be executed and performs a second confirmation of whether to execute it in turn before executing the command, y means confirm execution, n means cancel execution
[root@ecs-centos-7 tmp]# echo "a b c" | xargs -p touch touch a b c ?... y [root@ecs-centos-7 tmp]# ll Total Usage 0 -rw-r--r-- 1 root root 0 January 20 00:46 a -rw-r--r-- 1 root root 0 January 20 00:46 b -rw-r--r-- 1 root root 0 January 20 00:46 c
Example 4 -t option
The-t option prints out the final command and executes the command directly, eliminating the need for user confirmation.
[root@ecs-centos-7 tmp]# ll Total Usage 0 -rw-r--r-- 1 root root 0 January 20 00:49 a -rw-r--r-- 1 root root 0 January 20 00:49 b -rw-r--r-- 1 root root 0 January 20 00:49 c [root@ecs-centos-7 tmp]# echo "a b c"| xargs -t rm rm a b c
Example 5 - 0 Options
As we mentioned earlier, the xargs command is delimited by spaces or newlines. In most cases, execution is normal, but there is a problem when the file name contains spaces.
Filenames containing spaces, when passed as input to xargs, are processed by xargs as multiple command-line arguments
The following example reproduces the problem
[tt@ecs-centos-7 tmp]$ ls a.txt b 1.txt c.txt [tt@ecs-centos-7 tmp]$ ls |xargs -t rm rm a.txt b 1.txt c.txt rm: cannot delete 'b': there is no that file or directory rm: cannot delete' 1.txt ': there is no that file or directory [tt@ecs-centos-7 tmp]$
In the example, through the ls command, you know that there are three files in the directory: a.txt b 1.txt c.txt
When passing through LS| xargs -t rm command to delete all files in the directory found that a.txt and c.txt can be deleted correctly, but b 1.txt file name contains spaces, delete will be treated as b and 1.txt two files
Therefore, in the output of the example, the error prompt rm: cannot delete "b": there is no such file or directory and rm: cannot delete "1.txt": there is no such file or directory appear in the output.
The above problem can be solved by combining the-0 option with the find command
[tt@ecs-centos-7 tmp]$ ls a.txt b 1.txt c.txt [tt@ecs-centos-7 tmp]$ find . -type f -print0 | xargs -0 -t rm rm ./ a.txt ./ b 1.txt ./ c.txt [tt@ecs-centos-7 tmp]$ ll Total Usage 0 [tt@ecs-centos-7 tmp]$
The-print0 option of the find command indicates that the output file list is null-separated
Also, the-0 option of the xargs command indicates null as the delimiter
As you can see from the output, the-0 option in combination with the find command correctly removes filenames that contain spaces.
Example 6 -n Options
Sometimes the user enters multiple arguments, and the-n option is how many arguments in a line are taken as command line arguments at a time
[tt@ecs-centos-7 tmp]$ echo "a b c d e f" | xargs -n 2 a b c d e f [tt@ecs-centos-7 tmp]$ echo "a b c d e f" | xargs -n 4 a b c d e f [tt@ecs-centos-7 tmp]$
command echo "a b c d e f"| xargs -n 2 specifies that every 2 parameters are output as a command, so a b c d e f 6 parameters output 3 lines
Similarly, command echo "a b c d e f"| xargs -n 4 specifies that every 4 parameters are output once, so a b c d e f 6 parameters output 2 lines, and the second line has only two parameters
Example 7 -r option
This option means that the command is not executed when the input does not contain non-spaces, and by default the command is executed regardless of whether the input contains non-spaces
Some commands must have operands. If the input does not contain any parameters, there will be an error prompt for missing operands when executing these commands. You can add the-r option without an error prompt.
[tt@ecs-centos-7 tmp]$ echo '' | xargs rm rm: Missing operand Try 'rm --help' for more information. [tt@ecs-centos-7 tmp]$ echo '' | xargs -r rm [tt@ecs-centos-7 tmp]$
In the above example, the result of the command echo '' is passed as input to xargs rm on the right side of the pipeline. After parameter conversion, xargs rm has no parameters, so the rm command will prompt for missing operands, but the xargs -r rm command will not be executed, so it will not have an error prompt.
Example 8 -I Parameters
-I parameter represents the variable of each parameter of the command line parameter
[tt@ecs-centos-7 tmp]$ ls a b c [tt@ecs-centos-7 tmp]$ ls | sort | xargs -I F sh -c 'echo F.txt; touch F.txt' a.txt b.txt c.txt [tt@ecs-centos-7 tmp]$ ls a a.txt b b.txt c c.txt
In the example above, the current directory has three files, a b c.
command ls| sort |xargs -I F sh -c 'echo F.txt; touch F.txt' inputs are a, b, c, respectively, -I F indicates that F is a substitute string for the input parameters. When executing the command, the F in the subsequent command echo F.txt; touch F.txt will be replaced by the actual parameters, and the following commands will actually be executed.
echo a.txt; touch a.txt echo b.txt; touch b.txt echo c.txt; touch c.txt
This article describes common uses of the xargs command, and examples of commonly used options are provided.
The above is how to understand the Xargs command under Linux shared by Xiaobian. If you happen to have similar doubts, you may wish to refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.
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.