Use of Linux xargs command

Use of Linux xargs command

1. Function:

xargs can convert the data separated by spaces or newlines in stdin into arguments separated by spaces and pass them to other commands. Because spaces are used as delimiters, xargs may misjudge files or nouns with other meanings when they contain spaces. Simply put, xargs is a filter that passes parameters to other commands and is one of the important components for building single-line commands.

The reason why xargs is used is that many commands do not support the use of pipes | to pass parameters, for example:

find /sbin -perm +700 |ls -l //This command is wrong because standard input cannot be used as a parameter for ls find /sbin -perm +700 |xargs ls -l //This is correct

2. Command format

xargs [options] [command]

3. Option description:

-0: If the input stdin contains special characters, such as backquote `, backslash \, space, etc., xargs can restore it to normal characters. The default option for xargs.
-e <flag>,-E <flag>,--eof=<eof-str>: eof means end of file string. Flag can be a string or multiple strings separated by spaces. When xargs analyzes this flag, it will stop working. See Example 2.
-p: Ask the user each time an argument is executed.
-n <num>: Indicates the number of arguments used at a time when the command is executed, specified by num. The default is to use all parameters.
-t: Print the command first and then execute it.
-a <file>: Read from file as sdtin.
-i[replace-str]: Tells xargs to use {} instead of the parameters read from standard input. You can specify the replacement string replace-str. If not specified, the default is {}. It is recommended to use -I, which complies with the POSIX standard.
-I [replace-str]: Assigns each parameter output by xargs to the subsequent command separately. The parameter needs to be replaced by the specified replacement string replace-str. That is to say, replace-str cannot be defaulted and must be explicitly specified. Symbols such as {} $ @ can be used. Its main function is to adjust the parameter position when there are multiple parameters after the xargs command. For example: find . -name "*.txt"|xargs -I {} cp {} /tmp/{}.bak.
-r: or --no-run-if-empty, when the input of xargs is empty, xargs will stop and there is no need to execute subsequent commands. -r is the default option of xargs.
-s <num>: The maximum number of characters in the command line, which refers to the maximum number of characters in the command line after xargs, including commands, spaces, and line breaks. Each parameter is passed separately to the command following xargs. See Example 4.
-L <line_num>: Set the maximum number of lines in the standard input as the parameter for each execution of the command. See Example 5.
-d <delim>, --delimiter=<delim>: xargs uses newline and space as delimiters when processing standard input by default. The delimiter for output arguments is space. Here, change the delimiter when xargs processes standard input.
-x: means eXit, which is mainly used with -s. When the number of characters in the command line is greater than the value specified by -s, xargs will exit.
-P: Modify the maximum number of processes. The default is 1. When it is 0, it means as many as it can. This option is rarely used and its usage is currently unclear.

4. Usage Examples

(1) Restore shell special characters to normal characters.

[b3335@MIC ~]$ echo '`0123`4 56789'|xargs -t echo
echo `0123`4 56789 
`0123`4 56789

If you perform the following operations directly, you will get an error message that the command 01234 cannot be found, because the backquotes will execute 01234 as a command in the shell, but 01234 is not a command. -t means print the command first and then execute it.

[b3335@MIC ~]$ echo `01234` 56789
-bash: 01234: command not found
56789

(2) Set the end mark when xargs reads parameters to end with a comma. It should be noted here that the end mark must be a separate field, that is, a field separated by spaces or newlines.

[b3335@MIC ~]$ echo 01234 , 56789|xargs -E ","
01234

(3) When using commands such as rm and mv to operate multiple files at the same time, an error message "argument list too long" may be displayed. In this case, you can use xargs to solve the problem. xargs separates the string of standard input and passes it separately as a parameter to the subsequent command. For example, add the suffix to all files in the current directory.

ls | xargs -t -i mv {} {}.bak

#Select files that meet the conditions ls|grep -E "201701|201702|201703|201704|201705|201706|201707|201708|201709|201710" |xargs -i mv {} {}.bak

(4) Set the maximum number of characters in the command line. By default, the parameters are passed into the command one by one for execution.

[b3335@MIC test]$ echo "01234 56789"|xargs -t -s 11
echo 01234 
01234
echo 56789 
56789

(5) Set the number of lines in the standard input to be used as command parameters each time. The default is to merge all the lines in the standard input into one line and pass it to the command for execution at one time.

[b3335@MIC test]$ echo -e "01234\n56789\n01234" | xargs -t -L 2 echo 
echo 01234 56789 
01234 56789
echo 01234 
01234

(6) Output the file contents in the same line separated by spaces.

//List the contents of the file cat test.txt
abcd
fghijklmno

//Multiple lines of input and single line of output: 
cat test.txt | xargs
abcdefghijklmno

(7) Combined with ps, grep, awk and kill, forcibly terminate the specified process

ps -ef|grep spp|awk '{printf "%s ",$2}'|xargs kill -9

Command Explanation:
ps -ef|grep spp is used to find the process containing spp, awk '{printf "%s ",$2,FNR} prints the target process ID, and xargs kill -9 passes the target process ID as a parameter to kill -9 to kill the process.

The above is the detailed content of using Linux xargs command. For more information about Linux xargs command, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Linux Basics: An Introduction to the xargs Command
  • Detailed explanation of xargs command under Linux and the difference between xargs and pipeline
  • A detailed introduction to Linux xargs command
  • Detailed explanation of xargs command usage in linux shell script learning
  • Detailed Tutorial on Using xargs Command on Linux

<<:  Super detailed MySQL usage specification sharing

>>:  Detailed explanation of JavaScript operation mechanism and a brief discussion on Event Loop

Recommend

Samba server configuration under Centos7 (actual combat)

Samba Overview Samba is a free software that impl...

Implementation of CSS heart-shaped loading animation source code

Without further ado, let me show you the code. Th...

Analysis and solution of flex layout collapse caused by Chrome 73

Phenomenon There are several nested flex structur...

In-depth analysis of MySQL indexes

Preface We know that index selection is the work ...

Solve the problem that ifconfig and addr cannot see the IP address in Linux

1. Install the Linux system on the virtual machin...

Detailed explanation of for loop and double for loop in JavaScript

for loop The for loop loops through the elements ...

How to implement remote access control in Centos 7.4

1. SSH remote management SSH is a secure channel ...

Solution for Vue routing this.route.push jump page not refreshing

Vue routing this.route.push jump page does not re...

Detailed explanation of MySQL sql99 syntax inner join and non-equivalent join

#Case: Query employee salary levels SELECT salary...

Mysql 5.6 adds a method to modify username and password

Log in to MySQL first shell> mysql --user=root...

Vue example code using transition component animation effect

Transition document address defines a background ...

Introduction to major browsers and their kernels

Trident core: IE, MaxThon, TT, The World, 360, So...

Page Speed ​​Optimization at a Glance

I believe that the Internet has become an increas...

Zabbix configures DingTalk's alarm function with pictures

Implementation ideas: First of all, the alarm inf...