Ayush Agarwal
Ayush Agarwal's Blog

Ayush Agarwal's Blog

Aliases - Using Linux made easier

Aliases - Using Linux made easier
Ayush Agarwal's photo
Ayush Agarwal

Published on Nov 19, 2021

3 min read

Subscribe to my newsletter and never miss my upcoming articles

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"

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

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

  4. 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.

  5. In that file, just put: ~/.bashrc This will ensure the .bashrc file runs on every system too.

  6. 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

  7. 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.

 
Share this