LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-19-2008, 12:31 PM   #1
sinister1
Member
 
Registered: Jul 2007
Posts: 70

Rep: Reputation: 15
[BASH] how to put .htaccess files automatic in directories


Hai there,

I am trying to build a script which looks if .htaccess exists in different directories. If it exists do nothing else copy .htaccess to that folder.

This is what i have so far:

#!/bin/sh
BASEURL="/export/internet/www-website/html/"


###looks which folder there are in the BASEURL:
LOOK_FOLDER=`find . -type d -maxdepth 1 -name '[!.]*'|awk -F"/" '{print $2}'|while read line;do echo "$BASEURL"$line;done;`

###looks in which folder there is a .htaccess:
LOOK_FILE=`find . -type f -maxdepth 2 -name .htaccess|awk -F"/" '{print $2}'|while read line;do echo "$BASEU
RL"$line;done;`

How do i have to check if file exists copy the file else move on to the next line?

many many many thanks.
 
Old 06-19-2008, 01:26 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
[ -f .htaccess ] && echo Yup

[ ! -f .htaccess ] && echo Nope
or
[ -f .htaccess ] || echo Nope

Last edited by Mr. C.; 06-19-2008 at 01:27 PM.
 
Old 06-19-2008, 01:28 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
In bash, you can use the -a test. I replaced the find options with "..." for brevity:

Code:
find ... |while read d; do
    if [ -a "$d/.htaccess" ]; then
        cp -v "/my/.htaccess" "$d/"  
    fi
done
 
Old 06-19-2008, 01:39 PM   #4
sinister1
Member
 
Registered: Jul 2007
Posts: 70

Original Poster
Rep: Reputation: 15
Thank you

This is now my script:

find . -maxdepth 2 -type f -name .htaccess|awk -F"/" '{print $2}'|while read d;do
if [ -a "$d/.htaccess" ]; then
echo "$d"
#cp -v ".htaccess" "$d/"
fi
done

This shows all the directory where .htaccess found in. Can i see which directory .htaccess isn't found in. so i can cp the .htaccess to the directory that doesn't have an .htaccess yet?

Last edited by sinister1; 06-19-2008 at 01:46 PM.
 
Old 06-19-2008, 01:59 PM   #5
sinister1
Member
 
Registered: Jul 2007
Posts: 70

Original Poster
Rep: Reputation: 15
find . -maxdepth 2 -type f -name .htaccess|awk -F"/" '{print $2}'|while read d;do
if [ -a "$d/.htaccess" ]; then
echo "$d/"
else
cp -v ".htaccess" "$d/"
fi
done

If i run my script i get a:

cp: .htaccess is not a directory error.

After some searching on internet they can tell me that i can't copy more than 1 file at the time. any idea anyone?
 
Old 06-19-2008, 02:14 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
You can copy any number of files into a directory with a single copy command.

Place "echo" in front of your "cp" command, so that you can see what the arguments are.

Code:
find . -maxdepth 2 -type f -name .htaccess|awk -F"/" '{print $2}'|while read d;do
if [ -a "$d/.htaccess" ]; then
   echo "$d/"
else
   echo cp -v .htaccess "$d/"
fi
done
There is no reason to quote ".htaccess" from the shell - there is nothing special about the dot or the name htaccess.
 
Old 06-19-2008, 02:19 PM   #7
sinister1
Member
 
Registered: Jul 2007
Posts: 70

Original Poster
Rep: Reputation: 15
if i place a echo this is the output:

cp -v .htaccess .htaccess/

it seems that $d is changed after the else.

before the else $d is the directoylisting where .htaccess is found.

how can i place the .htaccess in all the directory where it isn't present yet?

thank you for your reply
 
Old 06-19-2008, 03:02 PM   #8
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
First, I'll point out that what you are trying to accomplish may not be the most efficient means to configure apache (since apache needs to open and read each encountered .htaccess file for each and every request). But I'll leave that for another discussion.

There is a more direct approach to what you are trying to accomplish. Consider:

Code:
find . -type d -exec sh -c 'test -e {}/.htaccess || cp -i /path/to/.htaccess {}' \;
 
Old 06-19-2008, 04:00 PM   #9
sinister1
Member
 
Registered: Jul 2007
Posts: 70

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Mr. C. View Post
First, I'll point out that what you are trying to accomplish may not be the most efficient means to configure apache (since apache needs to open and read each encountered .htaccess file for each and every request). But I'll leave that for another discussion.

There is a more direct approach to what you are trying to accomplish. Consider:

Code:
find . -type d -exec sh -c 'test -e {}/.htaccess || cp -i /path/to/.htaccess {}' \;
it seems to be working.

Can you explain the line:

find . -type d -exec sh -c 'test -e {}/.htaccess || cp -i /path/to/.htaccess {}' \;


find . -type d = find all directories in the current directory
-exec sh = execute ??
test -e = true if exist regardless of type
{}/.htaccess || cp -i /path/to/.htaccess {}' \; = ??????

Thank you!!
 
Old 06-19-2008, 04:22 PM   #10
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Find (find) in the current directory (.) directories only (-type d) and execute (-exec) the command (sh -c 'test -e {}/.htaccess || cp -i /path/to/.htaccess {}').

