Editing multiple .htaccess files - apache

We got around 500 websites on our server and all of them has their own user directory.
/home/siteA/public_html/
/home/siteB/public_html/
/home/siteC/public_html/
All of them has its own .htaccess file and its duplicated.
What i wanna do is comment out last row of each file. is it possible to do it without actually editing all files by hand.

Yes. Assuming you have a list of which directories are sites in say sites.txt, just loop over them like this:
while read sitename; do
sed -ri "$ s/(.+)/#\1/" /home/$sitename/.htaccess
done < sites.txt
Just make sure that you don't have a bunch of empty lines at the end of your files; this will comment out the last line, not the last line with content. If you need to do that, it will become much more difficult.

Related

delete all lines starting by: INSERT INTO `mdl_logstore_standard_log` VALUES

I have a SQL text file on my Linux server, and I need to remove all lines starting by
INSERT INTO `mdl_logstore_standard_log`
Looks like a duplicate of this.
Following the "Better Solution" in the chosen answer there, you would likely want to consider using sed. This will delete the lines without needing to open the file.
For your specific case, you can run
sed '/^INSERT INTO `mdl_logstore_standard_log`/d' text_file.sql > new_text_file.sql
where you'd replace text_file.sql and new_text_file.sql with your current file and the file with lines deleted. You may also want to consider looking at the -i (or equivalent --in-place) option if you'd prefer to overwrite your previous file with the new one.
You can achieve it with vim. Open the file, type :g and then make sure you run the following:
:g/^INSERT INTO `mdl_logstore_standard_log`/d
Explanation:
- ^ is the start of the string, here means starts with
- d at the end means delete
- the pattern between the slashes is the pattern to find
If it looks good, hit :w to save your file.

CLOC- count lines of code, exclude everything in a dir except for certain files

I am using cloc to count our code. We need to exclude everything in a directory, except for files with a specific form like *instru.c. Is it possible to use the exclude and include files to do this?

correct way to write to the same file from multiple processes awk

The title says it all.
I have 4 awk processes logging to the same file, and output seems fine, not mangled, but I'm not sure that just redirecting print output like this: print "xxx" >> file in every process is the right way to do it.
There are many similar questions around the site, but this one is particularly about awk and a pragmatic, code-correct way to approach the problem.
EDIT
Sorry folks, of course I wasn't "just redirecting" like I wrote, I was appending.
No it is not safe.
the awk print "foo" > "file" will open the file and overwrite the file content, till the end of script.
That is, if your 4 awk processes started writing to the same file on different time, they overwrite the result of each other.
To reproduce it, you could start two (or more) awk like this:
awk '{while(++i<9){system("sleep 2");print "p1">"file"}}' <<<"" &
awk '{while(++i<9){system("sleep 2");print "p2">"file"}}' <<<"" &
and same time you monitoring the content of file, you will see finally there are not exactly 8 "p1" and 8 "p2".
using >> could avoid the losing of entries. but the entry sequence from 4 processes could be messed up.
EDIT
Ok, the > was a typo.
I don't know why you really need 4 processes to write into same file. as I said, with >>, the entries won't get lost (if you awk scripts works correctly). however personally I won't do in this way. If I have to have 4 processes, i would write to different files. well I don't know your requirement, just speaking in general.
outputting to different files make the testing, debugging easier.. imagine when one of your processes had problem, you want to solve it. etc...
I think using the operating system print command is save. As in fact this will append the file write buffer with the string you provide as log. So the system will menage the actual writing process of the data to disc, also if another process will want to use the same file the system will see that the resource is already claimed and will wait for 1st thread to finish its processing, than will allow the 2nd process to write to the buffer.

Mod-Rewrite to variable ending file

