I’ve created this page in the hope that I’ll be able to find a solution to a problem I’ve had before, without doing the 3 hours of research to solve it in the first place.
Search and replace text in multiple files recursively
I found several answers to this, but the best was Michael Slade’s answer at Stack Overflow – I needed to include path characters (slashes /) so I modified it slightly to use a comma (,) as the delimiter
find . -type f -print0|xargs -0 perl -p -i -e "s,searchstring,replacestring,g;"
Searching for escaped characters
But that was not the whole answer, because my search string also has escaped characters, like `\&` and `\ ` (Spaces). So the command
find . -type f -print0|xargs -0 perl -p -i -e "s,/opt/GNS3/READMEs\ \&\ Scripts/WorkBench\ scripts/startup,~/GNS3/WorkBench/Scripts/startup,g;"
didn’t work – I had to change it to:
find . -type f -print0|xargs -0 perl -p -i -e "s,/opt/GNS3/READMEs\\\ \\\&\\\ Scripts/WorkBench\\\ scripts/startup,~/GNS3/WorkBench/Scripts/startup,g;"
Spawn a shell, run a command and keep the shell open
Thanks to http://stackoverflow.com/questions/3512055/avoid-gnome-terminal-close-after-script-execution
gnome-terminal -e "bash -c \"mycommand; exec bash\""
Recursively rename a set of similar files
Thanks to SvW’s contribution at http://serverfault.com/questions/226627/recursively-rename-files-using-find-and-sed
To rename all files beginning with the word “Instructions” to now begin with “instructions” instead, I used this command:
find . -name "Instructions*" -print0 | xargs -0 rename "s/Instructions/instructions/"