Find directories not containing foo, and copy foo to them

Hello all,

I have a situation where I have a web root directory with a few thousand users spread out into 100 subdirectories in a 00/firstname.lastname, 01/firstname.lastname, etc. hierarchy. I suddenly need to make sure that each of these user directories contains a default index.html file (about 1/3 of them don't).

I'm kind of a scripting newbie, so I'm having trouble with the mechanics. The thing that's most perplexing to me is to do a find in [00-99] -maxdepth=2 and search for all user directories that do not contain an index file (index.htm* and/or homepage.html).

I think if I can figure out how to search on that string, I can figure out how to put the index file where it doesn't exist, so what I really need to know is how to search for what's missing.

Thanks in advance for any help.

Dave

Presumably there is a way to do it with "find" by NOTting a clause and I'm fairly sure someone will point it out soon.

Still, there is a way to do it with a shell script and since this part of the forum is about scripting you could try this:

  1. It is easy to cycle through all your directories by doing an "ls -1" and feed that to a while-loop:
ls -1 <yourstartdir> | while read dir ; do
     print - "$dir"
done

If you have 2 levels of directories you can nest 2 of these loops to get what you want.

Now, there is an option to "test", "-f", which is true, if a file with this name does exist. We could even exchange it to "-r", which is only true if a file exists and is available for read-access (i suppose you know that "test -r" and "[ -r ]" is the same, don't you?):

ls -1 <yourstartdir> | while read dir ; do
     if [ -r "$dir/index.html" ] ; then
          print - "in dir $dir a readable index.html is missing"
     fi
done

You should be able to work your way from there, replace the "print - ..." statement with whatever you want to do, copy the file there, write to a log, etc.

Hope this helps.

bakunin

find -type d -maxdepth 2 -mindepth 2 |
while read dir; do
    if [ ! -e $dir/index.html ]; then
        cp template.html $dir/index.html
    fi
done

@pixelbeat: Ahem, but "-e" only tests the existence. It would be true even if "index.html" is not a file but a directory, symbolic link, FIFO, ...

True, the possibility of this happening is remote, but .... ;-))

bakunin

true. Should use -f instead.

Thanks for all the replies. I'm going to try a few things out and see how far I get. I'll post back what I wound up using.

Regards,

Dave

Thanks for the help. I got it sorted. Here's what I used:

#!/bin/sh
set -x
for i in `cat /data/dev/a-file-containing-a-listing-of-the-directories-i-want-to-work-in`
do
   cd /data/dev/$i 
   for dir in `another-file-with-subdirectories-i-want-to-work-in` 
        do 
        echo $dir
            if [ ! -f $dir/index.html -a ! -f $dir/index.htm -a ! -f $dir/homepage.htm ]; then
                cp /data/dev/template.html $dir/index.html
            fi
        done
done