Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

FreeBSD: Recursively find and replace text

Using a command line in FreeBSD, how can I replace the word "foobar" with the word "zooph" in all files that are found in a directory and all its subdirectories?
Avatar of R_Edwards
R_Edwards
Flag of United States of America image

using the BASH shell

$ shopt -s globstar
$ rename -n 's/foobar/zooph/' **

remove the -n after you test and confirm.

the shopt -s globstar is to enable the bash ** feature, (stands for recursive) however it might be enabled already.

/r
Richard
Avatar of hankknight

ASKER

Would that rename the files?  I want the file names to remain the same.  The text content should be changed.
aah.. I misunderstood, yes the above would rename the files..
grep -rl 'foobar' ./ | xargs sed -i 's/foobar/zooph/g'

This will search for the string 'foobar' in all files relative to the current directory and replace 'foobar' with 'zooph' for each occurrence of the string in each file.

sorry for the earlier confusion
/r
Richard
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial