Symlinks, alias, time statistics and tac( Common terms 2)

In this tutorial, you will learn the above features of wsl and how to implement them.
Don’t know what is wsl? Check out this page.

Symlinks, also called as symbolic links or soft link and terms are used interchangeably.
You might remember that in the tutorial from installing NeoVIM we had used Symlinks to associate NVIM with NeoVIM such that if you call nvim it actually functions as neovim.
So symlinks associate one file with the other. So when you create a symlink, a file is created with the name you specify. And when you run commands with that name, the file forwards the data to the file it is associated with. That’s why whenever you type nvim it calls neovim. This is permanent, and you can use this across different shells.

Syntax:

ln -s /path/to/actual/file /path/to/symlink 
ln -l /path/to/symlink

Alias

Alias is a replacement command, that is, when you type in the alias name it will replace the text with the assigned text. Imagine you want to go to a certain directory often so instead of typing in the address using the cd command, you can assign an alias which will do the work for you.

Syntax for assigning an alias

alias <alias_nm>=<replacement_text_or_command>

Do not add space between alias name and its replacement text.

A common use-case

A common use-case of alias is when you want to go to a certain directory regularly, but you are too lazy to type in the full name, and it’s annoying, you can assign an alias to the address.

alias cdd="cd /path/to/desired/directory"

After assigning the above command if you run cdd it will change the directory automatically.

Which alias

If you forget what your alias actually runs you can use the following command

alias <alias_nm>

this will show you the alias replacement text( replace <alias_nm> with actual alias name).
In the above case when you run alias cdd, the output will be cdd='cd /path/to/desired/directory'.

To check all the aliases in your terminal you can run the following command

alias

This will show you a list of all the aliases present.

Removing aliases

To remove any alias, use the following command

unalias <alias_nm>

Problem with assigning an alias and the Fix

When ever you assign an alias it is temporary. So when you close the terminal window, it will destroy the alias.
To fix this problem, you can put the alias command in the .configuration file of your terminal.
The following command will demonstrate this for oh my zsh!( for other shell frameworks, try finding it out yourself or contact secuRIT on telegram)

echo "alias <alias_nm>=<text_replacement_or_command" >> ~/.zshrc

Restart your wsl/terminal and try using the alias, it will work.

At first glance, both alias and symbolic link look the same, but there are a few key differences which we will point out here-

Time statistics

Run the following command

time curl https://securit.club &> /dev/null

You will get something like curl https://securit.club &> /dev/null 0.08s user 0.06s system 24% cpu 0.576 total. We will be discussing the user time, system time, total/real time.
The time taken for the program to finish from start is called total/real time( here, 0.576s).
The time spent doing user code and user related task is called user time. This includes running the given code, showing the output using GUI and such( here, 0.08s).
The time spent doing kernel related task are called system time or sys time. Things like allocating memory and accessing the harddrive are done only under kernel supervision, so these come under system time( here, 0.06s). You might have heard about malloc or fwrite/fread in your C programming class, these are executed under kernel supervision and come under sys time.

So you may wonder that, the system time and user time does not add up to real time. The rest is the response time of the securit server. Hence, you can see that in this case, waiting for the response from the server takes up major time.

Understanding time terminology better

We know that there are many programming languages. One of the key difference is that they differ in speed thus taking more user time.
Imagine you have written a game in C and python. The user time to run the game in python will be greater than C in most case. Hence, for such cases, C is more efficient than python. Here, user time might include the interface, processing the information from keyboard, etc. And this will tell you optimize the user/sys time then working on network time( which isn't of much use in this case).
Now take the example of a server like google drive. Here the response time from the server, download time and upload time matters more than user/sys time.
Thus you can know which part of your code you should optimize( the network/response time or the user/sys time.

Tac

This function is of almost no use to you at this particular moment, but you need to know that such function exists as it will help you in the future.
Run the command

l

Now, run the command

l | tac

the pipe | will pass the output of LHS to RHS so the output of l will be the input of tac.
Now you can see that the output of l is printed in the reverse order. And this is what tac does. tac reversers what ever is given to it( usually used for files).

Credits: secuRIT Core :)

Edit this page