7

I'm doing something where I'm frequently changing between two directories far away from each other in the system's file tree.

Is there anyway I can assign some kind of short name to each one for use with the cd command so that I can type cd directoryA and cd directoryB for example, instead of repeatedly typing cd C:/A/Really/Long/File/Path/Name/Makes/My/Fingers/Hurt?

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

11 Answers11

12

For exactly two directories, use cd -

$ cd /tmp
$ cd /var/tmp
$ cd -
/tmp
$ cd -
/var/tmp
$ cd -
/tmp
$ 
thrig
  • 34,938
  • While this solves my current problem, it also raises the obvious question. What about for more than two directories? – Bassinator Mar 29 '17 at 17:49
  • 5
    You could use pushd. See help pushd or the bash manual. – Johan Myréen Mar 29 '17 at 17:55
  • This solves your current problem. If it raises another question for you... post that question. The simplest answer to the above specific question is the approach detailed here, i.e. cd -. The simplest answer to there being three directories will be different. – Michael Durrant Mar 30 '17 at 11:33
9

Is there anyway I can assign some kind of short name to each one

Yes of course, with the alias command:

alias directoryA='cd /path/to/directoryA'

Then use directoryA as an alias for your cd command. It's really that straightforward ;)

rahmu
  • 20,023
  • This is exactly what I am looking for, thanks. However, a few things: What is the lifespan of this alias? Is it then defined forever? How would I disable an alias? – Bassinator Mar 29 '17 at 18:09
  • 1
    @Airhead It's defined during that terminal's lifespan, to have that alias defined for each subsequent terminal you would need to include the alias command in your startup script (ex: .bashrc). I source a file named .bash_aliases in my .bashrc, I believe this is a common and advised practice. Use unalias to disable an alias. – Jonny Henly Mar 29 '17 at 18:28
  • Perfect, exactly what I needed, thanks a bunch. – Bassinator Mar 29 '17 at 18:29
6

I would use shell variables:

da=/really/long/path/to/directory/a
db=/other/really/long/path/to/directory/b

cd $da
cd $db

The advantage of this method is that you can use $da and $db in place of file paths in any command, for example:

cp $da/file1 $db/file2
user253751
  • 2,266
  • 3
    And, if you happen to already be in one of the directories you want to save (perhaps by having used [TAB] to get there), you can do da=\pwd`` to save the current directory in the variable. – TripeHound Mar 30 '17 at 07:44
  • I've come back to this question after a long time, because having more experience now, I feel that this is a better answer for new people encountering this question, for the reasons you mentioned. I've marked this answer as accepted. – Bassinator Aug 10 '21 at 14:41
3

Use a different shell than Bash for interactive work. I use Fish shell that allows me to type

$ cd /u/sh/sounds

and then press Tab. Afterwards the command line reads

$ cd /usr/share/sounds/

No need to define any aliases, just write enough for it to become unique.

2

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.

2

You can install autojump.

so you can type "j directoryA<enter>" or "j dire<tab>" to select in multi directories.

eexpress
  • 381
1

In bash, you can use the cdable_vars shell option to do something similar:

$ mkdir -p /tmp/a/b/c/d/e/f/g/h
$ mkdir -p /tmp/i/j/k/k/l/m/n/o
$ h=/tmp/a/b/c/d/e/f/g/h
$ o=/tmp/i/j/k/k/l/m/n/o
$ shopt -s cdable_vars
$ cd /tmp
$ cd h
$ pwd
/tmp/a/b/c/d/e/f/g/h
$ cd o
$ pwd
/tmp/i/j/k/k/l/m/n/o
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
1

Use Apparix. It allows you to define bookmarks for your directories. Then jumping to and fro between them, once configured, is as easy as

to foo
# Goes to /this/is/a/complex/path/for/foo

to bar
# Goes to /a/completely/unrelated/path/for/bar

to foo subdir
# Goes to /this/is/a/complex/path/for/foo/subdir

Etc. And all the shortcuts and subdirectories are auto-completed.

It’s an immensely powerful, highly underrated tool. I find it performs better than alternatives such as autojump because it gives the user more control.

1

If you're just moving directly between two directories, cd - will take you to the last working directory.

Arronical
  • 280
0

pushd is the bash command for this purpose.

Read the section on how to use it in man bash to learn how it works.

Mio Rin
  • 3,040
-1

Don't know about bash (I don't use it if I can possibly avoid doing so), but in tcsh & other shells, you could simply use the history mechanism. So you would type e.g. "cd /" and ctl-UpArrow to scroll back through all commands to cd to paths starting at root.

Note that this works for any command, not just cd. You just need the first few characters of whatever you want to scroll back to.

jamesqf
  • 185
  • 6