How to make an "alias" for a long path?
Solution 1
Since it's an environment variable (alias has a different definition in bash
), you need to evaluate it with something like:
cd "${myFold}"
or:
cp "${myFold}/someFile" /somewhere/else
But I actually find it easier, if you just want the ease of switching into that directory, to create a real alias (in one of the bash
startup files like .bashrc
), so I can save keystrokes:
alias myfold='cd ~/Files/Scripts/Main'
Then you can just use (without the cd
):
myfold
To get rid of the definition, you use unalias
. The following transcript shows all of these in action:
pax> cd ; pwd ; ls -ald footy
/home/pax
drwxr-xr-x 2 pax pax 4096 Jul 28 11:00 footy
pax> footydir=/home/pax/footy ; cd "$footydir" ; pwd
/home/pax/footy
pax> cd ; pwd
/home/pax
pax> alias footy='cd /home/pax/footy' ; footy ; pwd
/home/pax/footy
pax> unalias footy ; footy
bash: footy: command not found
Solution 2
There is a shell option cdable_vars
:
cdable_vars
If this is set, an argument to thecd
builtin command that is not a directory is assumed to be the name of a variable whose value is the directory to change to.
You could add this to your .bashrc
:
shopt -s cdable_vars
export myFold=$HOME/Files/Scripts/Main
Notice that I've replaced the tilde with $HOME
; quotes prevent tilde expansion and Bash would complain that there is no directory ~/Files/Scripts/Main
.
Now you can use this as follows:
cd myFold
No $
required. That's the whole point, actually – as shown in other answers, cd "$myFold"
works without the shell option. cd myFold
also works if the path in myFold
contains spaces, no quoting required.
This usually even works with tab autocompletion as the _cd
function in bash_completion
checks if cdable_vars
is set – but not every implementation does it in the same manner, so you might have to source bash_completion
again in your .bashrc
(or edit /etc/profile
to set the shell option).
Other shells have similar options, for example Zsh (cdablevars
).
Solution 3
Maybe it's better to use links
Soft Link
Symbolic or soft link (files or directories, more flexible and self documenting)
# Source Link
ln -s /home/jake/doc/test/2000/something /home/jake/xxx
Hard Link
Hard link (files only, less flexible and not self documenting)
# Source Link
ln /home/jake/doc/test/2000/something /home/jake/xxx
How to create a link to a directory
Hint: If you need not to see the link in your home you can start it with a dot . ; then it will be hidden by default then you can access it like
cd ~/.myHiddelLongDirLink
Solution 4
First off, you need to remove the quotes:
bashboy@host:~$ myFolder=~/Files/Scripts/Main
The quotes prevent the shell from expanding the tilde to its special meaning of being your $HOME
directory.
You could then use $myFolder
an environment a shell variable:
bashboy@host:~$ cd $myFolder
bashboy@host:~/Files/Scripts/Main$
To make an alias, you need to define the alias:
alias myfolder="cd $myFolder"
You can then treat this sort of like a command:
bashboy@host:~$ myFolder
bashboy@host:~/Files/Scripts/Main$
Solution 5
You can add any paths you want to the hashtable of your bash:
hash -d <CustomName>=<RealPath>
Now you will be able to cd ~<CustomName>
. To make it permanent add it to your bashrc script.
Notice that this hashtable is meant to provide a cache for bash not to need to search for content everytime a command is executed, therefore this table will be cleared on events that invalidate the cache, e.g. modifying $PATH
.
Related videos on Youtube
bashboy
Updated on October 07, 2020Comments
-
bashboy over 3 years
I tried to make an "alias" for a path that I use often while shell scripting. I tried something, but it failed:
myFold="~/Files/Scripts/Main" cd myFold bash: cd: myFold: No such file or directory
How do I make it work ?
However,cd ~/Files/Scripts/Main
works.-
nsrCodes about 3 yearsI made a simple CLI for the same and it is proving to be very useful in overcoming this. Hope it helps you too :) github.com/nsrCodes/cdd
-
-
bashboy almost 11 yearsThanks. That works. I tried only alias myfold='~/some/path' and cd myfold. I get an error - no such directory. Why ? Also, is it possible to "un-assign" an alias variable ?
-
bashboy almost 11 yearsmyfold='~/some/path' cd "$myfold" Fails. Why ?
-
paxdiablo almost 11 years@bashboy, probably because you have no directory with that name. ~/some/path was an example, you should substitute the actual path you want to use. I'll change it to use your example directory so it's clearer. As to how to unassign an alias, you use the unalias command. I've also updated the answer with a transcript, including the use of that command.
-
Keith Thompson almost 11 yearsIt's not actually an environment variable unless you
export
it. -
David W. almost 11 years@KeithThompson Okay, it's just a shell variable. But, you don't want to export it because you don't want it to possibly affect child processes. You define it this way it in your
.bashrc
or whatever resource file you use, and it will be defined in the immediate shell, but not in shell scripts. -
goldisfine almost 9 yearsHow would you preserve this alias in different terminal sessions? When I open up a new tab and try to use this alias, it doesn't work.
-
paxdiablo almost 9 years@goldisfine, see my parenthetical
in one of the bash startup files like .bashrc
. Putting the alias into a suitable startup file will ensure it's available for all sessions. -
Vassilis almost 7 yearsI prefer this solutions because it works with auto completition
-
Will over 6 yearsI don't think this answers the question; it's specific only to
cd
. What if the user wants to domv sourcedir/filename reallylongpath
? -
paxdiablo over 6 years@Will, I think you'll find that's covered by the very first paragraph. I'll clarify.
-
User5910 about 6 yearsOne downside: If you
cd myFold
thenpwd
, it will show you're in myFold. This is a deal breaker for me because relative commands with p4 source control depend on the current directory. -
dvj almost 6 years@User5910 - use
pwd -P
-
Ryan Stull almost 6 yearsIs there an equivalent of this for ZSH?
-
Benjamin W. almost 6 years@RyanStull
setopt cdablevars
looks like it might be, but I don't know zsh at all. -
Benjamin W. over 5 years@JohnMee I'll roll your edit back, as a) the option is called differently in zsh (no underscore) b) the question tagged just Bash and c) I link to the Bash manual. I'll add a paragraph to mention the other shells, though.
-
Benjamin W. over 5 years@JohnMee And I can't find the option for csh or tcsh, what is it called?
-
Lucas P. over 5 yearsSuch a great answer! Thank you very much for the
cdable_vars
-
Muhammad Yusuf over 5 yearsBut cdable_vars doesn't give me the ability to do other commands such that ls for example
-
Benjamin W. over 5 years@MuhammadYusuf How do you mean? What are you trying to do?
-
Muhammad Yusuf over 5 years@BenjaminW. I want to have an alias for a directory, so that I can
cd
,ls
,mkdir
, ... and all needed operations on a directoy but cdable_vars only gives me the ability to cd into the aliased directory -
Benjamin W. over 5 years@MuhammadYusuf Yes, it's only aliasing for
cd
. I'm not sure if the kind of global alias you're looking for is possible in Bash. -
Dorin Brage over 4 yearsI used to use alias such as
alias project='cd /path/to/project'
and you just gave me a much better solution, thanks! -
sergej over 4 yearsI think it will only work in
zsh
but not inbash
. -
John Stewart over 4 yearsThis is the best answer IMHO, but it's true it works in
zsh
only, at least on my Ubuntu system. -
Erik J. over 4 yearsFor permanently storing the aliases it's advisable to add them to
~/bash_aliases
in stead of~/bashrc
. See askubuntu.com/a/5278/1045718 -
paxdiablo over 4 years@Erik, keep in mind that's a convention, not something that
bash
itself does. It will only work if a "real"bash
startup script calls it. -
Benjamin W. over 3 years@Vladislav Notice that that breaks the completion for
cd
provided by bash-completion, which uses the_cd
function (complete -o nospace -F _cd cd
). Adding-v
to that (to complete on variables) works, but can have undesired side effects – for me, it shows my complete environment when I tab complete. -
Vladislav over 3 years@BenjaminW., yes, you are right - I did not notice that it breaks regular autocompletion for
cd
command 🤦♂️. -
Benjamin W. over 3 years@Vladislav You can still try
complete -o nospace -F _cd -v cd
if your environment doesn't contain too many variables. -
Vladislav over 3 years@BenjaminW., unfortunately, it raises
bash: completion: function '_cd' not found
on Git for Windows. Maybe I have to declare this function in\Program Files\Git\mingw64\share\git\completion\git-completion.bash
? There is an example of_cp
in stackoverflow.com/q/26550327/155687 -
Benjamin W. over 3 years@Vladislav It's part of the bash-completion package, see here; if you don't have bash-completion, there is an example for
cd
completion in the Bash manual.