Linux 101: What are aliases and how do you use them?

3 years ago 319

Jack Wallen introduces you to aliases to help make your Linux command life a bit simpler.

linuxhero3.jpg

Image: Jack Wallen

Sometimes a command is either too long to always be typing out or you simply cannot always remember it. Or maybe you have a collection of commands that you frequently run, and constantly typing them isn't the best use of your time. When that's the case, what do you do? You create aliases. 

SEE: The best programming languages to learn--and the worst (TechRepublic Premium)

In Linux, an alias is a feature of bash that makes it possible for you to use shorthand for commands. Say, for instance, you have a bunch of servers you frequently SSH into and out of during the day. You could either configure them in the SSH config file or create an even faster alias. So instead of typing the command ssh jack@192.168.1.142 to gain remote access to your web1 server, you could just type web1. How do you do this? It's all in the .bash_aliases file in your home directory. 

Open that file with the command nano ~/.bash_aliases. In that file, you can add as many aliases as you like in the form of alias NICKNAME='COMMAND' (Where NICKNAME is the shorthand name for the command and COMMAND is the actual command to run). 

SEE: C++ programming language: How it became the foundation for everything, and what's next (free PDF) (TechRepublic)

So, let's stick with our SSH example. 

  1. For that, you'd add alias=web1='ssh jack@192.168.1.142'
  2. Save and close the file. 

Now, here's the trick: You need to close that terminal window before the new alias will take effect. Once you start a new terminal, you can simply type web1 to run the command. 

And that's all there is to creating an alias for a command on Linux. Get really creative with this, and your command-line work in Linux will become much more efficient.

Open Source Weekly Newsletter

You don't want to miss our tips, tutorials, and commentary on the Linux OS and open source applications. Delivered Tuesdays

Sign up today

Also see

Read Entire Article