AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 741724
Accepted
Malachy_P
Malachy_P
Asked: 2023-04-02 21:00:19 +0800 CST2023-04-02 21:00:19 +0800 CST 2023-04-02 21:00:19 +0800 CST

xargs -i 和 -I 之间的区别

  • 772

我明白,-i但我不知道它与手册有何不同-I,也不知道为什么手册说它-i已被弃用。例如,使用下面两行有什么区别:

ls -t | tail -n 4 | xargs -I{} mv {} test2/

ls -t | tail -n 4 | xargs -i mv {} test2/

对于第一行,是否可以将任何内容替换为第一{}行-I,或者仅仅是语法?

谢谢

xargs
  • 1 1 个回答
  • 378 Views

1 个回答

  • Voted
  1. Best Answer
    terdon
    2023-04-02T21:13:09+08:002023-04-02T21:13:09+08:00

    它被弃用的原因是因为维护者决定xargs它应该被弃用。没有更多要说的了1。man xargs这些是我的 Arch 系统的相关部分:

           -I replace-str
                  Replace occurrences of  replace-str  in  the  initial-arguments
                  with  names read from standard input.  Also, unquoted blanks do
                  not terminate input items; instead the separator is the newline
                  character.  Implies -x and -L 1.
    
           -i[replace-str], --replace[=replace-str]
                  This  option  is  a synonym for -Ireplace-str if replace-str is
                  specified.  If the replace-str argument is missing, the  effect
                  is  the  same  as  -I{}.  This option is deprecated; use -I in‐
                  stead.
    

    -i因此,和之间的唯一区别-I是-i没有参数的处理方式与-I{}. 字符串{}经常用于这种事情,已经成为一种约定俗成。例如,在其操作中也以类似的方式find使用:{}-exec

          -exec command {} +
                  This variant of the -exec action runs the specified command  on
                  the  selected files, but the command line is built by appending
                  each selected file name at the end; the total number of invoca‐
                  tions  of  the  command  will  be  much less than the number of
                  matched files.  The command line is built in much the same  way
                  that xargs builds its command lines.  Only one instance of `{}'
                  is allowed within the command, and it must appear at  the  end,
                  immediately before the `+'; it needs to be escaped (with a `\')
                  or quoted to protect it from interpretation by the shell.   The
                  command  is executed in the starting directory.  If any invoca‐
                  tion with the `+' form returns a non-zero value as exit status,
                  then  find  returns a non-zero exit status.  If find encounters
                  an error, this can sometimes cause an immediate exit,  so  some
                  pending  commands  may  not  be  run  at  all.  For this reason
                  -exec my-command ... {} + -quit may not  result  in  my-command
                  actually being run.  This variant of -exec always returns true.
    

    然而,这只是一个约定,因此xargs并不强制您使用它。您可以使用任何东西代替{}xargs' -I。例如:

    $ seq 5 | xargs -I'&' echo "I read &"
    I read 1
    I read 2
    I read 3
    I read 4
    I read 5
    

    甚至:

    $ seq 5 | xargs -I'a' echo "I read a"
    I re1d 1
    I re2d 2
    I re3d 3
    I re4d 4
    I re5d 5
    

    因此,当给定一个字符串时,该-i选项要么 100% 等同于-I,要么是 的特定情况的简写-I{},这意味着它告诉您xargs使用{}它,就好像您已经明确指定了它一样:

    $ seq 5 | xargs -i'a' echo "I read a"  ## as above
    I re1d 1
    I re2d 2
    I re3d 3
    I re4d 4
    I re5d 5
    
    $ seq 5 | xargs -i echo "I read {}"  ## implies -I{}
    I read 1
    I read 2
    I read 3
    I read 4
    I read 5
    
    $ seq 5 | xargs -I echo "I read {}"  ## breaks because -I requires a string
    xargs: I read {}: No such file or directory
    

    显然,维护者认为xargs这不再是一个好主意,因此该选项现在已被弃用,并且可能会从未来的版本中删除。


    1原来还有更多要说的。Jörg W Mittag在评论(现已删除)中指出以下内容:

    请注意,不仅仅是 xargs 的维护者决定了这一点。-i 也因为“过时”而从 SUSv6 (2004) 中删除。即使 -I 不是 SUS 核心的一部分,它也只是定义为(可选)XSI 扩展的一部分。因此,即使是保守的标准作者也认为它太古老了,已经差不多二十年了。

    • 10

相关问题

  • 如何将 xargs 与需要用户输入的命令一起使用?

  • 如何将逗号分隔的列表作为参数提供给下一个命令

  • 这个 xargs 命令有什么问题?

  • 如何使用 ls 的输出打开最后修改的文件?

  • Xargs `-J` 选项

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve