如何在每个目录和子目录中添加一个文件

huangapple go评论56阅读模式
英文:

how add a file in every dir and subdir

问题

我需要将文件放在每个目录和子目录中。

我在Mac上使用Bash和Brew。

我尝试了这个命令:

for dir in \`//Users/oscarristolfi/Downloads/roma/\* . -type d\` ; do cp index.html   ; done

我已经尝试过了,但它只显示回显,结果如下:

/Users/oscarristolfi/Downloads/roma/copia.sh: line 7: //Users/oscarristolfi/Downloads/roma/cerco-amici: is a directory

我尝试添加一个目录变量,但没有效果:

for dir in \`//Users/oscarristolfi/Downloads/roma/\* . -type d\` ; do cp index.html  $dir/index.html ; done
英文:

i need to put file in every dir and sub dir

i use bash on mac with brew

i tried this

for dir in \`//Users/oscarristolfi/Downloads/roma/\* . -type d\` ; do cp index.html   ; done

i' have tried but it show only echo, result

/Users/oscarristolfi/Downloads/roma/copia.sh: line 7: //Users/oscarristolfi/Downloads/roma/cerco-amici: is a directory

i try tu add a dir variable but nothing

for dir in \`//Users/oscarristolfi/Downloads/roma/\* . -type d\` ; do cp index.html  $dir/index.html ; done

答案1

得分: 1

我会使用find而不是for
假设在运行find的当前目录中存在index.html

find //Users/oscarristolfi/Downloads/roma/* -type d -exec cp index.html {} \;

cp添加选项:

  • -f,如果你想强制覆盖目标目录中已存在的index.html
  • -n,如果你不想覆盖目标目录中已存在的index.html

我不熟悉MacOS和路径中的//Users部分,但我假设它是某种链接到放置家目录的顶部的方式。

英文:

I would use find instead of for.
Given that index.html exists in the current directory where you run find from:

find //Users/oscarristolfi/Downloads/roma/* -type d -exec cp index.html {} \;

Add options to cp:

  • -f if you want to force overwriting an existing index.html in the target directory.
  • -n if you don't want to overwrite an existing index.html in the target directory.

<sup>I'm not familiar with MacOS and the //Users part of the path, but I'm assuming it's some sort of link to the top of where the home directories are placed.</sup>

huangapple
  • 本文由 发表于 2023年6月22日 03:25:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定