DEV Community

Cover image for How to create a alias for terminal
Sourav das
Sourav das

Posted on

How to create a alias for terminal

In this post i will show you how to create alias for long commands in terminal.
There is two way to do that

  1. Use alias command in your terminal
  2. Changing the .bashrc file

1. using alias command in terminal

alias files=ls
Enter fullscreen mode Exit fullscreen mode

it will map ls command to files(and the name is completely your personal choice you can use whatever like:-nobita )

but the drawback is you can't use the alias in different terminals you have to use the command in that terminal only

2. Changing the .bashrc file

open your root path of the system and find .bashrc file (remember it uses '.' in prefix so that file may be hidden for you to show the hidden file press ctrl+H in keyboard
then you can show your hidden files)
and write the following command. You can use your own custom alias and by adding alias directly to your .bashrc file you can use the alias in different terminal also

alias pg="ping"
Enter fullscreen mode Exit fullscreen mode

if you use zsh as your default terminal then change .zshrc file

Top comments (1)

djuber profile image
Daniel Uber

Another way to do this (which should be cross-shell portable, generally) is to save the named commands to files, instead of aliases. It's something I do by leveraging a user local ~/bin directory, that I create if it's not there, and stashing all my convenience wrapper commands there, and ensuring that comes first in my path (you'll want to set that for each shell, but once ~/bin is in your path you can use the same wrapper names in any shell).

For your examples, I would consider creating two files ~/bin/pg and ~/bin/files that just call ping and ls (the ls example probably deserves some care to handle arguments correctly).

There are of course upsides and downsides, from an efficiency standpoint the aliases are often better (they live in memory so don't need to get read as input, and don't need to fork a new shell to execute the script) but both possibilities have their strengths. Eventually, if you use the shell much, you'll end up with both local shell aliases and some standalone utility scripts, and develop some sense of which to use in your situation.