【问题标题】:Copy multiple files from one directory to multiple other directories将多个文件从一个目录复制到多个其他目录
【发布时间】:2015-10-15 06:09:32
【问题描述】:

我有一个目录结构

Dir_1
Dir_2
Dir_3
Source

。目录Source 包含文件File_1.txtFile_2.txt

我想将目录Source 中的所有文件复制到所有剩余目录,在本例中为Dir_1Dir_2Dir_3

为此,我使用了

for i in $(ls -d */ | grep -v 'Source'); do echo $i | xargs -n 1 cp ./Source/*; done

。但是,我不断收到消息

cp: target ‘5’ is not a directory

似乎cp 中包含空格的目录名称存在问题。我该如何解决这个问题(显然,在目录名称中保留空格)?

【问题讨论】:

    标签: bash for-loop ls xargs cp


    【解决方案1】:

    使用find 你可以这样做:

    find . -mindepth 1 -maxdepth 1 -type d ! -name Source -exec cp Source/*.txt {} \;
    

    此命令在当前目录中搜索所有子目录的深度一级,不包括Source,然后将文本文件复制到每个目录中。

    希望这会有所帮助:)

    【讨论】:

    • 糟糕!很好的捕捉@Cyrus!
    猜你喜欢
    • 2012-03-04
    • 2017-11-11
    • 2015-06-23
    • 2010-10-09
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    相关资源
    最近更新 更多