[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • 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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

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