Sponsored Content
Top Forums Shell Programming and Scripting Find all .htaccess files and make a backup copy in respective directories Post 302525963 by boxx on Monday 30th of May 2011 12:03:52 AM
Old 05-30-2011
I appreciate the responses, but I don't think that's quite what I'm looking for. Adding a date to the file... really doesn't get me anywhere, nor does a manual file by file copy to replace. Let me try to clarify...

I have approx 3,000 .htaccess files I am planning on targeting with a script to update them all at once removing a specified RE.

/dir1/.htaccess
/dir1/sub1/.htaccess
/dir1/sub2/.htaccess
/dir1/sub2/nest1/.htaccess
/dir2/.htaccess
/dir2/sub1/.htaccess
/dir2/sub2/.htaccess
/dir2/sub2/nest1/.htaccess
etc...

I've used:

Code:
find <start_directory> -iname ".htaccess" -exec cp -v {} {}_bak \;

To create a backup of each .htaccess to sit along side the original in every directory, so it now sits like this:

/dir1/.htaccess
/dir1/.htaccess_bak
/dir1/sub1/.htaccess
/dir1/sub1/.htaccess_bak
/dir1/sub2/.htaccess
/dir1/sub2/.htaccess_bak
/dir1/sub2/nest1/.htaccess
/dir1/sub2/nest1/.htaccess_bak
/dir2/.htaccess
/dir2/.htaccess_bak
/dir2/sub1/.htaccess
/dir2/sub1/.htaccess_bak
/dir2/sub2/.htaccess
/dir2/sub2/.htaccess_bak
/dir2/sub2/nest1/.htaccess
/dir2/sub2/nest1/.htaccess_bak
etc...

Now I'm going to run a script through all the original .htaccess files using find and sed -i to remove a selection of unwanted rewrite rules, while leaving the remaining rules in place. I'm pretty sure it is going to go as planned, but I want to be on the safe side and have backups in place, and a method to restore the original .htaccess files with the backups that reside along side them, if something goes wrong.

I need a method to find all .htaccess_bak files and rename them as .htaccess to overwrite the .htaccess files that are sitting along side them.

So in other words:

/dir1/.htaccess_bak is renamed to /dir1/.htaccess ... overwriting and replacing the .htaccess that is in place.
/dir1/sub1/.htaccess_bak is renamed to /dir1/sub1/.htaccess ... overwriting and replacing the .htaccess that is in place.
/dir1/sub2/.htaccess_bak is renamed to /dir1/sub2/.htaccess ... overwriting and replacing the .htaccess that is in place.

leaving a file structure like the original.

/dir1/.htacces
/dir1/sub1/.htaccess
/dir1/sub2/.htaccess
etc

But I need to do this for 3000 files at one time, not a cp or mv or rename... one by one by one manually..

I think this can be accomplished much along the lines of what I'm already using to create the backups. something like...

Code:
find <start_dir> -iname ".htaccess_bak" -exec rename {} {.htaccess} \;

I'm pretty sure the syntax there isn't correct, but I think it gives you an idea of what I'm trying to accomplish.

Any other thoughts or tips?

Thanks much!
BoxX
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

copy multiple files in different directories

I have a report file that is generated every day by a scheduled process. Each day the file is written to a directory named .../blah_blah/Y07/MM-DD-YY/reportmmddyy.tab I want to copy all of this reports to a separate directory without having to do it one by one. However, if I try cp... (3 Replies)
Discussion started by: ken2834
3 Replies

2. Shell Programming and Scripting

Make python script ignore .htaccess

I just wrote a tiny script with the help of ghostdog74 to search all my files for special content phrases. After a few modifications I now made it work, but one problem is left. The files are located in public_html folder, so there might also be .htaccess files. So I ignored scanning of that... (4 Replies)
Discussion started by: medic
4 Replies

3. UNIX for Dummies Questions & Answers

how can i copy those files into other directories have the same name

how can i copy those files into other directories have the same name but different in the end i have files in directory called test: 10_10_asdadfsdfad.txt 10_10_11_asdawqefwkjasd.txt 10_10_11_12_asdafjjhoqwd.txt i want to put them in exist directory thart i have on my system i have... (1 Reply)
Discussion started by: t17
1 Replies

