creating abbreviations for commonly used paths

6,690

In any shell, you can define a variable.

justpath=~/Dropbox/thisfolder

(Note: no quotes here, otherwise the ~ wouldn't be expanded.) Prefix it with a $ to use it:

cp $justpath/blahfile .

Note that unless you're using zsh, if the value contains whitespace or wildcards *?\[, you need to put double quotes around the variable expansion when you use it.

justpath=~/'Dropbox/that folder'
cp "$justpath/blahfile" .

Zsh has (as it often does) better facilities. You can define named directories accessed with the syntax ~foo, generalizing the case where foo is a user name and ~foo is this user's home directory.

alias -d justpath=~/Dropbox/thisfolder
cp ~justpath/blahfile .

And for more complex cases, zsh offers dynamic named directories.

Share:
6,690

Related videos on Youtube

Curious2learn
Author by

Curious2learn

Updated on September 18, 2022

Comments

  • Curious2learn
    Curious2learn over 1 year

    I was wondering whether it is possible to create abbreviations that can be used in terminal. I know about alias command, but am not sure whether that can be used for what I am looking for.

    Example: Say I often need to copy stuff from folder ~/Dropbox/thisfolder. I know that I can create a shortcut to switch to this folder by creating an alias, such as,

    alias tf="cd ~/Dropbox/thisfolder"
    

    However, if I do

    alias justpath="~/Dropbox/thisfolder"
    

    then, I cannot use commands such as cp justpath/blahfile ./. Is it possible to do something like this using some other way to abbreviate the path ~/Dropbox/thisfolder?

    • Mikel
      Mikel about 12 years
      Use a variable, e.g. justpath=~/Dropbox/thisfolder then cp $justpath...
    • jw013
      jw013 about 12 years
      I use symlinks, which I collect in a "shallow" easily accessible directory, like ~/Desktop. This way the short paths are shell-agnostic. The only caveat is to be careful running recursive commands on the symlink dir (tell them to not follow symlinks).
    • Michael Mrozek
      Michael Mrozek about 12 years
      This depends a lot on your shell; there are several good ways in zsh
  • Ez0r
    Ez0r over 11 years
    Strictly speaking that's not for any shell. In csh the variables are set with the set command.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 11 years
    @AlexanderShcheblikin “Shell” in the unix world today (as opposed to 20 years ago) usually means Bourne-style shell by default. Csh, fish, psh, non-command-line shells and so on aren't included.
  • Ez0r
    Ez0r over 11 years
    I agree, csh is mostly terrible, but "default" and "any" are quite different things anyway.