Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

FreeBSD: Recursively find and replace text

Posted on 2014-09-15
5
Medium Priority
?
387 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
ID: 40322962
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
ID: 40323288
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
ID: 40323346
aah.. I misunderstood, yes the above would rename the files..
0
 
LVL 8

Expert Comment

by:R_Edwards
ID: 40323356
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 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 40323360
find directory -type f | xargs perl -i -pe 's/foobar/zooph/'
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
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.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses
Course of the Month8 days, 6 hours left to enroll

876 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