긴 경로에 대한 "별칭"을 만드는 방법은 무엇입니까?
쉘 스크립팅 중에 자주 사용하는 경로에 대해 "별칭"을 만들려고했습니다. 나는 뭔가를 시도했지만 실패했습니다.
myFold="~/Files/Scripts/Main"
cd myFold
bash: cd: myFold: No such file or directory
어떻게 작동합니까?
그러나 cd ~/Files/Scripts/Main
작동합니다.
환경 변수 (별칭은에서 다른 정의 bash
를 가짐)이므로 다음과 같이 평가해야합니다.
cd "${myFold}"
또는:
cp "${myFold}/someFile" /somewhere/else
그러나 실제로 해당 디렉토리로 쉽게 전환하고 싶다면 실제 별칭 (예 : bash
시작 파일 중 하나) 을 만드는 것이 더 쉬우 .bashrc
므로 키 입력을 저장할 수 있습니다.
alias myfold='cd ~/Files/Scripts/Main'
그런 다음 (없이 cd
) 사용할 수 있습니다 .
myfold
정의를 제거하려면 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
쉘 옵션이 있습니다 cdable_vars
.
cdable_vars
이것이 설정되면,cd
디렉토리가 아닌 내장 명령에 대한 인수 는 값이 변경 될 디렉토리 인 변수의 이름으로 간주됩니다.
이것을 추가 할 수 있습니다 .bashrc
:
shopt -s cdable_vars
export myFold=$HOME/Files/Scripts/Main
물결표를 $HOME
; 따옴표는 물결표 확장을 방지하고 Bash는 디렉토리가 없다고 불평합니다 ~/Files/Scripts/Main
.
이제 다음과 같이 사용할 수 있습니다.
cd myFold
$
필요 하지 않습니다. 실제로 다른 답변에서 볼 수 있듯이 cd "$myFold"
쉘 옵션없이 작동합니다. cd myFold
경로에 myFold
공백 이 포함 된 경우에도 작동하며 따옴표가 필요하지 않습니다.
일반적으로이 _cd
기능 이 설정 되어 bash_completion
있는지 확인하기 때문에 탭 자동 완성과 함께 작동 cdable_vars
하지만 모든 구현이 동일한 방식으로 수행하는 것은 아니므로 bash_completion
다시 소스 .bashrc
(또는 /etc/profile
쉘 옵션을 설정하기 위해 편집) 해야 할 수 있습니다 .
다른 쉘에는 유사한 옵션이 있습니다 (예 : Zsh ( cdablevars
)).
링크를 사용하는 것이 더 나을 수도 있습니다.
소프트 링크
심볼릭 또는 소프트 링크 (파일 또는 디렉토리, 더 유연하고 자체 문서화)
# Source Link
ln -s /home/jake/doc/test/2000/something /home/jake/xxx
하드 링크
하드 링크 (파일 만 해당, 덜 유연하며 자체 문서화가 아님)
# Source Link
ln /home/jake/doc/test/2000/something /home/jake/xxx
힌트 : 집에서 링크를 볼 필요가없는 경우 점으로 시작할 수 있습니다. ; 기본적으로 숨겨져 있으며 다음과 같이 액세스 할 수 있습니다.
cd ~/.myHiddelLongDirLink
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
.
First, you need the $
to access "myFold"'s value to make the code in the question work:
cd "$myFold"
To simplify this you create an alias in ~/.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:
source ~/.bashrc
Now you can change to the folder using:
cdmain
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$
Another option would be to use a symbolic link. ie:
ln -s ~/Files/Scripts/Main ~/myFold
After that you can perform operations to ~/myFold
, such as:
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.
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.
IHTH
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...
$ pushd ~/my/aliased/dir
...you can then type dirs -v
to see its numeric position in the stack:
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 Tab to show the immediate subdirectories of the "aliased" directory:
$ cd ~0/<Tab>
child_dir1 child_dir2
Put the following line in your myscript
set myFold = '~/Files/Scripts/Main'
In the terminal use
source myscript
cd $myFold
참고URL : https://stackoverflow.com/questions/17958567/how-to-make-an-alias-for-a-long-path
'Program Tip' 카테고리의 다른 글
반환이있는 Switch 문 — 코드 정확성 (0) | 2020.10.11 |
---|---|
activerecord의 하위 쿼리 (0) | 2020.10.11 |
Swift 4.0으로 컴파일 된 모듈은 Swift 4.0.1에서 가져올 수 없습니다. (0) | 2020.10.11 |
Visual Studio에 포함 된 아이콘은 어디에 있습니까? (0) | 2020.10.11 |
프로그래밍 방식으로 버튼 위젯에 포커스를 설정하는 방법은 무엇입니까? (0) | 2020.10.11 |