bash 将一个文件复制到每个子目录中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15346464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:49:02  来源:igfitidea点击:

Copy one file into every subdirectory

bashshellscripting

提问by Chris

I'm trying to cpone file: index.phpinto all subdirectories, and subdirectories of those subdirectories, and so on, so that every child directory of the root has index.php

我正在尝试cp一个文件:index.php进入所有子目录,以及这些子目录的子目录,等等,这样根的每个子目录都有index.php

I started with this:

我从这个开始:

for d in */; do cp index.php "$d"; done; 

Which only worked for the top subdirectories. I tried to nest it in itself a few times like this:

这只适用于顶级子目录。我试着像这样将它嵌套几次:

for d in */; do cp index.php "$d"; for e in */; do cp index.php "$e";for f in */; do cp index.php "$f"; done; done; done

But that didn't seem to do anything

但这似乎没有任何作用

回答by Gilles Quenot

Try this :

尝试这个 :

find . -type d -exec cp index.php {} \;

Note

笔记

  • -type dfind all dirs and sub-dirs
  • -type d查找所有目录和子目录

回答by DigitalRoss

sputnick's answer is nice and simple. For the record, here is one way to do it with a shell function. You might want something like this if the operation is complicated or conditional.

sputnick的回答很好也很简单。作为记录,这是使用 shell 函数执行此操作的一种方法。如果操作复杂或有条件,您可能需要这样的操作。

t=$PWD/index.php

recurse () {
  for i in */.; do
    if [ "./$i" != './*/.' ]; then
      (cd "./$i" && cp "$t" . && recurse)
    fi
  done
}

recurse