Solved

FreeBSD: Recursively find and replace text

Posted on 2014-09-15
5
301 Views
Last Modified: 2016-02-11
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
Comment
Question by:hankknight
  • 3
5 Comments
 
LVL 8

Expert Comment

by:R_Edwards
Comment Utility
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
 
LVL 16

Author Comment

by:hankknight
Comment Utility
Would that rename the files?  I want the file names to remain the same.  The text content should be changed.
0
 
LVL 8

Expert Comment

by:R_Edwards
Comment Utility
aah.. I misunderstood, yes the above would rename the files..
0
 
LVL 8

Expert Comment

by:R_Edwards
Comment Utility
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
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
Comment Utility
find directory -type f | xargs perl -i -pe 's/foobar/zooph/'
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now