How to make aliases for file paths?
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-2to equal an alias such aspandaPicturesso that I can use it to do the following sorts of things:cp pandaPictures/pic1.png ~/Pictures cd pandaPictures cd pandaPicture/SpecialsAnd 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/Sector7to an alias such assafePandaImagesso 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.
12 Answers
Frankly, this is a job for variables:
pandaPictures=~/Documents/SectionA/Sub-folder65/SectionF-2
cp "$pandaPictures"/pic1.png ~/Pictures
cd "$pandaPictures"
cd "$pandaPictures"/SpecialsOr:
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 /usrAll that said, if you're doing this regularly, script it. Make a script. Add it to crontab. Forget it.
1Variables 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 ~/pandaPicturescp ~/pandaPictures/pic1.png ~/Pictures
cd ~/pandaPictures
cd ~/pandaPictures/Specials