Computing.Net > Forums > Unix > Find/Replace in subdirectories

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Find/Replace in subdirectories

Reply to Message Icon

Name: vasur
Date: June 20, 2003 at 07:20:44 Pacific
OS: Solaris
CPU/Ram: 1GB
Comment:

Hi, I have the following script that finds and replaces a given string pattern in all files within the directory it is executed. What change does it require so it can do the same in all the subdirectories. Thanks, Vasu.

#!/bin/ksh
#Loop through every file like this
for file in *.htm*
do
cat $file | sed "s/johhn/John/g" > $file.new
#do a global search and replace and put the results in $file.new
mv $file.new $file
#rename $file.new to original name of file
done



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: June 20, 2003 at 08:21:35 Pacific
Reply:

Hi:

Use the find command. This particular command greps only text files

#untested
find . -type f -print | xargs file | grep -i text | cut -f1 -d: | xargs egrep "*.htm*"

Regards,

Nails


0

Response Number 2
Name: Dlonra
Date: June 23, 2003 at 08:35:53 Pacific
Reply:

replace your "for file in *.htm"
with
for file in $(find . -type f -name '*\.htm')


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Find/Replace in subdirectories

Binary find & replace www.computing.net/answers/unix/binary-find-amp-replace/6021.html

find files in specific date www.computing.net/answers/unix/find-files-in-specific-date/4708.html

find files in current dir ONLY www.computing.net/answers/unix/find-files-in-current-dir-only/4800.html