unixcd

How can I save the path to a frequently used directory in UNIX?


Is there a way to save the path to a frequently used directory in UNIX, so instead of having to manually cd /path/to/directory I can just enter a shortcut cd myFavoritePath ??


Solution

  • Define your favorite directories in CDPATH environment variable. It's a colon-separated list of search paths available to the cd command. You should specify not a directory you want to switch but parent directory.

    Here is brief info about it: http://docstore.mik.ua/orelly/unix/upt/ch14_05.htm

    For example you have three directories you work with frequently:

    /home/user/scripts/favorite/
    /var/log/
    /var/lib/
    

    add to your ~/.bash_profile (or another shell profile file you use) the next line:

    export CDPATH=.:/home/user/scripts:/var
    

    In the example below I just redefine CDPATH in shell for the current session

    [user@server lib]$ CDPATH=.:/var:/home/user/scripts
    [user@server lib]$ cd log
    /var/log
    [user@server log]$ cd lib
    /var/lib
    [user@server lib]$ cd favorite
    /home/user/scripts/favorite
    

    If you want use tab while execute cd you can install bash-completion http://bash-completion.alioth.debian.org/ but it's optional

    Also do not forget cd - command for quick switching to previous working dir