【问题标题】:Bash Script - cp for loop cant find directory which is definitely thereBash Script - cp for loop 找不到绝对存在的目录
【发布时间】:2019-11-12 17:24:36
【问题描述】:

这是我的第一篇文章,所以如果我做错了什么,请让我现在!

我在 bash 中尝试在循环中使用 cp 时遇到问题,想知道是否有人可以提供帮助?我似乎无法让它找到一个绝对存在的目录

我正在尝试在一组类似的子目录路径中获取一些 .pdf 文件并将它们复制到一个易于查看的文件中。

这是我的脚本的样子:

shopt -s globstar

s_dir="C:/Bash Testing/CG SIS Standard Items"

t_dir="C:/Bash Testing/BoxFolder"

REFS=$(find "$s_dir"** -iname "*.pdf" -ipath "*/References/*" -not -ipath "*/Archive/*")


for ref in "${REFS[@]}"
do
    SORTED_REFS="$SORTED_REFS $ref"
done

COPY_REFS=$( "$SORTED_REFS" | sort -u )

echo "$COPY_REFS"

for file in "$COPY_REFS"
do
    cp -v "$file  C:/Bash Testing/BoxFolder/" .
done

这就是我运行脚本后 GIT bash 的样子,包括 pwd & ls 的结果,这样您就可以更好地理解文件夹结构。

./script.sh: line 47:  C:/Bash Testing/CG SIS Standard Items/00 General/References/CG SiS Standard Items - Material Board.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.001_Short Rail, 1368mm/References/CG.S.001_Short Rail, 1368mm.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.002_Short Base/References/CG.S.002_Short Base.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.003_Square Base/References/CG.S.003_Square Base.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.004_Long Rail, 1368mm/References/CG.S.004_Long Rail, 1368mm.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.005_Long Base/References/CG.S.005_Long Base.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.006_Short Rail, 1528mm/References/CG.S.006_Short Rail, 1528mm.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.007_Shelf Add On/References/CG.S.007_Shelf Add On.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.008_Storage Table/References/CG.S.008_Storage Table.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.009_Nesting Table/References/CG.S.009_Nesting Table.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.010_Storage Block/References/CG.S.010_Storage Block.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.011_Long Rail, 1528mm/References/CG.S.011_Long Rail, 1528mm.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.012_Wall Mounted Frame/References/CG.S.012_Wall Mounted Frame.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.013_Short Base, Slim/References/CG.S.013_Short Base, Slim.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.014_Long Base, Slim/References/CG.S.014_Long Base, Slim.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.015_Mannequin Stand/References/CG.S.015_Mannequin Stand.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.016_Accessory Unit/References/CG.S.016_Accessory Unit.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.018_End Rail/References/CG.S.018_End Rail.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.019_Tall Nesting Table/References/CG.S.019_Tall Nesting Table.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.020_End Rail Cap/References/CG.S.020_End Rail Cap.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.022_Wall Mounted Frame Rail/References/CG.S.022_Wall Mounted Frame Rail.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.023_Wall Mounted Frame Shelf/References/CG.S.023_Wall Mounted Frame Shelf.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.024_Wall Mounted Frame Top Shelf/References/CG.S.024_Wall Mounted Frame Top Shelf.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.025_Wall Mounted Frame Large/References/CG.S.025_Wall Mounted Frame Large.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.026_Wall Mounted Frame Large Rail/References/CG.S.026_Wall Mounted Frame Large Rail.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.027_Wall Mounted Frame Large Shelf/References/CG.S.027_Wall Mounted Frame Large Shelf.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.028_Wall Mounted Frame Large Top Shelf/References/CG.S.028_Wall Mounted Frame Large Top Shelf.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.029_Wall Mounted Frame Face Out Arm/References/CG.S.029_Wall Mounted Frame Face Out Arm REV 1.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.029_Wall Mounted Frame Face Out Arm/References/CG.S.029_Wall Mounted Frame Face Out Arm.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.030_Wall Mounted Frame Cabinet/References/CG.S.030_Wall Mounted Frame Cabinet.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.030_Wall Mounted Frame Cabinet/References/CG.S.030_Wall Mounted Frame Cabinet_NEW DIVIDER.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.031_Rail Face Out/References/CG.S.031_Rail Face Out REV1.pdf
C:/Bash Testing/CG SIS Standard Items/CG.S.031_Rail Face Out/References/CG.S.031_Rail Face Out.pdf: No such file or directory

cp: cannot stat '  C:/Bash Testing/BoxFolder/': No such file or directory
>> pwd
/c/Bash Testing
>> ls
 {testfile}.txt}  '~bash_profile'   1.txt   2.txt   3.txt   4.txt   5.txt   BoxFolder/  'CG SIS Standard Items'/   script.sh*   test.sh
