Question

I've a directory structure like this:

/posts
|
|
posts/page1 -- index.html
posts/page2 -- index.html
posts/page{3..100} -- index.html

I've figured out how to create 100 different page# directories, and I've touched index.html to each of them. However, I need to echo some basic HTML to each of the index.html files in each of the page# directories.

 for f in `find . -type d`; do echo "hello" >> f; done

but all that does is echo 100 hellos to a file named f

 for f in `find . -type d`; do echo "hello"; done 

simply echos hello 100 times.

and

for f in `find . -type d`; do echo "hello" >> index.html; done

just echos hello 100 times within posts/index.html

Anyway, I'm at a loss how to do this. How would I do this without having to manually do it? Theoretically I could open each folder in Sublime and copy the HTML then Ctrl+V + Ctrl+S + Ctrl+W for each instance, but there's gotta be a much easier way to do this.

Was it helpful?

Solution

You just need to use $f to refer the items in your find result, instead of f. And also, include /index.html to the path:

for f in $(find . -type d); do echo "hello" >> $f/index.html; done
                                               ^^^^^^^^^^^^^

In your current code you were redirecting to the constant f, while you want to refer to the variable name $f. That is the reason of using $f instead of f.

OTHER TIPS

> for f in `find . -type d`; do echo "hello" >> index.html; done

Command substitution is subject to word splitting and pathname expansion (globbing). This fails when filenames contain spaces (and/or wildcards). Either set IFS to a single newline (IFS=$'\n') and disable pathname expansion (set -f) (N.B. this will still fail if filenames contain newlines) or use find's -exec:

find . -type d -exec sh -c 'for dir in "$@" ; do echo "hello" >> "$dir/index.html" ; done' sh {} +
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top