3

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.

1
  • For the first one even a simple symbolic link would do: ln -s ~/Documents/SectionA/Sub-folder65/SectionF-2 ~/pandaPictures. – kos Mar 28 '16 at 20:44
7

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.

1
  • 3
    Put the variable definitions in the bashrc and they will be accessible from any terminal. – wjandrea Mar 28 '16 at 20:41
6

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy