How to make an “alias” for a long path?

前端 未结 10 1931
广开言路
广开言路 2020-12-22 17:58

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
         


        
相关标签:
10条回答
  • 2020-12-22 18:15

    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
    
    0 讨论(0)
  • 2020-12-22 18:15

    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
    
    0 讨论(0)
  • 2020-12-22 18:17

    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.

    0 讨论(0)
  • 2020-12-22 18:17

    Put the following line in your myscript

    set myFold = '~/Files/Scripts/Main'
    

    In the terminal use

    source myscript
    cd $myFold
    
    0 讨论(0)
  • 2020-12-22 18:21

    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
    
    0 讨论(0)
  • 2020-12-22 18:24

    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$
    
    0 讨论(0)
提交回复
热议问题