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

7 Answers 7

up vote 18 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

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

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

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

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

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

Bash variables can be used to create a bookmarking system. Variables will work with any command and bash will tab complete both the variable name and, if a / is appended to the name, the path that the variable contains.

mydir=/home/chris/dir
ls $my         #tab completion works on variable name
ls $mydir/     #tab completion is equivalent to that with ls /home/chris/dir/

For persistence, variable declarations can be kept in a file that is sourced from .bashrc. Since this file is a bash script, it can contain declarations that reference other variables, such as aur="${HOME}/AUR", or that only run on certain hosts if [[ $(hostname) == foo ]]; then bar=baz; fi, which is useful if you like to reuse config files across multiple hosts and users.

The following is a bash function (to be added to .bashrc, or sourced from it) that allows bookmarks to be added to and removed from the bookmarks file. It is fairly new and not guaranteed to be free of bugs.

bookmark_file=~/.bookmarks
source "$bookmark_file"

bm() {
usage='Usage:
bm add <name> <path>           Create a bookmark for path.
bm add <name>                  Create a bookmark for the current directory.
bm update                      Source the bookmark file.
bm remove <name>               Remove a bookmark'              

case $1 in
    add)
        local path
        if [[ $# -eq 2 ]]; then
            path=.
        elif [[ $# -eq 3 ]]; then
            path="$3"
        fi
        if [[ -n $path ]]; then
            if [[ -n $(eval echo \$$2) ]]; then
                echo "The variable name $2 is in use."
                return 1
            fi
            path=$(readlink -f "$path")
            echo ${2}=\""$path"\" >> "$bookmark_file"
            eval ${2}=\""$path"\"
            return 0
        fi
        ;;
    update)
        if [[ $# -eq 1 ]]; then
            source "$bookmark_file"
            return 0
        fi
        ;;
    remove)
        if [[ $# -eq 2 ]]; then
            unset $2
            local contents=$(grep -v ${2}= "$bookmark_file")
            echo "$contents" > "${bookmark_file}.tmp"
            rm -f "$bookmark_file"
            mv "${bookmark_file}.tmp" "$bookmark_file"
            return 0
        fi
        ;;
esac

echo "$usage"
return 1
}
share|improve this answer

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.