This article uses specific examples to introduce xargs command -I Common usage of parameters .

<>GNU args vs other

Pay attention to different situations xargs There may be differences in details , And these can be man To confirm , such as CentOS 7 lower man The content of , This is GNU Version of xargs

and MacOS The instructions on may be like this :

It should be noted that -I The details and limitations of the options are also slightly different ,GNU xargs Of -I The options are described below :

MacOS Of xargs Of -I Instructions for use are as follows :

We can see that there are some obvious differences between them .

<>-I vs -i

* old and new POSIX Differences in standards
-i yes 1997 Of POSIX Options supported in the standard , And in 2004 Of POSIX Removed from standard , So use as much as possible -I instead of -i.

* Can I specify a placeholder
-i Use default placeholder {}, and -I Can be specified , So there are also differences in use

Use example :
[root@liumiaocn easypack]# cat /etc/redhat-release CentOS Linux release
7.3.1611(Core) [root@liumiaocn easypack]# [root@liumiaocn easypack]# find .
-name "*.tar.gz" |xargs -I {} echo {} ./containers/alpine/redmine/themes.tar.gz
./containers/standard/rancher/k3s/ansible/download/files/pause-amd64-3.1.tar.gz
./k8s/shell/data/pause-amd64-3.1.tar.gz[root@liumiaocn easypack]# [
root@liumiaocn easypack]# find . -name "*.tar.gz" |xargs -i echo {}
./containers/alpine/redmine/themes.tar.gz
./containers/standard/rancher/k3s/ansible/download/files/pause-amd64-3.1.tar.gz
./k8s/shell/data/pause-amd64-3.1.tar.gz[root@liumiaocn easypack]#

in addition , Different versions of xargs Yes -i The treatment is different , For example, in the current MacOS On xargs, Not directly -i parameter , This is also something to consider when migrating scripts . The confirmation results are as follows , You can see the same command in the MacOS On xargs Is not available
liumiaocn:xargs liumiao$ sw_vers ProductName: Mac OS X ProductVersion: 10.15.2
BuildVersion: 19C57 liumiaocn:xargs liumiao$find . -name "*.tar.gz" |xargs -i
echo {} xargs: illegal option -- i usage: xargs [-0opt] [-E eofstr] [-I replstr
[-R replacements]] [-J replstr] [-L number] [-n number [-x]] [-P maxprocs] [-s
size] [utility [argument ...]] liumiaocn:xargs liumiao$
<>-I instructions

man The explanation words used in the content of replace, And in order to better understand ,-I Use to specify placeholders may be easier to accept . First let's take a look at the following example .

* Preparation in advance liumiaocn:easypack liumiao$ find . -name '*.tar.gz'
./k8s/shell/data/pause-amd64-3.1.tar.gz
./containers/alpine/redmine/themes.tar.gz
./containers/standard/rancher/k3s/ansible/download/files/pause-amd64-3.1.tar.gz
liumiaocn:easypack liumiao$
explain : Used to provide input information , You can do whatever you want , For example, you can display all files in a directory directly

* usage method : adopt -I Display
xargs Default use echo Display , Specified here -I Display liumiaocn:easypack liumiao$ find . -name
'*.tar.gz' |xargs -I {} echo {} ./k8s/shell/data/pause-amd64-3.1.tar.gz
./containers/alpine/redmine/themes.tar.gz
./containers/standard/rancher/k3s/ansible/download/files/pause-amd64-3.1.tar.gz
liumiaocn:easypack liumiao$
* And default xargs Comparison of actions
Because the demo here uses echo command , And default xargs Almost the same movement liumiaocn:easypack liumiao$ find . -name
'*.tar.gz' |xargs ./k8s/shell/data/pause-amd64-3.1.tar.gz
./containers/alpine/redmine/themes.tar.gz
./containers/standard/rancher/k3s/ansible/download/files/pause-amd64-3.1.tar.gz
liumiaocn:easypack liumiao$ liumiaocn:easypack liumiao$find . -name '*.tar.gz' |
xargs echo ./k8s/shell/data/pause-amd64-3.1.tar.gz
./containers/alpine/redmine/themes.tar.gz
./containers/standard/rancher/k3s/ansible/download/files/pause-amd64-3.1.tar.gz
liumiaocn:easypack liumiao$
* Understanding of placeholders
By , You can understand the meaning of placeholders more clearly , first , appoint -I Then we don't pass it on echo command , You can see that there are three blank lines liumiaocn:easypack
liumiao$find . -name '*.tar.gz' |xargs -I {} echo liumiaocn:easypack liumiao$
We add the file name before and after the output of the result hello and liumiao String of , You can simply do this by using the following command
liumiaocn:easypack liumiao$ find . -name '*.tar.gz' |xargs -I {} echo hello {}
liumiao hello ./k8s/shell/data/pause-amd64-3.1.tar.gz liumiao hello
./containers/alpine/redmine/themes.tar.gz liumiao hello
./containers/standard/rancher/k3s/ansible/download/files/pause-amd64-3.1.tar.gz
liumiao liumiaocn:easypack liumiao$
This extension allows you to specify a placeholder as a specified location for more than one parameter , such as mv and cp Common commands such as , No more details here .

* adopt -I Specify placeholders
-i Default use {} As placeholder , And new POSIX Placeholders in standards can be -I Assign , Examples of use are as follows : liumiaocn:easypack liumiao$
find . -name '*.tar.gz' |xargs -I @ echo hello @ liumiao hello
./k8s/shell/data/pause-amd64-3.1.tar.gz liumiao hello
./containers/alpine/redmine/themes.tar.gz liumiao hello
./containers/standard/rancher/k3s/ansible/download/files/pause-amd64-3.1.tar.gz
liumiao liumiaocn:easypack liumiao$ liumiaocn:easypack liumiao$find . -name
'*.tar.gz' |xargs -I @@ echo hello @@ liumiao hello
./k8s/shell/data/pause-amd64-3.1.tar.gz liumiao hello
./containers/alpine/redmine/themes.tar.gz liumiao hello
./containers/standard/rancher/k3s/ansible/download/files/pause-amd64-3.1.tar.gz
liumiao liumiaocn:easypack liumiao$

Technology
©2019-2020 Toolsou All rights reserved,
( Essence )2020 year 6 month 26 day C# Class library Exception handling help class ( Essence 2020 year 6 month 2 Daily update ) TypeScript Function explanation vue vue-element-admin Summary of the project ( Essence )2020 year 7 month 15 day Wechat applet template Use of PHP Printout log( Essence )2020 year 8 month 9 day C# Basic knowledge reflex element ui Drop down box search function ( Essence )2020 year 7 month 12 day webpack Use of common plug-ins [work] python read txt Last line of file ( Essence )2020 year 6 month 29 day C# Class library Interface signature verification