6

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?

  • 1
    You can have 2 terminals, each opened in one directory. You can use pushd and popd. You can use aliases And you can use cd - (and possibly other options) – njzk2 Mar 29 '17 at 19:50
  • Another option: symlink them both as subdirectories of the same place – pjc50 Mar 29 '17 at 22:07
  • this might also help you out github.com/wting/autojump – Sash Mar 30 '17 at 0:02
  • What shell are you using? – jamesdlin Mar 30 '17 at 10:25

11 Answers 11

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 ;)

  • 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
  • Actually, now that you mention it, I think I have done this previously so that I could use notepad++ from bash instead of vim. (Spoiler alert: I'm using Windows.) – Bassinator Mar 29 '17 at 18:34
10

For exactly two directories, use cd -

$ cd /tmp
$ cd /var/tmp
$ cd -
/tmp
$ cd -
/var/tmp
$ cd -
/tmp
$ 
  • 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
5

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
  • 2
    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 7:44
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.

  • you still need all the slashes, though, don't you? – njzk2 Mar 29 '17 at 19:50
  • The US keyboard layout makes typing a / really easy, there is a dedicated key for it. – Martin Ueding Mar 29 '17 at 19:51
  • it's still much slower than typing an alias or environment variable for very commonly used folders. – phuclv Mar 30 '17 at 9:08
  • 1
    Yes, for one path I just have defined a variable and just to cd $MA. This only works since there are no spaces in the path, otherwise it would have to be cd "$MA" in Bash. – Martin Ueding Mar 30 '17 at 9:11
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.

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
1

You can install autojump.

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

  • This is the correct solution to the problem. – whirlwin Mar 30 '17 at 6:28
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.

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.

-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.

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