4. Shell Programming and Scripting

Replace several numbers with respective tag and make a single file

Dear All, I have a final output files as 736645|0| 13879|1| 495563|10| 127933|14| 4975|16| 49038|6| 53560|7| 135115|8| 178857|9| Now I want to replace second column with respective tag as per the value (4 Replies)
Discussion started by: jojo123
4 Replies

5. Shell Programming and Scripting

Copy respective path next to last column with awk

Hi to all, I have the short print out sample of the DOS command "dir S/" as showed below. Directory of C:\Program Files\Winamp\Skins\Bento\window 02/12/2010 11:35 p.m. <DIR> . 02/12/2010 11:35 p.m. <DIR> .. 11/12/2009 10:31 a.m. 13,556... (3 Replies)
Discussion started by: cgkmal
3 Replies

6. Shell Programming and Scripting

Copy directories in make file

LD:=C:/WindRiver/diab/5.9.3.0/WIN32/bin/dld.exe CFILES:=$(wildcard *.c) OBJFILES:=$(subst .c,.o, $(CFILES)) OBJ_PATH:=$(PRJ_PATH)/out/ ADDOBJFILES := $(addprefix $(OBJ_PATH),$(OBJFILES)) FILES:=C:/EB/tresos/workspace/Test_Spi/output/src copyfiles: cp ... (3 Replies)
Discussion started by: ushacy
3 Replies

7. AIX

How to backup a directory (sub-directories/files) files from one server on to other ?

Hello, Server A: /directory1/ Server B: /Backups/ i wanted to backup contents of /directory1 from "server A" on to "Server B" every 1 hour. If there is any change in (only new/differences) contents on serverA (directory1/) supposed to be backeup on next run. I did used rsync command to... (5 Replies)
Discussion started by: System Admin 77
5 Replies

8. Shell Programming and Scripting

Copy files in respective directories

Hi Guys, I need to copy the files to respective directories based on name of the file. My script is something like below con=$1 for file in `cat $con` do file_tmp=$(ls -t1 $path| grep -i $file | head -n 1) echo $file_tmp if then cp $path$file_tmp $DIR/ap if then... (16 Replies)
Discussion started by: Master_Mind
16 Replies

9. Shell Programming and Scripting

Find and Copy Directories ONLY

I am trying to copy only the date specific folders/directories using the following command. However, the following copy command is also copying files from the root folder (OriginalFolder). find /OriginalFolder/ -type -d \{ -mtime 1 -o -mtime 2 \ } -exec cp -R {} /CopyTo/'hostname'__CopyTo/ \;... (2 Replies)
Discussion started by: apacheLinux
2 Replies
dircmp(1)							   User Commands							 dircmp(1)

NAME
dircmp - directory comparison SYNOPSIS
dircmp [-ds] [-w n] dir1 dir2 DESCRIPTION
The dircmp command examines dir1 and dir2 and generates various tabulated information about the contents of the directories. Listings of files that are unique to each directory are generated for all the options. If no option is entered, a list is output indicating whether the file names common to both directories have the same contents. OPTIONS
The following options are supported: -d Compares the contents of files with the same name in both directories and output a list telling what must be changed in the two files to bring them into agreement. The list format is described in diff(1). -s Suppresses messages about identical files. -w n Changes the width of the output line to n characters. The default width is 72. OPERANDS
The following operands are supported: dir1 A path name of a directory to be compared. dir2 USAGE
See largefile(5) for the description of the behavior of dircmp when encountering files greater than or equal to 2 Gbyte ( 2^31 bytes). ENVIRONMENT VARIABLES
See environ(5) for descriptions of the following environment variables that affect the execution of dircmp: LC_COLLATE, LC_CTYPE, LC_MES- SAGES, and NLSPATH. EXIT STATUS
The following exit values are returned: 0 Successful completion. >0 An error occurred. (Differences in directory contents are not considered errors.) ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWesu | +-----------------------------+-----------------------------+ SEE ALSO
cmp(1), diff(1), attributes(5), environ(5), largefile(5) SunOS 5.11 1 Feb 1995 dircmp(1)
All times are GMT -4. The time now is 03:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy