【问题标题】:Why I get the "omitting directory" error using globbing in cp command? [duplicate]为什么我在 cp 命令中使用 globbing 得到“省略目录”错误? [复制]
【发布时间】:2017-01-02 19:44:16
【问题描述】:

我正在尝试将“file.txt”文件复制到所有目录中。

[root@mycomputer]# ls -lh
total 132K
drwxr--r--.  2 postgres postgres 4.0K Jan  2 08:47 ilo01
drwxr--r--.  2 postgres postgres 4.0K Jan  2 08:40 ilo02
drwxr--r--.  2 postgres postgres 4.0K Jan  2 08:40 ilo03
drwxr--r--.  2 postgres postgres 4.0K Jan  2 08:40 ilo04
drwxr--r--.  2 postgres postgres 4.0K Jan  2 08:40 ilo05
drwxr--r--.  2 postgres postgres 4.0K Jan  2 08:40 ilo06
drwxr--r--.  2 postgres postgres 4.0K Jan  2 08:40 ilo07
drwxr--r--.  2 postgres postgres 4.0K Jan  2 08:40 ilo08
drwxr--r--.  2 postgres postgres 4.0K Jan  2 10:03 ilo09
drwxr--r--. 11 postgres postgres 4.0K Jan  2 11:15 ilo10
-rw-r--r--.  1 postgres postgres  64K Jun 27  2016 file.txt

使用TAB查看要运行的命令的行为:

[root@mycomputer]# cp -p file.txt ilo[0-1][0-9]
ilo01/ ilo02/ ilo03/ ilo04/ ilo05/ ilo06/ ilo07/ ilo08/ ilo09/ ilo10/

但我收到此错误:

[root@mycomputer]# cp -v -p file.txt ilo[0-1][0-9]*
`postgresTdas.txt' -> `ilo10/file.txt'
cp: omitting directory `ilo01'
cp: omitting directory `ilo02'
cp: omitting directory `ilo03'
cp: omitting directory `ilo04'
cp: omitting directory `ilo05'
cp: omitting directory `ilo06'
cp: omitting directory `ilo07'
cp: omitting directory `ilo08'
cp: omitting directory `ilo09'

同样的事情发生在:

[root@mycomputer]# cp -p file.txt ilo*

[root@mycomputer]# cp -p file.txt ilo*/

我不明白为什么“[0-1][0-9] 不能按我需要的方式工作。

我假设副本会将 file.txt 放入 TAB 显示的列表中。

我错过了什么?

【问题讨论】:

  • cp 可以将一个或多个文件复制到单个目标目录中,也可以将文件复制到单个目标文件中。它不能将单个文件复制到多个目标。您需要创建一个循环将文件复制到所有这些目录。
  • 您可以在这里找到答案:stackoverflow.com/questions/195655/…
  • 谢谢。我真的为这个问题感到羞耻:/

标签: linux cp glob


【解决方案1】:

参数扩展为文件+所有目录

cp 将最后一个参数视为目标,因此将其他目录视为源。

因为cp 不会复制目录,除非设置了-r-R 选项(复制目录 内容),您会在除最后一个目录之外的所有目录上收到警告。

我会用 bash/sh 脚本来代替:

for d in ilo[0-1][0-9]
do
   cp -p file.txt $d
done

【讨论】:

  • 天哪,我做了一个布布。所有的答案都是正确的。我把它交给 arkascha 接受,因为他首先回应了它。但是非常感谢。
  • 46 分钟前与 55 分钟前?我先回应。但随心所欲。你是老板 :)
【解决方案2】:

cp 命令的大多数实现能够复制到多个目标。对此你无能为力。您需要多次调用cp 来解决该限制。最简单的可能是这样的:

ls -d dir* | xargs -n 1 cp file.txt

【讨论】:

    猜你喜欢
    • 2013-12-05
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 2014-12-20
    • 2019-10-08
    相关资源
    最近更新 更多