bash directory site shortcuts

When I type cd ~foo, I would certainly such as bash to take me to some directory site foo as a faster way for keying the complete directory site course of foo. and also I would certainly such as to have the ability to cp ~foo/bar.txt ~/bar.txt to replicate a documents from the /foo/ directory site to the residence directory site ... So primarily, I desire something that functions specifically like ~/ does, yet where I define what the directory site needs to be. [I'm certain I need to jfgi, yet I do not recognize what to fg]

2019-05-04 04:54:39
Source Share
Answers: 5

A means would certainly be developing a pen name for cd which changes ~c to the needed course. Or simply make use of zsh ;)

2019-05-12 03:42:08

For transforming directory sites, you can make use of wcd : Wherever Change Directory

With that, it'll resemble wcd plugin_root.

2019-05-12 03:35:50

The means I made use of to do this is to create a directory site which contains symlinks to the directory sites you desire faster ways do, and also add that directory site to your CDPATH. CDPATH controls where cd will certainly look when you switch over directory sites, so if that directory site of symlinks remains in your CDPATH you can cd to any one of the symlinked directory sites promptly :

mkdir ~/symlinks
ln -s /usr/bin ~/symlinks/b
export CDPATH=~/symlinks
cd b   # Switches to /usr/bin

The disadvantage certainly is it will not function if there's a directory site in your existing directory site called "b"-- that takes priority over the CDPATH

I generally do not like solutions that claim "first you require to switch over coverings", yet this specific attribute exists in ZSH, if you're eager to make use of that rather; it's called called directory sites . You export a variable foo, and also when you describe ~foo it settles to the value of $foo. This is specifically hassle-free due to the fact that it operates in commands besides cd :

echo hi > /tmp/test
export t=/tmp
cat ~t/test   # Outputs "hi"
2019-05-07 23:26:46

You can write a wrapper function for cd and also call it "cd" (inevitably the function will certainly call builtin cd - making use of the builtin search phrase ). You can make use of a prefix personality that Bash will not expand on the command line prior to your function sees it which's not likely to look like the first personality in your directory site names, probably" :". You would certainly intend to make it extra durable, yet below's a straightforward synopsis :

# format: [semicolon] shortcut colon destination [semicolon] ...
export CDDATA='foo:/path/to/foo;bar:/path/to/bar;baz:/path/to/baz'

cd () {
    local dest=$1
    if [[ $dest == :* ]]
        [[ $CDDATA =~ (^|;)${dest:1}:([^;]*)(;|$) ]]
    if [[ -z $dest ]]
        builtin cd "$dest"

cd :bar    # pwd is now /path/to/bar
2019-05-07 23:17:17

with bash :

~foo is booked for the residence directory site of the customer foo. I would certainly not advise developing users simply for that ease.

You can make your life less complicated (or tougher ) when transforming directory sites by establishing the CDPATH setting variable (appearance it up in bash(1) ).

In addition to that, the only means to consider would certainly be to set setting variables for those directory sites you intend to abbreviate.

$ FOODIR=/var/lib/misc
$ cp ~/bar.txt $FOODIR
2019-05-07 23:16:18