The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Copy selected Directories sigurarm UNIX for Dummies Questions & Answers 2 12-21-2007 09:57 AM
Copy single file to multiple directories kthatch UNIX for Dummies Questions & Answers 2 11-11-2007 03:10 PM
copy multiple files in different directories ken2834 UNIX for Dummies Questions & Answers 3 03-25-2007 10:35 AM
How do copy certain files and directories from one hard drive to another? shorty UNIX for Dummies Questions & Answers 4 02-11-2006 05:26 PM
Copy Directories in to UNIX server khan1978 AIX 3 12-09-2005 05:19 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-27-2005
Registered User
 

Join Date: Jun 2004
Location: ~Philadelphia
Posts: 62
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
Reply With Quote
Forum Sponsor
  #2  
Old 06-27-2005
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,262
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
Reply With Quote
  #3  
Old 06-28-2005
Registered User
 

Join Date: Jun 2005
Location: Ireland
Posts: 61
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
Reply With Quote
  #4  
Old 06-28-2005
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,262
@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
Reply With Quote
  #5  
Old 06-28-2005
Registered User
 

Join Date: Jun 2005
Location: Ireland
Posts: 61
true. Should use -f instead.
Reply With Quote
  #6  
Old 06-28-2005
Registered User
 

Join Date: Jun 2004
Location: ~Philadelphia
Posts: 62
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
Reply With Quote
  #7  
Old 06-29-2005
Registered User
 

Join Date: Jun 2004
Location: ~Philadelphia
Posts: 62
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:49 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0