-3

I would like to know how to add a directory to path (such as my Downloads folder) so that whenever I type cd Downloads from whatever directory, I go to ~/Downloads.

1
  • 3
    And what research have you done? Mentioning your shell is also helpful. – Quasímodo Oct 21 '20 at 22:53
5

CDPATH

If you're using a shell that supports the CDPATH environment variable, you can set

CDPATH=.:$HOME

in your shell start-up script. Then, whenever you use a cd <directory> command and <directory> does not exist as a sub-directory of your current directory, the command will look for it as a sub-directory of your home directory instead (e.g. as ~/<directory> and will move there if it finds a match.

A directory name beginning ./ or ../ inhibits the path search, as does an absolute pathname of course, but only some shells document this in their manuals. cd ./Downloads will not search the path.

Note that the rules for CDPATH vary subtly from shell to shell.

  • The Almquist, Korn, and the Bourne Again shells do not explicitly search . if you have a CDPATH variable, so the variable must explicitly contain a . entry as aforegiven in order to retain the expected behaviour of searching the current directory.
  • The Z shell in contrast searches . first, unless CDPATH explicitly contains a . entry or you have the POSIX_CD shell option set; so you can just use

    CDPATH=$HOME

    and you will still obtain the behaviour of searching your current directory. (This way of allowing control of when . is searched in a path is an old idea, that users of MS/PC/DR-DOS who used command interpreters such as 4DOS/NDOS will find achingly familiar.)
  • The Watanabe shell searches . last, after checking the directories listed in CDPATH.

I would deliberately not export the CDPATH variable, and would try to restrict its effect to interactive shells only. Having CDPATH in effect when executing complex scripts might have unexpected side effects.

cdpath

To that end, in the Z shell you can use the cdpath variable instead of CDPATH. The C shells also have cdpath, and do not have CDPATH at all.

cdpath is an array type shell variable, that is harder to accidentally export. Being an array, it is somewhat simpler to manipulate than the colon-separated form of CDPATH.

C shell and TENEX C shell

In the C shells you can execute:

set cdpath=(~)
Note that you do not have to include . as that is automatically searched first by the C shells before it looks at the cdpath shell variable. To append to an existing path use:

set cdpath=($cdpath:q ~)
The :q causes the variable expansion in the assignment to be quoted, so that it isn't then subjected to filename expansion or command substitution.

The C shells search . first, before searching the cdpath.

Z shell

In the Z shell you can execute:

cdpath=(~)
The same rules as for CDPATH in the Z shell apply; this being just a more convenient syntax for specifying the search path. To append to an existing path use:

cdpath+=(~)
The Z shell has :q like the C shells, but this syntax neatly avoids the need for it.

Notice also that the Z shell automatically synchronizes cdpath with CDPATH.

2
  • If you're going to mention not exporting the variable to the environment anyway, you might as well go the whole distance and mention the cdpath shell variable in the Z and C shells, and how it is an array. – JdeBP Oct 22 '20 at 5:18
  • @JdeBP Feel free to edit my answer; I don't know the Z shell all that well, and still get flashbacks to my student days from the C shell :-) – telcoM Oct 22 '20 at 5:24
0

If you mean you want Downloads on certain directories to go to ~/Downloads, you can place a symlink there:

ln -s ~/Downloads .

Now, if you want to make cd Downloads to go to ~/Downloads no matter where you do it, you could override cd with a function that did this. For example by including on .bashrc:

cd() { if [ "$1" = Downloads ]; then builtin cd ~/Downloads; else builtin cd "$1"; fi; }

I don't recommend changing the meaning of standard commands, though. That would cause a lot of confusion. If you really want a shorter you could create an alias called cdDownloads, or even just downloads that moved you there:

alias downloads="cd ~/Downloads"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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