>> ls 'CG SIS Standard Items'
'00 Account Management'/        'CG.S.009_Nesting Table'/       'CG.S.022_Wall Mounted Frame Rail'/
'00 General'/                   'CG.S.010_Storage Block'/       'CG.S.023_Wall Mounted Frame Shelf'/
'00 Item Archive'/              'CG.S.011_Long Rail, 1528mm'/   'CG.S.024_Wall Mounted Frame Top Shelf'/
'CG.S.001_Short Rail, 1368mm'/  'CG.S.012_Wall Mounted Frame'/  'CG.S.025_Wall Mounted Frame Large'/
'CG.S.002_Short Base'/          'CG.S.013_Short Base, Slim'/    'CG.S.026_Wall Mounted Frame Large Rail'/
'CG.S.003_Square Base'/         'CG.S.014_Long Base, Slim'/     'CG.S.027_Wall Mounted Frame Large Shelf'/
'CG.S.004_Long Rail, 1368mm'/   'CG.S.015_Mannequin Stand'/     'CG.S.028_Wall Mounted Frame Large Top Shelf'/
'CG.S.005_Long Base'/           'CG.S.016_Accessory Unit'/      'CG.S.029_Wall Mounted Frame Face Out Arm'/
'CG.S.006_Short Rail, 1528mm'/  'CG.S.018_End Rail'/            'CG.S.030_Wall Mounted Frame Cabinet'/
'CG.S.007_Shelf Add On'/        'CG.S.019_Tall Nesting Table'/  'CG.S.031_Rail Face Out'/
'CG.S.008_Storage Table'/       'CG.S.020_End Rail Cap'/        'CG.S.032_ Plinths'/

提前致谢。如果您需要更多信息,请告诉我

【问题讨论】:

    标签: bash loops for-loop copy cp


    【解决方案1】:

    您可能想修正“cp”上的引用。看起来您希望将所有文件复制到“C:/Bash Testing/BoxFolder/”,但副本指定“。”作为目标文件夹。

    Instead of:
     cp -v "$file  C:/Bash Testing/BoxFolder/" .
    
    Use:
     cp -v "$file" "C:/Bash Testing/BoxFolder/"
    

    如果您想将所有文件放入当前文件夹,请将“Testing/BoxFolder/”替换为“.”

    更新 1: 根据来自 OP(下面的 cmets)的其他信息,看起来目标是将树结构中的所有文件复制到单个文件夹中。这可以使用组合的 find/xargs/cp 更有效地实现。

    shopt -s globstar
    
    s_dir="C:/Bash Testing/CG SIS Standard Items"
    t_dir="C:/Bash Testing/BoxFolder"
    
    find "$s_dir"** -iname "*.pdf" -ipath "*/References/*" -not -ipath "*/Archive/*" | xargs -L50 cp -t "$t_dir"
    

    更新 2:globstar 从表面上看,没有必要使用 globstar 和 find。由于 find 已被使用,globstar 和 '**' 可以删除。

    s_dir="C:/Bash Testing/CG SIS Standard Items"
    t_dir="C:/Bash Testing/BoxFolder"
    
    find "$s_dir" -iname "*.pdf" -ipath "*/References/*" -not -ipath "*/Archive/*" | xargs -L50 cp -t "$t_dir"
    

    【讨论】:

    • 感谢您回复我。所以当我修复引号时,我在 bash cp: cannot stat '': No such file or directory 中得到了这个,当我用“。”替换“测试/BoxFolder/”时,我得到了同样的结果(我尝试了带引号和不带引号的句号,只是为了确定“有什么想法吗?再次感谢
    • 从表面上看,文件似乎设置为空字符串。请参阅有关如何简化脚本的修订答案。特别是,我不确定您从排序文件列表或处理 ${REFS[@} 中获得什么好处
    • 您能否澄清一下您希望将文件复制到何处 - 当前文件夹 (.) 或特定文件夹 (Testing/BoxFolder/)。
    • 您好,感谢您再次回复我。我正在尝试将其复制到“Testing/BoxFolder/”更新 2 是我尝试它的第一种方法。不过,我对 xargs 及其所有选项还不是很热。使用更新 2,我遇到了一个问题,它将空白空间视为值的结尾和新值的开始。请参阅下面的所有文件路径已拆分我们cp: cannot stat 'C:/Bash': No such file or directory cp: cannot stat 'Testing/CG': No such file or directory cp: cannot stat 'SIS': No such file or directory cp: cannot stat 'Standard': No such file or directory ...
    【解决方案2】:

    对于任何偶然发现此问题并寻找答案的人:

    似乎 xargs 不喜欢多个文件或文件在新行上...

    但 -exec 效果很好

    这是我的有效代码(简单得令人讨厌。一定花了大约 5 个小时来摸索这个)

    s_dir="C:/Bash Testing/CG SIS Standard Items"
    t_dir="C:/Bash Testing/BoxFolder"
    
    find "$s_dir" -iname "*.pdf" -ipath "*/References/*" -not -ipath "*/Archive/*"  -exec cp {} "$t_dir" \;
    

    【讨论】:

      猜你喜欢
      • 2011-04-30
      • 1970-01-01
      • 1970-01-01
      • 2020-12-20
      • 2016-11-02
      • 2020-08-09
      • 2012-03-30
      • 2013-02-06
      • 2013-10-31
      相关资源
      最近更新 更多