First, find replaces the characters {} with the current pathname it is working on at the moment, and ; ends the -exec argument. Both {} and ; need to be escaped from the shell, as they are shell meta-characters, hence the backslashes.

The comamnd that is executed is a shell command (sh), and we've passed the run command option (-c), and a long argument:

Code:
test -e {}/.htaccess || cp -i /path/to/.htaccess {}
but of course find replaced the {} with a pathname. The command above is essentially:

Code:
cmd1 || cmd2
If cmd1 succeeds, there's no reason to run cmd2 (because the statement is true if either are true: we are taking advantage of what is called a logic short circuit)
If cmd1 fails, then cmd2 must be run to test its result for success, hence the cp occurs.
 
Old 06-19-2008, 04:39 PM   #11
sinister1
Member
 
Registered: Jul 2007
Posts: 70

Original Poster
Rep: Reputation: 15
Mr C.

You are the greatest!!

Thank you very much!!!
 
Old 06-24-2008, 11:58 AM   #12
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Quote:
Originally Posted by sinister1
Best Mr. C.

Can you please help me with a problem in my .htaccess script?

I want to put a .htpasswd at the directories where there is installed a new .htaccess.

I am very thankfull if you want to help me with this.

The script to put the .htaccess in the dirs:

test -e {}/.htaccess || cp -i /path/to/.htaccess {}

I cant use this one the same for the htpasswd file because the htpasswd files are called different every way.

thank thank thank thank you
Please keep requests online so other's may gain knowledge and help as well.

If your .htpassed files have various names, then the names of the htpasswd files must be inside the htaccess files, correct ? Please clarify how you determine the name of the htpasswd file.

Just to bring up something I briefly mentioned earlier, are you aware that an htaccess file controls the directory in which it resides *and* its subdirectories? Access protection works in a tree-like fashion, and apache reads first the top-most htaccess file, and then those in each subdirectory as it handles requests within a tree.
 
Old 06-25-2008, 01:26 AM   #13
sinister1
Member
 
Registered: Jul 2007
Posts: 70

Original Poster
Rep: Reputation: 15
Yes i am aware of that.

If i want to locate the pass file of htaccess i am doing this:

****FIND HTACCESS AND WRITE IT TO A FILE:

find . -name .htaccess > /htaccessesdir

****SELECT THE AuthUserFile FROM THE HTACCESSES:

cat /htaccessesdir|while read line; do grep AuthUserFile "$line" >> /htaccesses.info; done

But how can i implement the copy htaccess file in this script?
 
Old 06-25-2008, 03:12 AM   #14
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Let me make sure I understand you.
  • You have .htaccess files in various directories.
  • From each of those .htaccess files, you want to get the AuthUserFile entry; this specifies the path/name of the htpasswd file.
  • You want to move (or copy) the named htpasswd file into the same directory as the .htaccess file.

Here is a shell/perl script that will do what I think you are asking. If this is incorrect, please be a little more clear in your description.

Code:
find . -name .htaccess -exec grep -H AuthUserFile '{}' \;  | \
    perl -n -e '
        chomp;
        unless (/^([^:]+):AuthUserFile\s+(.+)$/) {
            print "Skipping: $_\n"; next;
        }
        ($htaccess,$htpasswd) = ($1,$2);
        $htpasswd =~ s/\r//g;
        ($htaccessdir) = ($htaccess =~ m{^(.*?/)[^/]+$});
        print "#FOUND:       $_\n";
        print "#PASSWD:      $htpasswd\n";
        print "#ACCESS:      $htaccess\n";
        print "#ACCESSDIR:   $htaccessdir\n";
        print "echo cp $htpasswd $htaccessdir\n";
    '
The code will print out shell commands.

The print lines are for you to see what is happening. You can leave them, or delete them. The are protected by shell comments, so they won't harm the output.

Once you have confirmed this is what you want, you can remove the word "echo" from the last print line to create the actual commands you want. Redirect the output of this program into file, or run it in the terminal and copy/paste the commands to run.
 
Old 06-25-2008, 05:25 AM   #15
sinister1
Member
 
Registered: Jul 2007
Posts: 70

Original Poster
Rep: Reputation: 15
I will explain it al little bit better:

I have several directories:

test1
test2 (contains a htaccess + htpasswd named .pass)
test3

I am going to look which dir already contains a htaccess. If exists then do nothing else cp htaccess to that dir.

find . -type d -exec sh -c 'test -e {}/.htaccess || cp -i /path/to/.htaccess {}' \;

What i extra want is if there is a dir where htaccess is NOT found the htaccess is copied in this dir and a htpasswd

I want to place a htaccess + htpasswd in the dirs: test1 and test3 in this example.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to put log files into single file and email DragonM15 Programming 13 11-08-2007 03:27 AM
bash ls not coloring files, directories etc mr_smith Red Hat 2 08-30-2006 07:05 AM
.htaccess files doesn't work in home directories rehtorisi Linux - Software 3 08-08-2005 08:02 AM
.htaccess code for blocking CVS directories mrtwice Linux - Software 1 10-12-2004 01:38 PM
Copying linked files, replacing directories in bash scripts? ta0kira Programming 2 10-10-2004 04:46 AM


All times are GMT -5. The time now is 06:29 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration