bash - How can I change the "/" path in linux

07
2014-07
  • VarunAgw

    When I type cd /, it changes my directory to a default one. How can I override the current settings so that / points to a different directory.

  • Answers
  • gniourf_gniourf

    What a shame you didn't explain what you're trying to do exactly!

    So, in this land of unknown, I can suggest one solution (that will very likely not be a solution to your problem, but don't blame me, I just don't know what you're trying to achieve exactly).

    In Bash, there's a variable called CDPATH, that is a colon-separated list of directories used as a search path for the cd builtin command (the italic part is taken verbatim from the Bash reference manual).

    With this variable you can do something like this:

    $ mkdir -p ~/Test/{one,two,three/insidethree}
    $ export CDPATH=~/Test:~/Test/three
    $ cd /
    $ pwd
    /
    $ # Here we're at any location
    $ cd insidethree
    $ pwd
    /home/gniourf/Test/three/insidethree
    

    The good thing is that you'll have TAB-completion on every directory that is a subdirectory of a path in CDPATH.


    Now if this doesn't help with what you're trying to achieve, you can always override cd with a function of your own, as so:

    cd() {
        case $1 in
            (/) builtin cd "where you want to go when you type cd /" ;;
            (*) builtin cd "$@" ;;
        esac
    }
    

    You can even export this function so that this cd will be used from within scripts:

    export -f cd
    

    After that, if you really need to change current directory to /, you can:

    builtin cd /
    

    If you want to re-root everything at another directory, same thing applies:

    cd() {
        case $1 in
            (/*) builtin cd "My_new_root$1" ;;
            (*) builtin cd "$@" ;;
        esac
    }
    

    Of course, with this method you wont have TAB-completion capabilities, unless you explicitly write the completion rule (but that's another story). Also, this won't take any options into account. E.g., cd -- /, cd -L /, etc. will fail. You can of course modify the function to take options into account.

    I personally find this only suitable for mentally ill people, but I don't know what you're trying to achieve, so maybe this only shows my limited capabilities to imagine the wicked thing you have in mind! Unless you're playing a prank on your colleagues, in which case I'll find it funny. Or not.

    It would be slightly better to use another name for this cd function too.

  • LPChip

    The / will always point to the root that is specific for that user. If you want cd / to point to a different directory, create a new user and set its root to a different directory. Note that from that moment on, the user will not be able to go to any directory higher than that.

    Next time you use terminal, login with that user and its set.

    Alternatively create a directory junction in the root to wherever you want it to go and give it a short name such as l (closest to /) so you can type cd /l


  • Related Question

    linux - how to change the color of the terminal
  • Questioner

    I'm using CentOS 4.8. I like the grey background color and the black foreground color,by which my eyes wouldn't sore.

    when I set 'export PS1="\e[0;30m\e[47m\u@\h \w>"' , It only changes the bash prompt line colors.

    When I'm editing text via 'vi' or looking up information via 'info',the colors are back to default.

    So I'm wondering how to change the background and foreground color globally. thanks for any tip.

    updates:

    My CentOS is server version, so there is no gui interface.


  • Related Answers
  • ennuikiller

    The terminal color is set by a flag on the xterm command (if you're running X of course):

    -bg color
    This option specifies the color to use for the background of the window. The default is ''XtDefaultBackground.''
    
    -fg color
    This option specifies the color to use for displaying text. The default is ''XtDefaultForeground.''
    -fn font
    
  • Astha chauhan

    Gnome Terminal picks up the background colour from the profile in use, so any change will impact the background colour of all terminals using the same profile. The configuration key is stored in /apps/gnome-terminal/profiles/Default/background_color, where Default is the currently used profile. I have demonstrated it in below code.

    1. Open the file named %gconf.xml under */apps/gnome-terminal/profiles/Default
    2. Change the background and foreground colour accordingly.

       <?xml version="1.0"?>
        <gconf>
        <entry name="use_theme_colors" mtime="1401108737" type="bool" value="false"/>
        <entry name="visible_name" mtime="1401108737" type="string">
          <stringvalue>Default</stringvalue>
        </entry>
      <entry name="palette" mtime="1401108737" type="string">
          <stringvalue>#2E2E34343636:#CCCC00000000:#4E4E9A9A0606:#C4C4A0A00000:#34346565A4A4:#757550507B7B:#637AD089D262:#D3D3D7D7CFCF:#555557575353:#EFEF29292929:#8A8AE2E23434:#FCFCE9E94F4F:#72729F9FCFCF:#ADAD7F7FA8A8:#3434E2E2E2E2:#EEEEEEEEECEC</stringvalue>
      </entry>
      <entry name="background_color" mtime="1401108737" type="string">
          <stringvalue>#000000000000</stringvalue>
      </entry>
      <entry name="foreground_color" mtime="1401108737" type="string">
          <stringvalue>#FFFFFFFFFFFF</stringvalue>
      </entry>
      <entry name="bold_color" mtime="1401108737" type="string">
          <stringvalue>#000000000000</stringvalue>
      </entry>