How to make aliases for file paths?

6,638

Solution 1

Frankly, this is a job for variables:

pandaPictures=~/Documents/SectionA/Sub-folder65/SectionF-2
cp "$pandaPictures"/pic1.png ~/Pictures
cd "$pandaPictures"
cd "$pandaPictures"/Specials

Or:

safePandaImages=PandaImages/2016/Sector7
cp ~/Documents/Images/"$safePandaImages"/panda.jpg ~/Pictures
cd ~/Pictures/Photos/Images/Wallpaper/"$safePandaImages"

Bash isn't smart enough to complete with the contents of variables, but zsh can:

$ foo=/usr   
$ cp $foo/  # press Tab
bin/      include/  lib/      lib32/    lib64@    local/    sbin@     share/    src/

With bash, you can first get it to expand every variable, alias, command substitution, etc., using CtrlAltE and then use tab completion:

$ foo=/usr
$ cp $foo # press Ctrl-Alt-E
$ cp /usr

All that said, if you're doing this regularly, script it. Make a script. Add it to crontab. Forget it.

Solution 2

Variables are probably the best solution, but you could also create symlinks that would accomplish the same thing, at least for the first case. The benefit is easy autocompletion.

ln -s ~/Documents/SectionA/Sub-folder65/SectionF-2 ~/pandaPictures

cp ~/pandaPictures/pic1.png ~/Pictures
cd ~/pandaPictures 
cd ~/pandaPictures/Specials
Share:
6,638

Related videos on Youtube

muru
Author by

muru

Updated on September 18, 2022

Comments

  • muru
    muru over 1 year

    I have many folders on my machine, many of those folders have more folders within them, more folders within those folders, and so on... Now when I am doing certain cd and cp operations that I do every day as part of a backup process or something it becomes rather annoying having to type in all the file path to a file or location, even with the TAB auto-correct feature.

    So what I think would be really useful is if I could make an alias, like for a command, but instead for a file path. Is this possible?

    Here are some examples of how I would like it to work:

    • I would like to be able to assign ~/Documents/SectionA/Sub-folder65/SectionF-2 to equal an alias such as pandaPictures so that I can use it to do the following sorts of things:

      cp pandaPictures/pic1.png ~/Pictures
      cd pandaPictures
      cd pandaPicture/Specials
      

      And it would also be nice if TAB auto-complete would work with the file paths when they include these file path aliases too.

    • I would also like to be able to assign PandaImages/2016/Sector7 to an alias such as safePandaImages so that I can use it to do the following sorts of things:

      cp ~/Documents/Images/safePandaImages/panda.jpg ~/Pictures
      cd ~/Pictures/Photos/Images/Wallpaper/safePandaImages
      

    In the examples I give above, the second section is not an absolute must (where the alias is not just at the beginning, but also in the middle and end) if it is not possible or very impracticable to do. Though if it is possible, I don't mind it not quite looking like the above (for instance if you have to have []s around the alias or something so that it is clear that it is not just a regular file path).

    I am running Ubuntu GNOME 15.10 with GNOME 3.18.

    • kos
      kos about 8 years
      For the first one even a simple symbolic link would do: ln -s ~/Documents/SectionA/Sub-folder65/SectionF-2 ~/pandaPictures.
  • wjandrea
    wjandrea about 8 years
    Put the variable definitions in the bashrc and they will be accessible from any terminal.