DEV Community

Cover image for How the alias Command works on Linux
Johnny Simpson
Johnny Simpson

Posted on • Originally published at fjolt.com

How the alias Command works on Linux

We've covered a lot of linux commands, and it's sometimes necessary to write the same command over and over again. On Linux and other Unix like systems, we can use the alias command to avoid the drudgery of typing the same thing over and over again. It lets us make custom commands that will run a specific command of our choosing.

The alias command has no options, so it can simply be written as shown:

alias NAME="STRING"
Enter fullscreen mode Exit fullscreen mode

How the alias Command works on Linux

Let's say we are constantly using the cd command to move into our documents folder. Instead of writing cd ~/Documents every time, we can define a new command which runs cd ~/Documents. I'm going to call it gtd (go to documents):

alias gtd="cd ~/Documents"
Enter fullscreen mode Exit fullscreen mode

Now all we have to type into our terminal to go to our documents folder is gtd, and it'll run cd ~/Documents.

You can literally create any custom command with alias. Here is another example where we create a command mtf, which moves my-file.txt in the current folder to ./test/my-file.txt:

alias mtf="mv my-file.txt ./test/my-new-file.txt"
Enter fullscreen mode Exit fullscreen mode

Alias only lasts for the session

The only thing to note about alias is that the commands created are not permanent. If you close the terminal window, you'll lose them - so they provide a nice efficiency boost for a session, but need to be redefined if you want to create them again. This is so you don't end up with many unused commands sitting around, and taking up name spaces.

unalias Command

If you think you've made a mistake, and want to remove an alias command, then just use unalias. For example, to remove our mtf command, we can run the following:

unalias mtf
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
jcubic profile image
Jakub T. Jankiewicz • Edited

It's kind of "How to use alias command" I was expecting internals of Bash by this title.

It would also be nice to see that alias is not the same type of command as find. alias is builtin bash command and find is executable program.

Collapse
 
smpnjn profile image
Johnny Simpson

Maybe I'll write that one could be cool.