• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 484
  • Last Modified:

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?
0
hankknight
Asked:
hankknight
  • 3
1 Solution
 
R_EdwardsCommented:
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
0
 
hankknightAuthor Commented:
Would that rename the files?  I want the file names to remain the same.  The text content should be changed.
0
 
R_EdwardsCommented:
aah.. I misunderstood, yes the above would rename the files..
0
 
R_EdwardsCommented:
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
0
 
ozoCommented:
find directory -type f | xargs perl -i -pe 's/foobar/zooph/'
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now