[英]How can I create a file in each folder?
I want to create an index.html
file in each folder of my project in linux.我想在我的 linux 项目的每个文件夹中创建一个
index.html
文件。
index.html
should contain some sample code. index.html
应该包含一些示例代码。
How can I create a file in single command?如何在单个命令中创建文件?
find . -type d -exec touch {}/index.html \;
This'll create an index.html
in .
这将创建一个
index.html
在.
and all subdirectories.和所有子目录。
cd /project_dir && find . -type d -exec touch \{\}/index.htm \;
HTH
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.要创建您的 projects.txt,您可以使用
find
命令。 You could replace the cat
directly with a find
invocation but I thought it more clear to separate the two operations.您可以直接用
find
调用替换cat
,但我认为将这两个操作分开会更清楚。
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 {} \;
The following command will create an empty index.html file in the current directory以下命令将在当前目录中创建一个空的 index.html 文件
touch index.html
You will need to do the appropriate looping if necessary.如有必要,您将需要进行适当的循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.