Find directories not containing foo, and copy foo to them


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find directories not containing foo, and copy foo to them
# 1  
Old 06-27-2005
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
# 2  
Old 06-27-2005
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:

Code:
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?):

Code:
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
# 3  
Old 06-28-2005
Code:
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

# 4  
Old 06-28-2005
@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
# 5  
Old 06-28-2005
true. Should use -f instead.
# 6  
Old 06-28-2005
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
# 7  
Old 06-29-2005
Thanks for the help. I got it sorted. Here's what I used:

Code:
#!/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

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ssh foo.com sudo command - Prompts for sudo password as visible text. Help?

I am writing a BASH script to update a webserver and then restart Apache. It looks basically like this: #!/bin/bash rsync /path/on/local/machine/ foo.com:path/on/remote/machine/ ssh foo.com sudo /etc/init.d/apache2 reloadrsync and ssh don't prompt for a password, because I have DSA encryption... (9 Replies)
Discussion started by: fluoborate
9 Replies

2. UNIX for Dummies Questions & Answers

Having trouble understanding this command: >foo<bar bc

Sometimes it works for me and sometimes I get this error: syntax error on line 1, teletype Basically I've got no idea whats going on, especially at the end of the command: bc Any help is appreciated (1 Reply)
Discussion started by: phunkypants
1 Replies

3. UNIX for Dummies Questions & Answers

Odd result from cp -R foo/.* bar

I'm not all that much of a newbie but I've not encountered this before. Happens both in Cygwin and in Mac OS X (Darwin): cp -R /path/to/foo/.* /path/to/bar (where directory 'bar' exists) ... seems to copy not only the contents of directory 'foo', but also other directories that are... (3 Replies)
Discussion started by: ChapHarrison
3 Replies

4. UNIX for Dummies Questions & Answers

the meaning of "!:*" in "alias foo 'command\!:*' filename"

Hi: How can I remove my own post? Thanks. (2 Replies)
Discussion started by: phil518
2 Replies

5. Shell Programming and Scripting

SED: Extracting text between first occurance of foo in front of bar

Suppose I have a text file that contains the tags <foo> and <bar>. The text file can have unlimted occurances of <foo> and <bar> and looks somthing like this: <foo> Some Text <foo> Some Text <bar> Some Text <foo> Some (1 Reply)
Discussion started by: ArterialTool
1 Replies

6. Shell Programming and Scripting

using /etc/foo.config in shell script

I'm very very new to shell scripting (about 4 hours) i've google'd till i can't google no more is it possible to have store values in a config file .e.g /etc/foo.conf data=/home/ mount=/dev/sda1 size=1GB and access these values from a shell script but also be able to use... (3 Replies)
Discussion started by: xpd259
3 Replies

7. Shell Programming and Scripting

if [ -z echo foo | egrep -e 'regexp' != '' ] -> dont work

Hallo, I need to test a String (a special ip number-string). So I want to run that: ipadress=172.0.0.0 # for debugging: echo $ipadress | egrep -e '172\.?\.??\.??$' # the test that doesnt work if test -z `echo $ipadress | egrep -e '172\.?\.??\.??$'` != "" then echo "match" else... (1 Reply)
Discussion started by: wiseguy
1 Replies

8. Shell Programming and Scripting

Regex & grep-foo

I need a way to grep -v a list of times/date from the output of postqueue -p that are a few hours old, in order to remove them with postsuper -d. Right now I have a script that is deleting the previous day of messages left in the queue, which runs once each day. I want to clean up the job and... (1 Reply)
Discussion started by: DoneWithM$
1 Replies

9. UNIX for Dummies Questions & Answers

foo

if ; then echo Its Cold for $sid at $c_dst $jobroot/scripts/abc.sh $sid COLD $c_dst elif ; then echo Its Hot for $sid at $h_dst $jobroot/scripts/abc.sh $sid HOT $h_dst else echo No backup for $sid today wat does foo do ?? (2 Replies)
Discussion started by: bn_unx
2 Replies
Login or Register to Ask a Question