I need a one liner to replace <string1> with <string2> recursively. Folder Names, File Names, and File contents.
Thanks in advance.
grep meister
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
Be nice to others! Read the FreeCAD code of conduct!
grep meister
"fight the good fight"
Re: grep meister
You are asking a lot, and it's not clear what you mean recursively: walk a folder tree and process all files or actually recursively substitute strings?
The tool to replace file contents is sed. Something like sed -i -e 's/string1/string2/g' filename1 filename2 ... should work.
The tool to change file names (including folder names) is mv.
The tool to recursively walk a folder tree is find.
So for changeing file contents something like
find targetfolder -exec sed -i -e 's/string1/string2/g' {} \;
should work and for renaming
find targetfolder -name *string1* | while read name; do mv $name $(echo $name | sed -e s/string1/string2/g'); done
or
find targetfolder -name *string1* -exec mv {} $(echo {} | sed -e s/string1/string2/g') \;
Disclaimers:
* I didn't test this
* I probably got some quoting/escaping wrong
* This will break horribly on filenames with whitespace, etc.
I hope this gets you started, but you will need to tweak this to what you acutally need. Solving this kind of problem generally (ie robust against strange filenames, strange patterns, etc.) is hard, so I hope your actual problem is easier then what you stated.
Also if you allowed things like creation of temporary files, there might be much more efficient solutions. Of course this is only a concern if there are really lot's of files affected and you need to run this repeatedly.
The tool to replace file contents is sed. Something like sed -i -e 's/string1/string2/g' filename1 filename2 ... should work.
The tool to change file names (including folder names) is mv.
The tool to recursively walk a folder tree is find.
So for changeing file contents something like
find targetfolder -exec sed -i -e 's/string1/string2/g' {} \;
should work and for renaming
find targetfolder -name *string1* | while read name; do mv $name $(echo $name | sed -e s/string1/string2/g'); done
or
find targetfolder -name *string1* -exec mv {} $(echo {} | sed -e s/string1/string2/g') \;
Disclaimers:
* I didn't test this
* I probably got some quoting/escaping wrong
* This will break horribly on filenames with whitespace, etc.
I hope this gets you started, but you will need to tweak this to what you acutally need. Solving this kind of problem generally (ie robust against strange filenames, strange patterns, etc.) is hard, so I hope your actual problem is easier then what you stated.
Also if you allowed things like creation of temporary files, there might be much more efficient solutions. Of course this is only a concern if there are really lot's of files affected and you need to run this repeatedly.
- wandererfan
- Veteran
- Posts: 6268
- Joined: Tue Nov 06, 2012 5:42 pm
- Contact:
Re: grep meister
I know why sgrogan asks for this. We've discussed of providing a freecad-legacy package for v0.16.6712 when v0.17 is released. Because when it is, existing freecad-stable PPA users will see their 0.16 version be automatically upgraded to 0.17. But considering migration issues with some models that mix Part and PartDesign work flow, it may be best at least at first to edit legacy models in 0.16. So having both versions installed is desirable.
Basically what's needed is to reproduce what we did for the freecad-daily package, which is to replace all occurrences of "freecad" in filenames and inside files with "freecad-legacy" in the debian folder shown in the gitpackaging release branch. Of course a new "legacy" branch needs to be created first. I did all of that manually for freecad-daily, sgrogan is smarter to look for an automated way.
Re: grep meister
Ah, you only expect to run this once on a rather small set of files. In this case my solution is clearly overengineered - probably would take longer to debug the command line then to do things semi-manual:
I propose just renaming the files manually and for the content use this simplified command:
sed -i -e 's/freecad/freecad-legacy/g' * */*
This is still untested, but you have the files under version control, so nothing bad should happen. Right?
HTH,
Harald
I propose just renaming the files manually and for the content use this simplified command:
sed -i -e 's/freecad/freecad-legacy/g' * */*
This is still untested, but you have the files under version control, so nothing bad should happen. Right?
HTH,
Harald
Re: grep meister
Both I think, but the string substitution is the same for folders, files, and content strings.
Thank You!, this gives me a path forward.
As NormandC has explained this happens enough to try and debug the command line. And yes I have version control so that will help.
This looks very good. I just need to be a better python scripter.wandererfan wrote: ↑Sun Feb 11, 2018 1:09 pm This is to go into a script, right? So regexxer and PyRenamer are no help?
Thanks everyone for the help!
"fight the good fight"