Find the answer to your Linux question:
Results 1 to 3 of 3
Hello all, I want to move to some direcotory on my server, but to get to this directory I must type 3 long names of parent direcotiries everytime. Instead of ...
  1. #1
    Just Joined!
    Join Date
    Jan 2007
    Posts
    3

    Move to a directory with shell/bash scripting

    Hello all,
    I want to move to some direcotory on my server, but to get to this directory I must type 3 long names of parent direcotiries everytime.
    Instead of that I want to write a script that take me to this directory when I'm running the file that contains the script.
    I wrote this one, but it won't work:
    The name of the script file is for example : scriptfile.
    The name of my destination directory is: destdir.

    #!/bin/sh
    scriptfile=cd /longdirname/longdirname/destdir

    I would like to get some help with this.
    Thanks ahead,
    Boris Reuven

  2. #2
    xuo
    xuo is offline
    Just Joined!
    Join Date
    Oct 2006
    Posts
    29
    Hi,

    I am not sure to understand what you want to do but :
    if you want to go to a directory when you are in a shell window, you can create an alias (under csh/tcsh)
    Ex : in your .cshrc/.tcshrc file :
    alias newdir 'cd /longdirname/longdirname/destdir'

    Then under the shell prompt :
    # newdir

    or if you prefer to run a script :

    #!/bin/sh
    cd /longdirname/longdirname/destdir
    exit 0

    # chmod 755 scriptfile
    <put the directory where scriptfile script is in your PATH environment>
    # scriptfile

    Regards.

    Eric.

  3. #3
    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.

    Generally, each process inherits settings from the parent, but not vice-versa. So if you have a script that does a cd, it will do that in the current script process, but that will have no effect on the parent (unless you use source or ".", q.v., to run the script in the current process). I find the latter to be non-intuitive.

    For interactive work, I use variable CDPATH (cdpath in tcsh) to avoid having to enter long sequences. Auto-completion is another possibility ... cheers, drl
    CDPATH The search path for the cd command. This is a colon-separated
    list of directories in which the shell looks for destination
    directories specified by the cd command. A sample value is
    ".:~:/usr".
    -- excerpt from man bash
    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 )

Posting Permissions

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