Change Current Directory with Ruby script
Posted by Paul McConnon
I was searching online for a method to change the current working directory of a Bash session using a ruby script.
Essentially I wanted to write a shortcut script that would take me straight to the directory of whatever Rails application I was working on.
I wanted a script called ‘go’ that I could use like this:
paul@ubuntu:~$ go plopcentral paul@ubuntu:~/Development/Sites/PlopCentral$
The script would search a few predefined base paths for the folder and then CD to that folder.
I first tried writing in pure Ruby but unfortunately, executing a ruby script that changes the current working directory within the script, loses these changes when the script exits (this is by design and completely correct, a script should not be able to affect it’s parent process)
I came up with a workaround by making the ruby script return the location of the first folder it found and used a Bash function to change to the returned folder.
Now for the script go.rb, store this in your home folder
#!/usr/bin/ruby
# searches PATHS for a folder then cds to that folder
search = ARGV[0] #get search term
PATHS = ['~/Development','~/Documents']
result = nil
PATHS.each {|path|
result = `ls -R #{path}/ | grep -m1 -i /#{search}`.gsub!(/:$/,'') unless result
}
puts "cd #{result}" if result
This uses the OS’s ls and grep to very quickly find a path with the search term.
This on it’s own is not enough, we need the OS to take that output and use it to change the current directory. To accomplish this we need to define a function in our ~/.bashrc file.
Add the following to the bottom of your ~/.bashrc file.
go()
{
eval $(~/go.rb $1)
}
This function takes it’s first parameter and passes it to the Ruby script defined before, eval’s it and uses the output (if any) to change working directory of the current Bash session.
New you can jump straight to folders with
go path
where path is a partial part the folder you want to cd to.
This is a very simple use of the method but illustrates how to use ruby with Bash and could be extended so that it uses databases etc.
Imagine Activerecord or more complex Ruby libraries for use in Bash.
Note: For those very familiar with shell programming there are bound to be pure Bash ways to accomplish this. I am more familiar with Ruby and wanted to use it.

Photos
This is far easier for a user to simply log onto a webpage without registering than to have to spend time registering and downloading often large programs.
Thanks mate. This is such good info for my research. I will bookmark your post here on Digg.
This is really a very simple use of the method!
Very interesting and informative article I think. I have to admit that I always follow your website because it is full of various information about everything. Reading this post about the Ruby script I have known many new information which I have not known before. Some parts of your article was a little bit difficult for me to understand but when I read everything twice, everything was clear for me. Thanks a lot one more time for the useful information, without you I would never known about such a thing ever.
You're post is well explained and a big help. This is a simple method and will save a person a lot of time. Thanks for this.
You're post is well explained and a big help. This is a simple method and will save a person a lot of time. Thanks for this.
Thanks a lot one more time for the useful information, without you I would never known about such a thing ever. secret life of the american teenager season 2 episode 22
Official balenciaga handbags Online Shop - offering you balenciaga inspired fashion and lifestyle for women balenciaga handbags
Thanks for the script, I wsite! coll
I can you this script on webcam sites, thanks!
Some user left spam, please get rid of it,
Cool post.
I will read this over and over
Its a pleasure reading your post.
This blog is very rich, content, will often come around. correctalittle.com
It's a good post.
I love this post.
I love this post. Expecting more like this.
Awesome.
I read it. Its good.
Good post.
You're post is well explained and a big help. This is a simple method and will save a person a lot of time. Thanks for this.
just click this to paly.....casino en ligne
casino en ligne francais
car hire kerry
host guide
web hosting guide
web hosting news
casino online
sports betting online
car hire france
car hire ireland
car hire italy
car hire portugal
Hire a car Bergamo
New Cars
Hotel Accommodation
Eonon Manufacturer is an online trading firm based in Hong Kong. The objective is to cater for the needs of car audio,car dvd,car video,small & medium enterprises, home users and item collectors.
At acemegastore we specialize in XBox , xbox 360 accessories , xbox 360 controller. We also have a full range of major brand modded consoles. We are you on stop gaming shop
Canvas Prints & Photos: We create stunning canvas prints by printing your photos on canvas. For Canvas Photo, Canvas Pictures, Canvas Print, Canvas Printing visit photobloc.com.