DEV Community

Cover image for Aliases - Using Linux Just Got Easier
Ayush Agarwal
Ayush Agarwal

Posted on • Updated on • Originally published at blogs.ayushdev.com

Aliases - Using Linux Just Got Easier

It is a lovely day, you are sitting in front of your laptop on your work desk writing some excellent code, and your terminal is full of some scripts or commands.

Now you realize I need to navigate to a directory inside another directory that is inside another. The chain continues for about 4-5 directories until you reach the required folder.

Such a mood spoiler, right! This potential move has the tendency to force you to get up from your desk and take a 5-minute break which can very quickly turn into a long break.

See, the damaging domino effect it can have? Now you will argue, ah! You don't know, I can keep pressing the "up" arrow and find the command I need because I used it just about a few hours ago.

But is that the most efficient way or the best we can do?

Of course not, and this is where the magic of Aliases come in.

To understand things better, let's assume you have to go to a folder named "test," inside 5 folders A,B,C,D, and E.

Most people will do either of the below two:

cd A/B/C/D/E/test

or

cd A

cd B

cd C

cd D

cd E

cd test

Now let's do the same task using Aliases. (The reason for all the build-up until now,)

In simple terms, Aliases are one-word replacements of those types of long commands you see above.

Aliases in Action :

  1. Create a ".bashrc" file. Now let us create an alias for the task we did above. In the file, write :

alias test="cd /users/$USER/A/B/C/D/E/test"

  1. $USER will be replaced by your system username.

  2. Now, to have this working, you will have to run this .bashrc file every time the system starts. Sounds redundant, right?

  3. There is a simple solution to it. You must have a file named ".zshenvuser." This is the file that runs every time your system starts.

  4. In that file, just put:

    ~/.bashrc

This will ensure the .bashrc file runs on every system too.

  1. If you don't have the ".zshenvuser" file, create a ".bash_profile" and put the below code in it:

if [ -r ~/.bashrc ];
then
source ~/.bashrc

  1. These are the two ways to automate the running of bashrc. If you know of any other, please comment below.

Now, after all, this, what have we achieved.

Go to your Linux terminal and just type "test." It will take you to the test directory exactly what

cd A/B/C/D/E/test

does.

Similarly, you can create different aliases for all the commands you use frequently and make your coding session more productive.

To create more alias, just add another line with the same name alias="."

That's it for this tech Friday blog. I would love your feedback, even negative ones.

Comment below if you feel I have missed something. After we are all learning.

Discussion (2)

Collapse
danhmanh profile image
Danh Manh Nguyen

I used to have a lot of alliases to switch between these project history directories. But when I got autojump, things has changed. You can check it out to easily switch your directories without adding alliases.

github.com/wting/autojump

Collapse
ayushdev_24 profile image
Ayush Agarwal Author

Wow, this looks interesting. Thanks for sharing it, will check it out. Also thanks a lot for reading out my blog. :)