简体   繁体   English

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

[英]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.

相关问题 如何在bash文件夹中的每个文件的开头添加一个字符串? - How can I add a string to the beginning of each file in a folder in bash? 如何为文件的每一行创建文件夹? - How to create folder per each line of a file? Linux shell:LOOP用于在每个文件夹中创建文件 - Linux shell: LOOP for create file in each folder 如何创建77个文件,其内容是每个文件的名称? - How can I create 77 files the content of which is the name of each file? 如何在现有文件夹树中(在每个现有文件夹中)递归创建文件夹? - How to create a folder recursively in an existing tree of folders (in each existing folder)? 如何在Linux或Perl中为每台计算机创建唯一标识符? - How can I create unique identifiers for each machine in Linux or Perl? 如何在文件夹层次结构中找到所有不同的文件扩展名? - How can I find all of the distinct file extensions in a folder hierarchy? 如何在 Ubuntu 中创建 .env 文件? - How can I create a .env file in Ubuntu? 如何对目录中的所有文件执行命令,并将每个文件的输出移至新文件夹? - How do I conduct a command on all the files in a directory and move the output for each file to a new folder? 如何将字符串添加到文件中每一行的开头? - How can I prepend a string to the beginning of each line in a file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM