Linux 如何在每个文件夹中创建文件?

Linux 如何在每个文件夹中创建文件?,linux,Linux,我想在linux项目的每个文件夹中创建一个index.html文件 index.html应该包含一些示例代码 如何在单个命令中创建文件 find . -type d -exec touch {}/index.html \; 这将在和所有子目录中创建index.html。假设您在名为“projects.txt”的文件中有一个项目目录列表,您可以这样做(对于bash和zsh) 要创建projects.txt,可以使用find命令。您可以用find调用直接替换cat,但我认为将这两个操作分开比较清楚

我想在linux项目的每个文件夹中创建一个
index.html
文件

index.html
应该包含一些示例代码

如何在单个命令中创建文件

find . -type d -exec touch {}/index.html \;

这将在
和所有子目录中创建
index.html

假设您在名为“projects.txt”的文件中有一个项目目录列表,您可以这样做(对于bash和zsh)


要创建projects.txt,可以使用
find
命令。您可以用
find
调用直接替换
cat
,但我认为将这两个操作分开比较清楚

以下命令将在当前目录中创建一个空index.html文件

touch index.html
如有必要,您需要进行适当的循环

cd /project_dir && find . -type d -exec touch \{\}/index.htm \;

HTH

我知道这是一个老问题,但当前的答案都不允许添加一些示例代码,下面是我的解决方案:

#create a temp file
echo "<?php // Silence is golden" > /tmp/index.php

#for each directory copy the file
find /mydir -type d -exec cp /tmp/index.php {} \;

#Alternative : for each directory copy the file where the file is not already present
find /mydir -type d \! -exec test -e '{}/index.php' \; -exec cp /tmp/index.php {} \;
#创建临时文件

echo“在许多shell中,
{}
被扩展,导致在
/
处创建。更好的版本是
find.-type d-exec touch'{}/index.html'\;
。单引号可以防止任何shell扩展。
#create a temp file
echo "<?php // Silence is golden" > /tmp/index.php

#for each directory copy the file
find /mydir -type d -exec cp /tmp/index.php {} \;

#Alternative : for each directory copy the file where the file is not already present
find /mydir -type d \! -exec test -e '{}/index.php' \; -exec cp /tmp/index.php {} \;