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

时间:2022-05-15 06:39:06

I want to create an index.html file in each folder of my project in linux.

我想创建一个索引。我的项目在linux中的每个文件夹中的html文件。

index.html should contain some sample code.

索引。html应该包含一些示例代码。

How can I create a file in single command?

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

5 个解决方案

#1


19  

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

This'll create an index.html in . and all subdirectories.

这将创建一个索引。html。和所有子目录。

#2


4  

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

HTH

HTH

#3


0  

Assuming you have a list of your project directories in a file called "projects.txt", you can do this (for bash and zsh)

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

for i in $(cat projects.txt)
do
  touch $i/index.html
done.

To create your projects.txt, you can use the find command. You could replace the cat directly with a find invocation but I thought it more clear to separate the two operations.

创建您的项目。txt,可以使用find命令。您可以直接用find调用替换cat,但我认为将这两个操作分开会更清楚。

#4


0  

I know it's an old question but none of the current answers allow to add some sample code, here my solution:

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

#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 {} \;

#5


-1  

The following command will create an empty index.html file in the current directory

下面的命令将创建一个空索引。当前目录中的html文件

touch index.html

You will need to do the appropriate looping if necessary.

如果需要,您将需要执行适当的循环。

#1


19  

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

This'll create an index.html in . and all subdirectories.

这将创建一个索引。html。和所有子目录。

#2


4  

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

HTH

HTH

#3


0  

Assuming you have a list of your project directories in a file called "projects.txt", you can do this (for bash and zsh)

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

for i in $(cat projects.txt)
do
  touch $i/index.html
done.

To create your projects.txt, you can use the find command. You could replace the cat directly with a find invocation but I thought it more clear to separate the two operations.

创建您的项目。txt,可以使用find命令。您可以直接用find调用替换cat,但我认为将这两个操作分开会更清楚。

#4


0  

I know it's an old question but none of the current answers allow to add some sample code, here my solution:

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

#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 {} \;

#5


-1  

The following command will create an empty index.html file in the current directory

下面的命令将创建一个空索引。当前目录中的html文件

touch index.html

You will need to do the appropriate looping if necessary.

如果需要,您将需要执行适当的循环。