【问题标题】:"xargs -a file" to copy files to a folder"xargs -a file" 将文件复制到文件夹
【发布时间】:2016-01-31 13:42:19
【问题描述】:

我正在尝试使用xargs -a 来读取其中包含文件名列表的文件的内容。

我的工作目录如下:

backups
file1.bak
file2.bak
file3.bak
bakfiles.txt

包含文件名的文件名:bakfiles.txt

bakfiles.txt 内容:

file1.bak
file2.bak
file3.bak

所以基本上我正在尝试将file1.bak,file2.bak,file3.bak 复制到文件夹backups 中。但是使用bakfiles.txt的内容来做到这一点。

我试过了:

xargs -a bakfiles.txt | cp {} backups

但我得到了错误:

cp: cannot stat `{}': No such file or directory

我应该提到我也尝试过:

xargs -a bakfiles.txt cp {} backups

并得到错误:

cp: target `file3.bak' is not a directory

【问题讨论】:

  • 第一次尝试肯定是错的。通过查看 xargs 联机帮助页,在您第二次尝试时,尝试在大括号周围放置单个刻度。比如:xargs -a bakfiles.txt -I'{}' cp '{}' 备份
  • 那行不通。不过感谢您的帮助!
  • 我刚刚在 tsch 和 bash 中尝试过,对我来说效果很好。您是否使用了我评论的最新版本?我不得不多次更新它,因为我一直把它搞砸了。
  • 我找到了一种使用 cp -t 文件夹的方法,感谢您的帮助!

标签: linux xargs cp


【解决方案1】:

这适用于我在 Windows 7 上使用 mks 工具包版本的“xargs”

cat bakfiles.txt | xargs -I '{}' cp '{}' backups/'{}'

【讨论】:

  • 这在 CentOS 版本 6.8 (Final) 和 tcsh shell 上对我有用。
【解决方案2】:

从下面的链接我知道了:

https://superuser.com/questions/180251/copy-list-of-files

命令如下:

xargs -a bakfiles.txt cp -t 备份

【讨论】:

    猜你喜欢
    • 2010-12-18
    • 2017-12-08
    • 2013-03-09
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    • 2021-12-22
    相关资源
    最近更新 更多