# script to change unix path to windows path in all files



## csross (Sep 2, 2004)

I need a shell script to recursively read through a directory and change all instances in a program of /var/www/html/ to /www/Apache2/htdocs/. Additionally I want it to print the full path and name of the file it changed. I can use bash or csh or even perl.

Thank you


----------



## lotuseclat79 (Sep 12, 2003)

Hi csross,

Please confirm the following information:
1) You are writing a script on a unix platform - (Y/N)?
2) The files you are dealing with are text files, and you want to edit a hard-coded path in the files with a text modifcation where the from string is "/var/www/html/" and the to string is "/wwwApache2/htdocs/". (Y/N)?

If the answer to both questions is Y or Yes, then its time you learned the find command in unix. Consult the man page before you code the script in whatever language you choose. Here is an example:

Let's assume we are located at the folder /src for the purpose of illustration which has subfolders with .c source files in them that are the targets of the modifications you want to make. So, for example in a C shell script which you develop:

#/bin/csh
cd /src
find . -name "*.c" -exec sed s/\/var\/www\/html\//\/ww\/Apache2\/htdocs\//g -print
end
exit

Note: the format for the sed command is s/pattern/replacement/flag, and the pattern in this case is \/var\/www\/html\/, where the '/' characters need to be escaped in the shell to get the equivalent: /var/www/html/ to be the pattern; similarly the replacement is: \/ww\/Apache2\/htdocs\/ so that the equivalent is: /www/Apache2/htdocs/

The find command work like this: being position at /src directory, the '.' implies that the find command will search from the current directory and all of its subdirectories - thus meeting your recursive decent requirement. It will the search for all files that are ".c" files that use the .c extension, i.e. C source files in this example. The -exec modifies what the find command is instructed to do when it finds a .c file, i.e. execute the sed command on the .c file replaceing all instances of the pattern with the replacement strings globally on each line - and note: the sed command is by default globally instructed to perform the replacement, so that every instance of the patttern string will be replaced by the replacement string. The print then satisfies your requirement for printing the pathnames for the *.c files found.

This is all from memory, and its been a while since I have coded in C, so play with the sed and find commands on a test experiment directory with .c files before you do it on the real deal files and directories to assure youself that it is working as you want it to work.

-- Tom


----------



## lotuseclat79 (Sep 12, 2003)

The more I thought about it, I think I left out '{}' from the find command in my previous message. I think - without trying it - that it should have been:

find . -name "*.c" -exec sed s/\/var\/www\/html\//\/ww\/Apache2\/htdocs\//g {} \; -print

or something like that.

The {} substitutes the file .c that was found for the sed command, and the \; delimits the sed command.

-- Tom


----------

