Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8605179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:45:44+00:00 2026-06-12T02:45:44+00:00

I want to replace for example all instances of 123 with 321 contained within

  • 0

I want to replace for example all instances of “123” with “321” contained within all .txt files in a folder (recursively).
I thought of doing this

sed -i 's/123/321/g' | find . -name \*.txt

but before possibly screwing all my files I would like to ask if it will work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:45:46+00:00Added an answer on June 12, 2026 at 2:45 am

    You have the sed and the find back to front. With GNU sed and the -i option, you could use:

    find . -name '*.txt' -type f -exec sed -i s/123/321/g {} +
    

    The find finds files with extension .txt and runs the sed -i command on groups of them (that’s the + at the end; it’s standard in POSIX 2008, but not all versions of find necessarily support it). In this example substitution, there’s no danger of misinterpretation of the s/123/321/g command so I’ve not enclosed it in quotes. However, for simplicity and general safety, it is probably better to enclose the sed script in single quotes whenever possible.

    You could also use xargs (and again using GNU extensions -print0 to find and -0 and -r to xargs):

    find . -name '*.txt' -type f -print0 | xargs -0 -r sed -i 's/123/321/g'
    

    The -r means ‘do not run if there are no arguments’ (so the find doesn’t find anything). The -print0 and -0 work in tandem, generating file names ending with the C null byte '\0' instead of a newline, and avoiding misinterpretation of file names containing newlines, blanks and so on.

    Note that before running the script on the real data, you can and should test it. Make a dummy directory (I usually call it junk), copy some sample files into the junk directory, change directory into the junk directory, and test your script on those files. Since they’re copies, there’s no harm done if something goes wrong. And you can simply remove everything in the directory afterwards: rm -fr junk should never cause you anguish.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to read a file, and replace all instances of:
I'm aware that the following command can be used to recursively replace all instances
I want to replace all the instances of ' st ' in my address
Every once in a while I want to replace all instances of values like:
For example, let's say I want to find all instances of using the ==
I want to replace color of an image. For example turn all blue colors
I want to replace a string (in the example the word Linux) with whatever
I want to replace char '&' if it's not followed by '#'. For example
I want to use PHP to replace javascript functions in HTML documents. For example:
I want to take an XML file and replace an element's value. For example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.