Operating System - HP-UX
1829454 Members
1611 Online
109992 Solutions
New Discussion

find/replace in any text file in a whole directory structure

 
Jayson Hurd_2
Advisor

find/replace in any text file in a whole directory structure

I was wondering if anyone had a nifty command script to find any text file, starting from a single point, all the way downwards in that directory structure, and find and replace text in all of them...
Most things worth having don't come easily.
12 REPLIES 12
Elmar P. Kolkman
Honored Contributor

Re: find/replace in any text file in a whole directory structure

Not really hard...
find . -type f -exec <script replacing texts>

If you want to make sure it is an ASCII file, the script needs to check the filetype with the file command.
Every problem has at least one solution. Only some solutions are harder to find.
Helen French
Honored Contributor

Re: find/replace in any text file in a whole directory structure

This is a script that will check all files (and subdirectories) under /test and will replace every occurence of a text 'txt' with 'text':

find /test -type f -depth | while read FILE
do
sed "s/txt/text/g" < ${FILE} > ${FILE}.shiju
mv ${FILE}.shiju $FILE
done

Replace your directory name and text appropriately.
Life is a promise, fulfill it!
Jayson Hurd_2
Advisor

Re: find/replace in any text file in a whole directory structure

I need to make sure it's an ascii text file. And I need it to vi each file and do a find/replace.
Most things worth having don't come easily.
Helen French
Honored Contributor

Re: find/replace in any text file in a whole directory structure

Again, that's exactly the above script will do. '-type f' will find only regular files and 'sed' command will edit it like 'vi' and replace the text.
Life is a promise, fulfill it!
curt larson_1
Honored Contributor

Re: find/replace in any text file in a whole directory structure

how about something like this

old="text to replace"
new="text to substitute"

find /test -type f -depth |
while read File
do
if [[ ! -r $File ]] ;then
print "$File isn't readable"
continue
fi
file $File | grep 'ascii text' >/dev/null 2>/dev/null
if [[ $? != 0 ]] ;then
print "$File isn't ascii text"
continue
fi
if [[ ! -r $File ]] ;then
print "$File isn't writable"
continue
fi
ex -s +"%s/$old/$new/g | wq" $File
done
curt larson_1
Honored Contributor

Re: find/replace in any text file in a whole directory structure

oops

that last

if [[ ! -r $File ]] ;then
print "$File isn't writable"
continue
fi

should have been
#use -w to test for writable
#
if [[ ! -w $File ]] ;then
print "$File isn't writable"
continue
fi
curt larson_1
Honored Contributor

Re: find/replace in any text file in a whole directory structure

another test to do after if writable

grep "$old" $File >/dev/null 2>/dev/null
if [[ $? = 0 ]] ;then
print "modifing $File"
ex -s +"%s/$old/$new/g | wq" $File
else
print "no changes to $File"
fi
curt larson_1
Honored Contributor

Re: find/replace in any text file in a whole directory structure

you'll need to work with the file command to get the correct files that you're wanting to find. you asked for ascii text files. the file command will say that a file such as /etc/passwd is ascii text, but will say that a shell script is commands text (not ascii text). Therefore, the script that i wrote, will tell you that a shell script isn't ascii text and not make any modifications.
Elmar P. Kolkman
Honored Contributor

Re: find/replace in any text file in a whole directory structure

As for my own solution: I missed some parts of the find statement. It should be:
find . -type f -exec <script> {} ';'
So that the files are presented one-by-one to the script.

As for finding ascii files, that completely depends on what you think ascii files are. The file command will report them, but sometimes as 'commands text', sometimes as 'ascii file'. It depends on a lot of things what it will report. For instance a shell script is reported as commands text while the .profile is reported as ascii file, even though it is in fact a shell script too... It also depends on your magic file...

So what you need to do is come up with criteria for the names/contents of the files you want to alter, write those criteria in your script, have the script make backups so files wrongly touched can be put back, and then test it...
Every problem has at least one solution. Only some solutions are harder to find.
hein coulier
Frequent Advisor

Re: find/replace in any text file in a whole directory structure

DIR -> starting point
FROM -> text to replace
TO -> replace text to

find ${DIR} -type f -exec file {} \;|
grep text|cut -f1 -d':'|
while read fname
do
ed -s ${fname}<
Peter Nikitka
Honored Contributor

Re: find/replace in any text file in a whole directory structure

Hi,

similar to heins solutions, I prefer to create an "ed"-inputfile to have the chance to review and test the editor-commands:

find DIR -type f .... | while read fname
do
echo e $fname
echo s/FROM/TO/g
echo w
done >/tmp/cmd.ed

After review:
ed -s
If you really want to do it immediately,
change the last line to
...
done | ed -s

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Jayson Hurd_2
Advisor

Re: find/replace in any text file in a whole directory structure

Hmm... Some regular files are actually binaries and others besides text files. This script edited some of the wrong files. But.. It did get me going in the right direction. Thanks.
Most things worth having don't come easily.