LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-18-2021, 09:32 AM   #1
wagscat123
Member
 
Registered: Jan 2009
Location: Maryland-Pennsylvania border, USA
Distribution: openSUSE 15.2/15.3, Tumbleweed, Kubuntu 18.04/21.04, macOS 10.15, antiX 19, and Linux Mint 19.3
Posts: 860
Blog Entries: 45

Rep: Reputation: 120Reputation: 120
Bulk and Recursive Text Replacements


Hi all,


I see some sed and wildcard tricks here, but am looking for a way to search every single non-binary file under a directory tree and replace every occurrence of a particular string of text with another (like changing all occurrences of "yellow_dog" to "black_cat" in every text file in a directory and all its sub directories).


Any ideas?

Last edited by wagscat123; 03-18-2021 at 09:34 AM.
 
Old 03-18-2021, 10:16 AM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,596

Rep: Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545
Quote:
Originally Posted by wagscat123 View Post
...every text file in a directory and all its sub directories
You can use find to recursively find files, and run commands on those files.

It can be a complicated command, so you'll probably want to make sure you're locating the correct files before modifying them and ensure you have appropriate backups beforehand.

Alternatively, most IDEs will have the ability to search and replace across multiple files/dirs recursively - and will probably have an easier to use interface.

 
Old 03-19-2021, 04:40 PM   #3
wagscat123
Member
 
Registered: Jan 2009
Location: Maryland-Pennsylvania border, USA
Distribution: openSUSE 15.2/15.3, Tumbleweed, Kubuntu 18.04/21.04, macOS 10.15, antiX 19, and Linux Mint 19.3
Posts: 860

Original Poster
Blog Entries: 45

Rep: Reputation: 120Reputation: 120
Code:
grep -Iilr "yellow_dog" * | xargs sed -i 's/yellow_dog/black_cat/g'
ended up giving me what I wanted. Thank you!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: How to Bulk Rename Files in Linux with Thunar’s Bulk Rename Tool LXer Syndicated Linux News 0 11-27-2019 07:03 PM
Bulk remove all text after extension Freaksta Linux - Newbie 13 03-12-2018 01:34 PM
non Recursive query and Recursive query prashsharma Linux - Server 1 06-27-2007 09:33 AM
Isolate lines in a text file and perform replacements neville310 Programming 3 06-19-2007 09:20 AM
I need partition magic and ghost replacements - don't need too many features though versaulis Linux - Software 4 09-28-2003 01:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration