


xargs is a filter for passing parameters to commands, and is also a tool for combining multiple commands. The following article mainly introduces to you the relevant information about the usage of xargs command in linux. Friends who need it can refer to it. Let’s take a look with the editor.
Preface
The xargs command reformats the received data and then provides it as a parameter to other commands. The xargs command is introduced below. Let’s take a look at various techniques for using commands.
1. Convert multi-line input into single-line input:
[root@host1 test]# echo -e "1 2 3 4 5 \n6 7 8 \n9 10 11 12" >example.txt [root@host1 test]# cat example.txt 1 2 3 4 5 6 7 8 9 10 11 12 [root@host1 test]# cat example.txt |xargs 1 2 3 4 5 6 7 8 9 10 11 12
Convert single-line input into multi-line output:
[root@host1 test]# cat example.txt | xargs -n 3 1 2 3 4 5 6 7 8 9 10 11 12
Customize the delimiter for conversion (the default delimiter is a space):
[root@host1 test]# echo "Hello:Hello:Hello:Hello" | xargs -d : -n 2 Hello Hello Hello Hello
2. Use it in the script:
[root@host1 test]# cat echo.sh #!/bin/bash echo $* '^-^'
When the parameters are passed to echo.sh
, it will print out these parameters and end with "^-^":
[root@host1 test]# echo -e "Tom\nHarry\nJerry\nLucy" > args.txt [root@host1 test]# cat args.txt | xargs bash echo.sh Tom Harry Jerry Lucy ^-^ [root@host1 test]# cat args.txt | xargs -n 2 bash echo.sh Tom Harry ^-^ Jerry Lucy ^-^
In the above example, we The parameter sources are put into the args.txt file, but in addition to these parameters, we also need some fixed parameters, such as:
[root@host1 test]# bash echo.sh Welcome Tom Welcome Tom ^-^
During the execution of the above command, Tom is the variable , the rest are constants, we can extract the parameters from "args.txt" and provide them to the command in the following way:
[root@host1 test]# bash echo.sh Welcome Tom [root@host1 test]# bash echo.sh Welcome Herry [root@host1 test]# bash echo.sh Welcome Jerry [root@host1 test]# bash echo.sh Welcome Lucy
At this time we need to use xargs -I command:
[root@host1 test]# cat args.txt | xargs -I {} bash echo.sh Welcome {} Welcome Tom ^-^ Welcome Harry ^-^ Welcome Jerry ^-^ Welcome Lucy ^-^
-I {} specifies the replacement of string. For each command parameter, the string {} will be replaced by the parameters read from stdin. ,
When -I is used, the command is executed in a loop. If there are 4 parameters, the command will be executed 4 times together with {}, in each execution {} will be replaced with the corresponding parameters.
3. Use with find
xargs and find are a very good combination, but we usually use it in the wrong way Ways to use them, such as:
[root@host1 test]# find . -type f -name "*.txt" -print | xargs rm -f
This is dangerous, sometimes delete unnecessary files, if the file name contains a space character (' '), then xargs It is possible to think that they are delimiters (e.g. file text.txt will be mistaken for file and text.txt by xargs).
If we want to use the output of find as the input of xargs, we must use -print0 in conjunction with find to separate the output with the characters null ('\0'), and use find to find out All .txt files, and then use xargs to delete these files:
[root@host1 test]# find . -type f -name "*.txt" -print0 | xargs -0 rm -f
This way you can delete all .txt files, xargs -0
Use \0 as the input delimiter.
4. Use the while statement and subshell
[root@host1 test]# cat files.txt | (while read arg ;do cat $arg;done)
This command is equivalent to:
[root@host1 test]# cat files.txt | xargs -I {} cat {}
In while loop, cat $arg
can be replaced with any number of commands, so that we can execute multiple commands on the same parameter, or we can use The output is passed to other commands. This technique is suitable for a variety of problem scenarios. Multiple commands within subshellOperator can be run as a whole.
Summarize
The above is the detailed content of Detailed explanation of various uses of xargs command techniques in Linux. For more information, please follow other related articles on the PHP Chinese website!

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

手机远程linux工具有:1、JuiceSSH,是一款功能强大的安卓SSH客户端应用,可直接对linux服务进行管理;2、Termius,可以利用手机来连接Linux服务器;3、Termux,一个强大的远程终端工具;4、向日葵远程控制等等。

linux中,lsb是linux标准基础的意思,是“Linux Standards Base”的缩写,是linux标准化领域中的标准;lsb制定了应用程序与运行环境之间的二进制接口,保证了linux发行版与linux应用程序之间的良好结合。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download
The most popular open source editor