LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 04-28-2006, 11:49 AM   #1
csross
Newbie
 
Registered: Mar 2004
Posts: 25

Rep: Reputation: 15
script to change unix path to windows path in all files


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
 
Old 04-28-2006, 12:28 PM   #2
Vagrant
Member
 
Registered: Nov 2001
Posts: 75

Rep: Reputation: 15
um, why not just `mkdir -p /www/Apache2/htdocs/ && cp -R /var/www/html/* /www/Apache2/htdocs/` ?
 
Old 04-28-2006, 12:31 PM   #3
Vagrant
Member
 
Registered: Nov 2001
Posts: 75

Rep: Reputation: 15
If you're trying to change paths that are written inside of files, you could loop through every file cat and pipe it through sed to do the replace.
 
Old 04-28-2006, 01:08 PM   #4
csross
Newbie
 
Registered: Mar 2004
Posts: 25

Original Poster
Rep: Reputation: 15
yes, I am trying to read each file and change every occurance of /var/www/html to /www/Apache2/htdocs. I do not script so I was hoping someone had such a script.

Thanks very much.
 
Old 04-28-2006, 01:09 PM   #5
csross
Newbie
 
Registered: Mar 2004
Posts: 25

Original Poster
Rep: Reputation: 15
This needs to be a recursive read through all directories and the files within each directory.

Again thanks
 
Old 04-28-2006, 03:54 PM   #6
Vagrant
Member
 
Registered: Nov 2001
Posts: 75

Rep: Reputation: 15
Ok, you can do this then. Run this in the dir where the files are:

Code:
#!/bin/bash

for i in `find .`
do
  if [ -f $i ]; then
        grepResult=`cat $i | grep "/www/Apache2/htdocs/"`
        if [ -z "$grepResult" ]; then
                echo $i
                cat $i | sed 's/\/var\/www\/html\//\/www\/Apache2\/htdocs\//' > $i
        fi
  fi

done
 
Old 04-28-2006, 04:56 PM   #7
burninGpi
Member
 
Registered: Mar 2006
Location: Fort McMurray, Canada
Distribution: Gentoo ~amd64
Posts: 163

Rep: Reputation: 30
The code should be:
Code:
#!/bin/bash

for i in `find . -type f`
do
  if [ -f $i ]; then
        grepResult=`cat $i | grep "/www/Apache2/htdocs/"`
        if [ -z "$grepResult" ]; then
                echo $i
                cat $i | sed 's/\/var\/www\/html\//\/www\/Apache2\/htdocs\//' > $i
        fi
  fi

done
the -type f switch makes find only list files, not dirs.
 
Old 04-28-2006, 05:37 PM   #8
Vagrant
Member
 
Registered: Nov 2001
Posts: 75

Rep: Reputation: 15
burnin, "should" is a bit strong eh? My original code works. The line "if [ -f $i ];." Actually, your way convolutes it, its redudant. If you added "-type f," you could get rid of the "if [ -f $i ]," business.

Its a style choice, I prefer to add a restriction that way.
 
Old 04-29-2006, 02:05 PM   #9
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
How about:

Code:
perl -i -wpe 's,/var/www/html,/www/Apache2/htdocs, && print "$ARGV\n"' $(find -type f /top/dir)
Note that this will do _in_place_ replacement, so you might want to backup the directory tree first.

Last edited by ioerror; 04-29-2006 at 02:11 PM.
 
  


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
Image Path reference in Linux (Absolute path) javabuddy Linux - General 7 06-05-2006 08:45 AM
Why is the value of $PATH in console mode different from the $PATH in xterm emulator? Akhran Debian 9 03-09-2006 07:10 PM
Append the windows path with Samba login script ?? amitsharma_26 Linux - Networking 2 09-29-2005 11:46 AM
How to Chnage Python's module search path (sys.path)? lramos85 Linux - Software 1 05-02-2004 07:10 PM
Change path of log files linuxboy69 Linux - Software 4 12-23-2003 02:17 PM


All times are GMT -5. The time now is 04:55 PM.

Main Menu
Advertisement
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