You can give a directory (or any file) an abbreviated name by creating a symbolic link to it. A symbolic is a directory entry that doesn't actually contain any data, but points to another path where the actual data is to be found. Operating on the directory entry (e.g. create, rename, delete) manipulates the symbolic link, while operating on the content (e.g. read, write, cd
and ls
for a directory, etc.) operates on the target of the link. For example, create a symbolic link with the ln
command:
ln -s /A/Really/Long/File/Path/Name/Makes/My/Fingers/Hurt ~/hurt
Then cd ~/hurt
is mostly equivalent to /A/Really/Long/File/Path/Name/Makes/My/Fingers/Hurt
. It's mostly equivalent because the shell remembers and displays ~/hurt
as the current working directory; if you want the shell to forget about the symbolic link, use cd -P ~/hurt
.
If you very often change to subdirectories of a particular directory, you can use the CDPATH
variable. When you run cd
with a relative path (i.e. an argument that doesn't start with /
, either explicitly or via an abbreviation such as ~
or a variable whose value starts with /
), the shell tries to change to a subdirectory of each element of CDPATH
in turn until it finds one that exists. If you use CDPATH
, you are strongly recommended to put .
(the current directory) first, otherwise an innocent-looking cd subdir
could make you jump to a completely unrelated location.
CDPATH=.:/A/Really/Long/File/Path/Name/Makes/My
cd Fingers/Hurt
You can also define abbreviations inside the shell as variables. Use $
in front of the variable name to use its value. Note that if the variable's value contains special characters such as spaces, you need double quotes when using it (unless you use zsh or fish as your shell).
hurt=/A/Really/Long/File/Path/Name/Makes/My/Fingers/Hurt
cd $hurt
spaced='/A/Really Long/File Path Name/Makes My Fingers/Hurt'
cd "$spaced"
In bash the cdable_vars
option makes this easier on the fingers.
If a component in the path is long, use completion. If your Tab key isn't worn out, you're doing it wrong. For best results, avoid having many file names that have a few letters in common at the beginning, and avoid starting file names with hard-to-type characters such as uppercase letters.
cd -
(and possibly other options)