replace string in multiple files, dir and subdir


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace string in multiple files, dir and subdir
# 1  
Old 11-22-2009
Lightbulb replace string in multiple files, dir and subdir

Hello, I have a directory www with multiple directories. Every directory has site name with .htm, .html, .php files or sub directories with .htm, .php, .html file as example

- www [dir]
- sitename 1 [dir]
- [subdirectories] site 1
- sitename 2 [dir]
- sitename 3 [dir]

What I'm looking for is a script that reads ALL the folders and subfolders in
WWW and looks for htm, html and php and changes a content string inside the files from COMPANYNAME1 to COMPANYNAME2 for all .htm, .html and .php files.

I found this but how do I lock the changes ONLY in WWW directory/subdirectories without messing up the other server files, and how do I select multiple file extension.


Code:
#!/bin/bash
OLD="xyz"
NEW="abc"
DPATH="/home/you/foo/*.txt"
BPATH="/home/you/bakup/foo"
TFILE="/tmp/out.tmp.$$"
[ ! -d $BPATH ] && mkdir -p $BPATH || :
for f in $DPATH
do
  if [ -f $f -a -r $f ]; then
    /bin/cp -f $f $BPATH
   sed "s/$OLD/$NEW/g" "$f" > $TFILE && mv $TFILE "$f"
  else
   echo "Error: Cannot read $f"
  fi
done
/bin/rm $TFILE

Thanks for help
# 2  
Old 11-22-2009
You can read files with multiple extension as below.
Could you clarify more on your first question ?

Code:
DPATH="/home/you/*.txt /home/you/*.pl /home/you/*.sh"

# 3  
Old 11-22-2009
Hi, thanks for the answer, basically I want the script to run ONLY in WWW and WWW sub directories

Thanks
# 4  
Old 11-22-2009
Hi , Try this

Code:
find directory_www \( -name "*.php" -o -name "*.html" -o -name "*.htm" \)

# 5  
Old 11-22-2009
try this:-


Code:
for x in `find /path/to/WWW_dir  \( -name "*.html" -o -name "*.htm"  -o -name *.php \)`
do
sed 's/\<Company2\>/Company11/g' < $x > $x.mod
mv $x.mod $x
done

SmilieSmilieSmilie
# 6  
Old 11-22-2009
Quote:
Originally Posted by ahmad.diab
try this:-


Code:
for x in `find /path/to/WWW_dir  \( -name "*.html" -o -name "*.htm"  -o -name *.php \)`
do
sed 's/\<Company2\>/Company11/g' < $x > $x.mod
mv $x.mod $x
done

SmilieSmilieSmilie
use a while loop
Code:
find ....... | while read file
do
 sed .....
done

# 7  
Old 11-22-2009
hello, something is not working ....some suggestions

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a string in files in all dir and sub dirs

Hello, I need to replace xml version='1.1' with xml version='1.0' in all xml files under /app/jenkins/ in all dir and sub dirs in my CentOS VM, I tried below command but it didn't help, looks like I'm missing a character somewhere. grep -rl "xml version='1.1'" . | xargs sed -i 's/"xml... (2 Replies)
Discussion started by: mahesh Madpathi
2 Replies

2. UNIX for Dummies Questions & Answers

copying the dir/subdir structure from one server to another?

Hi All, I want to copy the dir/subdir structure from SERVER-A to SERVER-B without copying all the files in each dir. Is it possible using SCP / SFTP command? For example, SERVER-A has following two dir/subdirectories and files under each subdir. ... (1 Reply)
Discussion started by: Hangman2
1 Replies

3. Shell Programming and Scripting

Move all files from dir and subdir to Archive with File name as complete path_filename

HI, I need to move all files from a dir & its all subdir to Archive folder which is indise dir only. and moved filename should changed to complete path ( Like Dir_subdir_subdir2_.._filename ). also all files names shoud capture in a file in order to mail I written below code ... (11 Replies)
Discussion started by: minijain7
11 Replies

4. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

5. UNIX for Dummies Questions & Answers

replace string in multiple files

Hi, I'm new to Unix. My understanding of Unix and its command is very limited. I have about 1000 text files that have a word in it that I need to replace with a different word. e.g. a.txt has 1 line of txt: monday, tuesday, wednesday b.txt has 1 line of txt: monday, tuesday,... (5 Replies)
Discussion started by: millsy5
5 Replies

6. Shell Programming and Scripting

KSH - Find paths of multiple files in CC (dir and sub-dir))

Dear Members, I have a list of xml files like abc.xml.table prq.xml.table ... .. . in a txt file. Now I have to search the file(s) in all directories and sub-directories and print the full path of file in a output txt file. Please help me with the script or command to do so. ... (11 Replies)
Discussion started by: Yoodit
11 Replies

7. Shell Programming and Scripting

Perform action in dir if dir has .git subdir

Hi, I want to run git status for the dir which has subdir ".git" in it with dir path mentioned in output log? If a dir does not have .git subdir then skip that dir. Dir will have 100 main dirs & 500 + subdirs and so on. I appreciate all your help :b: (4 Replies)
Discussion started by: dragon.1431
4 Replies

8. Shell Programming and Scripting

find string from multiple dir and redirect to new files

Hi, I am new to script and I want find one string from multiple files in diff directories and put that out put to new file. Like I have A,B & C directories and each has multiple files but one file is unic in all the directories like COMM.txt Now I want write script to find the string... (8 Replies)
Discussion started by: Mahessh123
8 Replies

9. Shell Programming and Scripting

moving dir subdir and files

I have created a directory structure and under the directory subdirectories and files are there.I need to move the entire thing to another path.How can i write a script to do that. currently the path of files is as below : /data1/serial/mcycle/archive : under this path differnt sub dir exist ... (6 Replies)
Discussion started by: dr46014
6 Replies

10. Shell Programming and Scripting

Grep in subdir, multiple files

Hi, I m trying to have script to grep a pattern in all files under a directory. I use Sun Solaris, the below want doesnt do exactly what I want. find /home/xxx/ tagHeu | while read FILE; do grep text $FILE && echo 1 grep text $FILE || echo 0 done I also tried running: find... (2 Replies)
Discussion started by: s3rro
2 Replies
Login or Register to Ask a Question