Find the answer to your Linux question:
Results 1 to 10 of 10
Hi there, I would like to create a script, for example "xyz" that will change my current working directory to a specific location in my Linux file system. I tried ...
  1. #1
    WMD
    WMD is offline
    Just Joined!
    Join Date
    Dec 2006
    Posts
    6

    Scripts to change the working directory

    Hi there,

    I would like to create a script, for example "xyz" that will change my current working directory to a specific location in my Linux file system.

    I tried this with adding "cd /home/WMD/XYZFolder" to a script file called xyx, it would not work.

    I put xyz in the /user/local/bin folder and set its permisions to (user=)rwx. I know that the script executed, because I added an echo statement which was displayed when I ran it. However, it never left me in the said folder.

    What's the trick?

    Thanks,
    WMD

  2. #2
    Just Joined!
    Join Date
    Feb 2003
    Posts
    6
    When you execute a script (i.e. /path/to/script) it runs in runs in a separate shell and then exits to your original shell. In your case the directory change happens in the shell the script is running in. So to get it to run in the current shell, you have to source it.

    Create your script with the cd command.

    Then run it like this:
    Code:
    $ source /path/to/script
    I would create your script and then add an alias to your .bashrc file like this:
    Code:
    alias xyz='source /path/to/script'
    Then you should be able to type xyz at the command line and get what you want.

  3. #3
    WMD
    WMD is offline
    Just Joined!
    Join Date
    Dec 2006
    Posts
    6
    Thanks for the tip. I'll give it a shot.

  4. #4
    Linux Newbie hughitt1's Avatar
    Join Date
    Oct 2005
    Location
    Baltimore, MD USA
    Posts
    167
    Aliases would work well for that purpose as well (e.g. http://bliki.rimuhosting.com/space/k...g+with+aliases)

  5. #5
    Just Joined!
    Join Date
    Feb 2003
    Posts
    6
    hugghitt1 is right.

    You could real easily create an alias to do this. If all you have in your script is to change directory, no point in scripting that.

    Just create an alias in .bashrc

  6. #6
    WMD
    WMD is offline
    Just Joined!
    Join Date
    Dec 2006
    Posts
    6
    Quote Originally Posted by Netizen
    hugghitt1 is right.

    You could real easily create an alias to do this. If all you have in your script is to change directory, no point in scripting that.

    Just create an alias in .bashrc
    I've been looking around for .bashrc for a while. Yesterday I found it in /usr/local/bin. I added the previously mentioned file "xyz" to the same folder and inserted an alias='source /usr/bin/local/xyz' line to .bashrc. I also played around with chmod on some test folders and I had to use it to make xyz work properly. Here's the result: This morning, nothing was accessible. I got libc??? input/output errors, couldn't run any programs and got stuck shutting Linux down. I powered off and on, and my boot failed.

    Any ideas? Can a bit of tweaking in the Linux system do this?
    I am using UBUNTU 6.0 something LTS. I think I'll try to boot the UBUNTU CD again and see what it can do.

    Hmm, not a good start to today.

  7. #7
    Just Joined!
    Join Date
    Feb 2003
    Posts
    6
    Quote Originally Posted by WMD
    Can a bit of tweaking in the Linux system do this?
    I am using UBUNTU 6.0 something LTS. I think I'll try to boot the UBUNTU CD again and see what it can do.

    Hmm, not a good start to today.
    Yes..depending on what you change.

    First off, whats in your script? are you just doing a change directory?

    I would do the following alias in your bashrc (located at /home/you/.bashrc):

    alias xyz='cd /path/to/directory'

    Then you could just type $ xyz and change directories. No scripts, no changing permissions on folders.

    Second, if you have more than cd in the script and are the only one using the script, just leave it in your home directory. I have a lot of scripts I have written, and I just put them in:

    /home/me/bin

    root can still use them with the path, and you can always add the path to your profile.

    Here is a sample .bashrc file:

    http://tldp.org/LDP/abs/html/sample-bashrc.html

  8. #8
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    If you are going to the "specific location" often, there is a feature in many shells, including bash, that may serve your purpose. It is CDPATH. It seems relatively unknown, but I have used it upon occasion when I have a project directory buried deep and I want to quickly go there frequently.

    There is an easy-to-read article at http://www.macosxhints.com/article.p...05031814311425 , but not very much beyond a mention in man bash.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  9. #9
    WMD
    WMD is offline
    Just Joined!
    Join Date
    Dec 2006
    Posts
    6
    Quote Originally Posted by Netizen
    Yes..depending on what you change.

    First off, whats in your script? are you just doing a change directory?

    I would do the following alias in your bashrc (located at /home/you/.bashrc):

    alias xyz='cd /path/to/directory'

    Then you could just type $ xyz and change directories. No scripts, no changing permissions on folders.

    Second, if you have more than cd in the script and are the only one using the script, just leave it in your home directory. I have a lot of scripts I have written, and I just put them in:

    /home/me/bin

    root can still use them with the path, and you can always add the path to your profile.

    Here is a sample .bashrc file:

    http://tldp.org/LDP/abs/html/sample-bashrc.html
    I am just changing directories. Later I may add some setup. Eventually I want more than 100 abc shortcuts being installed, for each of the main xyz shortcuts. For now, this sounds much simpler than I expected. Thanks for the tips and the link.

  10. #10
    WMD
    WMD is offline
    Just Joined!
    Join Date
    Dec 2006
    Posts
    6
    Quote Originally Posted by drl
    Hi.

    ...It is CDPATH...

    There is an easy-to-read article at http://www.macosxhints.com/article.p...05031814311425 , but not very much beyond a mention in man bash.

    Best wishes ... cheers, drl
    Thanks for the suggestion and the link.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...