簡體   English   中英

如何在每個文件夾中創建一個文件?

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM