![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
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
Hope this helps. bakunin |
|
#3
|
|||
|
|||
|
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
|
|||
|
|||
|
@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
|
|||
|
|||
|
true. Should use -f instead.
|
|
#6
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
| Google The UNIX and Linux Forums |