Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash脚本-cp for loop找不到确实存在的目录_Bash_Loops_For Loop_Copy_Cp - Fatal编程技术网

Bash脚本-cp for loop找不到确实存在的目录

Bash脚本-cp for loop找不到确实存在的目录,bash,loops,for-loop,copy,cp,Bash,Loops,For Loop,Copy,Cp,这是我的第一篇帖子,如果我做错了什么,现在就告诉我吧 我在bash中尝试在循环中使用cp时遇到了一个问题,我想知道是否有人可以帮助我?我似乎找不到一个确实存在的目录 我正在尝试在一组类似的子目录路径中获取一些.pdf文件,并将它们复制到一个易于查看的文件中 下面是我的脚本的样子: shopt -s globstar s_dir="C:/Bash Testing/CG SIS Standard Items" t_dir="C:/Bash Testing/BoxFolder" REFS=$(f

这是我的第一篇帖子,如果我做错了什么,现在就告诉我吧

我在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'/

提前谢谢。如果您需要更多信息,请告诉我,您可能需要修复“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提供的其他信息(下面的注释),目标似乎是将树结构中的所有文件复制到单个文件夹中。使用组合的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"

对于任何一个跌跌撞撞地想找到答案的人:

似乎xargs不喜欢多个文件或文件位于新行上

但是-exec工作得很好

这是我的代码(非常简单,大概花了5个小时摸索)


谢谢你回复我。因此,当我修复引号时,我在bash
cp:cannot stat'':没有这样的文件或目录
,当我将“Testing/BoxFolder/”替换为“.”时,我得到了同样的结果(我尝试了带引号和不带引号的句号,只是为了确定)“有什么想法吗?再次感谢surface,看起来文件被设置为空字符串。请参阅关于如何简化脚本的修订答案。特别是,我不确定排序文件列表或处理${REFS[@}是否会对您有任何好处。您是否还可以澄清您希望将文件复制到的位置-当前文件夹(.)或者一个特定的文件夹(Testing/BoxFolder/)。您好,谢谢您再次回复我。我正在尝试将其复制到“Testing/BoxFolder/”更新2是我第一次尝试的方法之一。但我对xargs及其所有选项还不太感兴趣。更新2遇到了一个问题,它将exmpty空格视为值的结尾和新值的开始。请参见下面所有文件路径的拆分位置。我们
cp:cannot stat'C:/Bash::没有这样的文件或目录cp:cannot stat'Testing/CG':没有这样的文件或目录cp:无法统计“SIS”:没有这样的文件或目录cp:无法统计“标准”:没有这样的文件或目录…
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" \;