whiterose
whiterose

Reputation: 4711

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.

index.html should contain some sample code.

How can I create a file in single command?

Upvotes: 16

Views: 14882

Answers (5)

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72755

Assuming you have a list of your project directories in a file called "projects.txt", you can do this (for bash and 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.

Upvotes: 2

fchastanet
fchastanet

Reputation: 57

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

Upvotes: 1

Zsolt Botykai
Zsolt Botykai

Reputation: 51593

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

HTH

Upvotes: 4

mdec
mdec

Reputation: 5242

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

touch index.html

You will need to do the appropriate looping if necessary.

Upvotes: -2

Erik
Erik

Reputation: 91270

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

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

Upvotes: 32

Related Questions