Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

unix

cd

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 ??

like image 397
NumenorForLife Avatar asked Nov 29 '25 12:11

NumenorForLife


2 Answers

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

like image 89
Andrey Sabitov Avatar answered Dec 02 '25 01:12

Andrey Sabitov


You can always add the directory path in ~/.bashrc

vi ~/.bashrc

export FAV_DIR1=''

The variables in .bashrc load into the environment on new session. So make sure to reboot.

Then you can visit the directory by something like:

cd $FAV_DIR1

like image 26
Vedant Panchal Avatar answered Dec 02 '25 01:12

Vedant Panchal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!