Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When I type cd ~foo, I'd like bash to take me to some directory foo as a shortcut for typing the full directory path of foo. and I'd like to be able to cp ~foo/bar.txt ~/bar.txt to copy a file from the /foo/ directory to the home directory... So basically, I want something that works exactly like ~/ does, but where I specify what the directory should be. [I'm sure I should jfgi, but I don't know what to fg]

share|improve this question
add comment

6 Answers

up vote 17 down vote accepted

The way I used to do this is to create a directory that contains symlinks to the directories you want shortcuts do, and add that directory to your CDPATH. CDPATH controls where cd will search when you switch directories, so if that directory of symlinks is in your CDPATH you can cd to any of the symlinked directories instantly:

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

The downside of course is it won't work if there's a directory in your current directory named "b" -- that takes precedence over the CDPATH


I normally dislike answers that say "first you need to switch shells", but this exact feature exists in ZSH, if you're willing to use that instead; it's called named directories. You export a variable foo, and when you refer to ~foo it resolves to the value of $foo. This is especially convenient because it works in commands besides cd:

echo hi > /tmp/test
export t=/tmp
cat ~t/test   # Outputs "hi"
share|improve this answer
1  
In what ways does this ZSH feature differ from doing: echo hi> /tmp/test;export t=/tmp; cat $t/test At least on my machine, this works just fine. The only difference is the character you have to type. –  Steven D Sep 1 '10 at 16:37
    
referring the op to zsh is the right thing to do. i've not missed that feature once in 15 years. seems like the kind of thing some people get really obsessed about while others don't care. that's why there is more than one shell. –  hop Sep 1 '10 at 17:06
2  
@Steven It's pretty similar, but ZSH knows it's a named directory in this case so it can treat it specially in prompt expansions and shell built-in commands –  Michael Mrozek Sep 1 '10 at 18:34
    
[I'd mostly be doing this using emacs M-x shell (since that's when I use the terminal), I guess that rules out ZSH solutions]... –  Seamus Sep 2 '10 at 0:13
1  
Added it to my .zshrc and that worked. –  Seamus Sep 3 '10 at 18:14
show 4 more comments

You could write a wrapper function for cd and call it "cd" (ultimately the function will call builtin cd - using the builtin keyword). You could use a prefix character that Bash won't expand on the command line before your function sees it and that's unlikely to appear as the initial character in your directory names, perhaps ":". You would want to make it more robust, but here's a simple outline:

# 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 == :* ]]
    then
        [[ $CDDATA =~ (^|;)${dest:1}:([^;]*)(;|$) ]]
        dest=${BASH_REMATCH[2]}
    fi
    if [[ -z $dest ]]
    then
        cd
    else
        builtin cd "$dest"
    fi
}

cd :bar    # pwd is now /path/to/bar
share|improve this answer
1  
This is a really good idea –  Michael Mrozek Sep 1 '10 at 22:38
add comment

with bash:

~foo is reserved for the home directory of the user foo. I would not recommend creating users just for that convenience.

You can make your life easier (or harder) when changing directories by setting the CDPATH environment variable (look it up in bash(1)).

Apart from that, the only way to think of would be to set environment variables for those directories you want to abbreviate.

$ FOODIR=/var/lib/misc
$ cp ~/bar.txt $FOODIR
share|improve this answer
add comment

An way would be creating an alias for cd which replaces ~c to the wanted path. Or just use zsh ;)

share|improve this answer
2  
Yes, I thought about doing that, like alias ~c="cd /daten/cvs/plugin_root" but that only helps with cd, while named directories can be used with every command. –  fschmitt Sep 22 '10 at 11:01
add comment

For changing directories, you could use wcd: Wherever Change Directory

With that, it'll be like wcd plugin_root.

share|improve this answer
    
I wanted something that works with commands other than cd as well... –  Seamus Sep 23 '10 at 16:37
add comment

I have the below function which will create alias on the fly,

s () {
        if [[ "x$1" != "x" ]]
        then
                alias $1="cd $PWD;pwd"
                echo "alias $1=\"cd $PWD;pwd\""
        else
                echo "Usage: s[s] <directory bookmark name>"
                return 1
        fi
}

When I want to bookmark a directory, I just type s dirName. This creates a alias like alias dirName="cd /my/current/directory/;pwd". so I can come back to this directory by just typing dirName. I have a version which saves it into bash_aliases as well. This works in any shell.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.