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

时间:2013-03-11 19:02:36

标签: bash shell scripting

我正在尝试cp一个文件:index.php进入所有子目录,以及这些子目录的子目录,依此类推,以便根目录的每个子目录都有index.php < / p>

我从这开始:

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

仅适用于顶级子目录。我试着将它自己嵌入几次:

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

但那似乎没有做任何事情

2 个答案:

答案 0 :(得分:8)

试试这个:

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

注意

  • -type d找到所有dirs和sub-dirs

答案 1 :(得分:1)

sputnick的答案很简单。对于记录,这是使用shell函数执行此操作的一种方法。如果操作很复杂或有条件,你可能想要这样的东西。

t=$PWD/index.php

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

recurse
相关问题