Using find with sed to replace a whole buncha stuff!

So, taking what I’ve learned through open source it’s good to document things you have had difficulty with/learned. Not only can it potentially help others but chances are you probably will forget the exact specifics and you now have an easy to find solution to your problem!

My groups PRJ666 project is winding down to the final few polishing steps. One thing that has annoyed me is that all of our links have been prefaced with /PRJ666-Implementation/pages/ to prevent previous issues we were having before, which are now gone. I wanted to remove all of this nonsense because, in the off chance someone actually uses this project (Hah!) and they decide to change one element of the deployment (a highly likely move)…. KABOOM. Site’s broken.

I know there are a lot of powerful commands available to me using command line unix. I just didn’t really know how to combine them together.

I used a combination of find, sed and a bash script to help solve this problem. Here’s what the code looked like:

for files in `find PRJ666-Implementation/web/pages/ -type f -print 2>/dev/null`
sed 's/\/PRJ666\-Implementation\/pages\//\.\.\//g' $files >$files.tmp
mv $files.tmp $files

This finds all files in a directory, including it’s subdirectories and searches for the string /PRJ666-Implementation/pages/ and removes it by replacing it with ../ .

Hope this helps someone out there like me. These things are definitely one of my many weaknesses and it’s something I’m looking to improve on.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: