[英]How can I add a string to the beginning of each file in a folder in bash?
[英]How can I create a file in each folder?
我想在我的 linux 项目的每个文件夹中创建一个index.html
文件。
index.html
应该包含一些示例代码。
如何在单个命令中创建文件?
find . -type d -exec touch {}/index.html \;
这将创建一个index.html
在.
和所有子目录。
cd /project_dir && find . -type d -exec touch \{\}/index.htm \;
HTH
假设您在名为“projects.txt”的文件中有一个项目目录列表,您可以执行此操作(对于 bash 和 zsh)
for i in $(cat projects.txt)
do
touch $i/index.html
done
要创建您的 projects.txt,您可以使用find
命令。 您可以直接用find
调用替换cat
,但我认为将这两个操作分开会更清楚。
我知道这是一个老问题,但当前的答案都不允许添加一些示例代码,这里是我的解决方案:
#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 {} \;
以下命令将在当前目录中创建一个空的 index.html 文件
touch index.html
如有必要,您将需要进行适当的循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.