The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 12:57 PM
Copy single file to multiple directories kthatch UNIX for Dummies Questions & Answers 2 11-11-2007 06:10 PM
copy multiple files in different directories ken2834 UNIX for Dummies Questions & Answers 3 03-25-2007 01:35 PM
How do copy certain files and directories from one hard drive to another? shorty UNIX for Dummies Questions & Answers 4 02-11-2006 08:26 PM
Copy Directories in to UNIX server khan1978 AIX 3 12-09-2005 08:19 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-27-2005
dkaplowitz dkaplowitz is offline
Registered User
  
 

Join Date: Jun 2004
Location: ~Philadelphia
Posts: 63
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 (permalink)  
Old 06-27-2005
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
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 (permalink)  
Old 06-28-2005
pixelbeat pixelbeat is offline
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
  #4 (permalink)  
Old 06-28-2005
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
@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 (permalink)  
Old 06-28-2005
pixelbeat pixelbeat is offline
Registered User
  
 

Join Date: Jun 2005
Location: Ireland
Posts: 61
true. Should use -f instead.
  #6 (permalink)  
Old 06-28-2005
dkaplowitz dkaplowitz is offline
Registered User
  
 

Join Date: Jun 2004
Location: ~Philadelphia
Posts: 63
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 (permalink)  
Old 06-29-2005
dkaplowitz dkaplowitz is offline
Registered User
  
 

Join Date: Jun 2004
Location: ~Philadelphia
Posts: 63
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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:14 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0