Need help with a script

hi,

What I want to do with bash script (or python or whatever else it takes) is that:

1)Will find all subdirectories of a specified directory, for example; public_html :slight_smile:
2)Will find if a spesific file exist in each of them, lets say index.html :smiley:
3)if any of those subdirectories does’t have that spesific file, copy a spesified file, (for example, index.html I created earlier <=D ) to all locations that does not have it.

I know I have to do something with find, ls, foreach loop etc. but I cant figure out the whole algorithm. Can anyone experienced with such scripts help me with this?

Thanks in advance :slight_smile:


find  /fullpath/public_html -type d
find  /fullpath/public_html -type f -name "index.html" -exec dirname "{}" ";"

for d in $(find /fullpath/public_html -type d) ; do
 -e $d/index.html ] || cp /otherfullpath/index.html $i
done

wow, that was quick and easy

thank you :slight_smile:

I just encountered one more problem, how do I check if none of specified files does not exist, for example index.html index.htm index.php ?!?

edit: and also can you explain what does this line do;

-e $d/index.html ] || cp /otherfullpath/index.html $i

my guess is inside …] there is some kind of if statement, -e is an option and || only operates if …] true. Am I correct?

for d in $(find /fullpath/public_html -type d) ; do
 -e $d/index.html -o -e $d/index.php -o -e $/index.htm ] || cp /otherfullpath/index.html $i
done

That would copy the file only if none of index.html, index.php or index.htm exists. They could be symlinks though. If you want only plain files, use -f instead of -e, as in the example below.

for d in $(find /fullpath/public_html -type d) ; do
 -f $d/index.html -o -f $d/index.php -o -f $/index.htm ] || cp /otherfullpath/index.html $i
done

This is the same as :

if  ! -e $index.html ] ; then
     cp /otherfullpath/index.html $i
fi
 -e $d/index.html ] || cp   /otherfullpath/index.html $i
 exist  thatfile ?   OR copy thatOtherFile into    thatDirectory

when you talked about symlinks, that made me think that, instead of copying file, if I symlink the file, would my site still work when I uploaded it to my web host (assuming web host uses linux too)

If you have administrator rights to the Apache server, yes (you’ll have to use the option FollowSymLinks in your DocumentRoot directory), otherwise probably not. I doubt a web hoster will use that option (which is not enabled by default).

Then one last question; I want to find all the index.[htm|html|php] files that contains “Directory access is forbidden.” I tries this;

#!/bin/bash
for d in $(find /home/yasar/public_html -name index) ; do
! grep $d “Directory access is forbidden” ] || echo $d
done

but it didn’t work

find /home/yasar/public_html -type f -name "index.*" -exec grep -q "Directory access is forbidden." "{}" ";" -exec echo "{}" ";"

if I didnt bothered you enough already, can you explain that code? ( It worked well by the way :slight_smile: )

my guess is -exec executes another code, you are giving -g option to grep which means ??? and then something with “{}” and then you execute echo with “{}” this again and “;” this comes up. I am pretty confused :S

  • The grep option is “-q” not “-g” and means “quiet”. Otherwise grep would display the text it finds.
  • -exec is a find option. it means execute the following command if the previous condition is met (or the previous -exec succeeded).
  • “{}” is the wildcard used by find for the search result (usually a file or a directory).
  • “;” terminates an -exec command in find.