Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The problem that you'll have using find is that it will try to feed the directories to the programme doing the substitution, as well as the files, causing it to bomb. (I just wrote a little shell script to test this and that's what happened.)
I will consider this further and report back if I find a solution - it's a "real world" problem that must have a simple answer!
* the first s is substitue (someone correct me if I'm wrong!)
* between the first and second slashes is the string you want changed
* between the second and third slashes is what you want the string changed to
* the g means do all occurences, not just the first one (g=global)
Note - if you want to have / or other reserved characters in your string(s), you will need to escape it with a backslash \.
Not sure on your last example - do what I did with mine and try it!
I just set up a temporary directory with a file in it containing the the string I wanted to change and a subdirectory with a further file also containing that string. Then I tweaked and ran the code until it worked, making many references to the find manpage in the process...
Conclusion:
1) experiment - just not on live data ;-)
2) manpages Are Your Friends.
I think that the trucation issue is a case of "your mileage may vary" (it's a buffering thing, I think), but Alex's solution is actually better practice than my original suggestion.
As a rule, it's probably best NOT to take the contents of a file, process it and then put it back in - all in one step. If your process doesn't involve taking a copy, just use a temporary file and delete it afterwards.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.