Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have some directories that I have to go to a lot more often than others, like for music, work, programming, etc. So I want to have exclusive commands, say "gotomusic" or "gotodropbox" to go to these folders because otherwise I have to type something like cd /media/sda9/Work/Dropbox and so on.

Is it a good idea? Is there a better way of getting the same result?

I tried writing the following script "gotomusic"

#!/bin/bash

cd /media/sda8/Music

But it does not work. What is wrong in doing so?

Thanks!

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

I think that you can find the right solution on stackoverflow:

change-current-directory-from-a-script

share|improve this answer
    
That's it. Thanks! –  user1953384 Aug 2 '13 at 11:12
add comment

You may do this by using aliases, just add in your ~/.bashrc

alias gotomusic='cd /home/user/music'
alias gotopics='cd /home/user/pictures'

etc...

share|improve this answer
    
Thanks! That's a much simpler solution. –  user1953384 Aug 2 '13 at 11:39
add comment

The problem is, that you can't change the working directory of the parent process.

Related to your problem the procedure is as follows:

The current working directory of your interactive shell is $PWD. Then you start gotomusic and your interactive shell (i will call it parentbash) starts a new instance of bash (i call it childbash) as a new process. childbash inherits several attributes of parentbash. Among other things it inherits it's working directory from parentbash.

Since you instruct childbash to read the commands from your script it changes its own working directory to /media/sda8/Music and terminates.

At this point parentbash gets active again with the same state as before the execution of childbash.

In order that parentbash should invoke its cd builtin command you should source your script. This is easily done by

source gotomusic

But the question remains if this approach is well suited for a quick directory change.

There are several implementations of directory-bookmark systems bashmarks but i think the best solution would be that you can easily choose from a list of the most used directories. I don't know whether there exists something like this for bash but if you want to use zsh you can get it at [1].

share|improve this answer
    
Wow that's interesting. I'd vote up your reply if I could :) –  user1953384 Aug 2 '13 at 11:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.