Linux 如何为长路径制作“别名”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17958567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make an "alias" for a long path?
提问by bashboy
I tried to make an "alias" for a path that I use often while shell scripting. I tried something, but it failed:
我试图为我在编写 shell 脚本时经常使用的路径创建一个“别名”。我尝试了一些东西,但失败了:
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.
我如何使它工作?
但是,cd ~/Files/Scripts/Main
有效。
采纳答案by paxdiablo
Since it's an environment variable (alias has a different definition in bash
), you need to evaluate it with something like:
由于它是一个环境变量(别名在 中有不同的定义bash
),您需要使用以下内容对其进行评估:
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 realalias (in one of the bash
startup files like .bashrc
), so I can save keystrokes:
但我实际上发现它更容易,如果您只想轻松切换到该目录,创建一个真正的别名(在一个bash
启动文件中,如.bashrc
),这样我就可以保存击键:
alias myfold='cd ~/Files/Scripts/Main'
Then you can just use (without the cd
):
然后你就可以使用(没有cd
):
myfold
To get rid of the definition, you use unalias
. The following transcript shows allof these in action:
要摆脱定义,请使用unalias
. 以下成绩单显示了所有这些操作:
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
回答by hek2mgl
First, you need the $
to access "myFold"'s value to make the code in the question work:
首先,您需要$
访问“myFold”的值以使问题中的代码起作用:
cd "$myFold"
To simplify this you create an alias in ~/.bashrc
:
为了简化这一点,您可以在~/.bashrc
以下位置创建别名:
alias cdmain='cd ~/Files/Scripts/Main'
Don't forget to source the .bashrc
once to make the alias become available in the current bash session:
不要忘记源.bashrc
一次以使别名在当前 bash 会话中可用:
source ~/.bashrc
Now you can change to the folder using:
现在您可以使用以下方法更改文件夹:
cdmain
回答by shellter
but an actual alias for a dir is also possible, try
但是目录的实际别名也是可能的,请尝试
myScripts="~/Files/Scripts/Main"
alias myScripts="cd $myScripts"
This way you have a common naming convention (for each dir/alias pair), and if you need to copy something from the current dir to myScripts, you don't have to think about it.
这样你就有了一个通用的命名约定(对于每个目录/别名对),如果你需要从当前目录复制一些东西到 myScripts,你不必考虑它。
IHTH
IHTH
回答by David W.
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.
引号可防止 shell 将波浪号扩展为作为您的$HOME
目录的特殊含义。
You could then use $myFolder
an environmenta shell variable:
然后,您可以使用$myFolder
环境shell 变量:
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$
回答by Rodrigo Martins de Oliveira
You can add any paths you want to the hashtable of your bash:
您可以将您想要的任何路径添加到 bash 的哈希表中:
hash -d <CustomName>=<RealPath>
hash -d <CustomName>=<RealPath>
Now you will be able to cd ~<CustomName>
. To make it permanent add it to your bashrcscript.
现在您将能够cd ~<CustomName>
。要使其永久添加到您的bashrc脚本中。
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
.
请注意,此哈希表旨在为 bash 提供缓存,而无需在每次执行命令时搜索内容,因此该表将在使缓存无效的事件(例如修改$PATH
.
回答by Benjamin W.
There is a shell option cdable_vars
:
有一个 shell 选项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.
cdable_vars
如果设置了此参数,cd
则假定不是目录的内置命令的参数是变量的名称,该变量的值是要更改到的目录。
You could add this to your .bashrc
:
您可以将此添加到您的.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
.
请注意,我已将波浪号替换为$HOME
; 引号会阻止波浪号扩展,Bash 会抱怨没有目录~/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.
不需要$
。实际上,这就是重点——如其他答案所示,在cd "$myFold"
没有 shell 选项的情况下工作。cd myFold
如果路径中myFold
包含空格,也可以使用,不需要引用。
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).
这通常甚至适用于选项卡自动完成,因为检查中的_cd
函数bash_completion
是否cdable_vars
已设置 - 但并非每个实现都以相同的方式执行此操作,因此您可能需要bash_completion
在您的.bashrc
(或编辑/etc/profile
以设置 shell 选项)中再次获取源代码。
Other shells have similar options, for example Zsh (cdablevars
).
其他 shell 也有类似的选项,例如 Zsh ( cdablevars
)。
回答by dvj
Another option would be to use a symbolic link. ie:
另一种选择是使用符号链接。IE:
ln -s ~/Files/Scripts/Main ~/myFold
After that you can perform operations to ~/myFold
, such as:
之后,您可以对 执行操作~/myFold
,例如:
cp some_file.txt ~/myFold
which will put the file in ~/Files/Scripts/Main
. You can remove the symbolic link at any time with rm ~/myFold
, which will keep the original directory.
这会将文件放入~/Files/Scripts/Main
. 您可以随时使用 删除符号链接rm ~/myFold
,这将保留原始目录。
回答by Mahmoud Bahnasy
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
回答by Chayan Pathak
Put the following line in your myscript
将以下行放在你的 myscript 中
set myFold = '~/Files/Scripts/Main'
In the terminal use
在终端使用
source myscript
cd $myFold
回答by chb
The preceding answers that I tried do not allow for automatic expansion (autocompletion) of subdirectories of the aliased directory.
我尝试的上述答案不允许别名目录的子目录的自动扩展(自动完成)。
However, if you push the directory that you want to alias onto the dirs
stack...
但是,如果您将要别名的目录推送到dirs
堆栈上......
$ pushd ~/my/aliased/dir
...you can then type dirs -v
to see its numeric position in the stack:
...然后您可以键入dirs -v
以查看其在堆栈中的数字位置:
0 ~/my/aliased/dir
1 ~/Downloads
2 /media/usbdrive
and refer to it using that number for most if not all commands that expect a directory parameter:
并使用该编号为大多数(如果不是所有)需要目录参数的命令引用它:
$ mv foo.txt ~0
You can even use Tabto show the immediate subdirectories of the "aliased" directory:
您甚至可以使用Tab来显示“别名”目录的直接子目录:
$ cd ~0/<Tab>
child_dir1 child_dir2