I'm trying to get apache to serve any request for /uploaded/2 with the first file that starts with 2 in a certain directory (say /foo/bar/).
Basically, If I have directory /foo/bar with contents:
1-filenameclutter.wav
2-clutterinthefilename.mp3
3-someweirdtext.jpg
And a web browser makes a request for /uploaded/1, apache would return 1-filenameclutter.wav; a request for /uploaded/2 would return 2-clutterinthefilename.mp3; etc. (all files with the right mime-type).
As far as I can see, ModRewrite can only go from a source with extraneous data to a simplified file on the file system, not the other way around.
Do you guys know any way to do this, with ModRewrite or using apache in another way (no PHP)?
EDIT:
Two things to point out, 1) I'm not concerned with duplicate files starting with the same id. These files correspond to an object in a database, which has a primarykey, id. 2) The reason I'm doing this is because I won't know exactly what the extension of the file is, but I do know the id, so when I form these I just prepend the orignal filename to the end of {{id}}- (Don't worry, I replace all ".."'s with "~").
Mod-rewrite has the ability to check if a single specific file exists, but you can't search a directory for a file pattern. Note that what you are suggesting would have horrible scaling attributes because the system would have to search all the files to find the file you are looking for. Since you already have the file in the database, why don't you just name the file with the id and keep the real filename in the database? In that case, /uploaded/2 would return the file at that location. You don't even need mod_rewrite.

Inconsistent Behavior In A Batch File's For Statement

I've done very little with batch files but I'm trying to track down a strange bug I've been encountering on a legacy system.
I have a number of .exe files in particular folder. This script is supposed to duplicate them with a different file name.
Code From Batch File
for %%i in (*.exe) do copy \\networkpath\folder\%%i \\networkpath\folder\%%i.backup.exe
(Note: The source and destination folders are THE SAME)
Example Of Desired Behavior:
File1.exe --> Becomes --> File1.exe.backup.exe
File2.exe --> Becomes --> File2.exe.backup.exe
Now first, let me say that this is not the approach I would take. I know there are other (potentially more straight forward) ways to do this. I also know that you might wonder WHY on earth we care about creating a FileX.exe.backup.exe. But this script has been running for years and I'm told the problem only started recently. I'm trying to pinpoint the problem, not rewrite the code (even if it would be trivial).
Example Buggy Output:
File1.exe.backup.exe
File1.exe.backup.exe.backup.exe
File1.exe.backup.exe.backup.exe.backup.exe
File1.exe.backup.exe.backup.exe.backup.exe.backup.exe
File1.exe.backup.exe.backup.exe.backup.exe.backup.exe.backup.exe
File1.exe.backup.exe.backup.exe.backup.exe.backup.exe.backup.exe.backup.exe
etc...
File2.exe.backup.exe
File2.exe.backup.exe.backup.exe
File2.exe.backup.exe.backup.exe.backup.exe
File2.exe.backup.exe.backup.exe.backup.exe.backup.exe
File2.exe.backup.exe.backup.exe.backup.exe.backup.exe.backup.exe
File2.exe.backup.exe.backup.exe.backup.exe.backup.exe.backup.exe.backup.exe
Not knowing anything about batch files, I looked at this and figured that the condition of the for statement was being re-evaluated after each iteration - creating a (near) infinite loop of copying (I can see that, eventually, the copy will fail when the names get too long).
This would explain the behaviour I'm seeing. And when cleaned the directory in question so that it had only the original File1.exe file and ran the script it produced the bug code. The problem is that I CANNOT replicate the behaviour anywhere else!?!
When I create a folder locally with a few .exe files and run the script - I get the expected output. And yes, if I run it again, I get one instance of 'File1.exe.backup.exe.backup.exe' (and each time I run it again, it increases in length by one). But I cannot get it to enter the near-infinite loop case.
It's been driving me crazy.
The bug is occurring on a networked location - so I've tried to recreate it on one - but again, no success. Because it's a shared network location, I wondered if it could have something to do with other people accessing or modifying files in the folder and even introduced delays and wrote a tiny program to perform actions in the same folder - but without any success.
The documentation I can find on the 'for' statement doesn't really help, but all of the tests I've run seem to suggest that the in (*.exe) section is only evaluated once at the beginning of execution.
Does anyone have any suggestions for what might be going on here?
I agree with Andriy M's comment - it looks to be related to Windows 7 Batch Script 'For' Command Error/Bug
The following change should fix the problem:
for /f "eol=: delims=" %%i in ('dir /b *.exe') do copy \\networkpath\folder\%%i \\networkpath\folder\%%i.backup.exe
Any file that starts with a semicolon (highly unlikely, but it can happen) would be skipped with the default EOL of semicolon. To be safe you should set EOL to some character that could never start a file name (or any path). That is why I chose the colon - it cannot appear in a folder or file name, and can only appear after a drive letter. So it should always be safe.
Copy supports wildcard characters also in target path. You can use
copy \\networkpath\folder\*.exe \\networkpath\folder\*.backup.exe