[BASH] how to put .htaccess files automatic in directories
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
###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?
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?
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 {}' \;
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 {}' \; = ??????
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.