To can create an alias to quickly execute your favorite Linux commands, or multiple piped commands. You can use it to limit the amount of keystrokes that are required for a command that is used often. In this Linux quick tip we will show you how to create an alias, and how to make the alias permanent.
I do a lot of DNS administration and find myself using the dig command quite often. Most times I just need a quick answer to see if a host name is resolving correctly and do not necessarily need to see all the output generated by the dig command.
Here is the normal output of the dig command:
$ dig putorius.net
<<>> DiG 9.9.4-P2-RedHat-9.9.4-12.P2.fc20 <<>> putorius.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 10961
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4000
;; QUESTION SECTION:
;putorius.net. IN A
;; ANSWER SECTION:
putorius.net. 14400 IN A 188.8.131.52
;; Query time: 99 msec
;; SERVER: 192.168.1.1#53(192.168.1.1)
;; WHEN: Sat Jul 19 13:21:03 EDT 2014
;; MSG SIZE rcvd: 57
I often only require the IP address to which a host resolves, so I need to add
+short to my dig query like so:
$ dig +short putorius.net
Creating a Bash Alias
Instead of typing “dig +short” every time, I created an alias called sdig, like so:
[savona@putor ~]$ alias sdig='dig +short' [savona@putor ~]$ [savona@putor ~]$ sdig putorius.net 184.108.40.206
If you create an alias on the command line like above, it will only be available for that shell instance. If you create a new shell instance, or log out of the system, the alias will no longer be available.
Making an Alias Permanent
To create the permanent alias you must add it to your
.bashrc file. Using your favorite text editor, open your
.bashrc file, which is a hidden file (notated by the . in the beginning of the name) in your home directory. I am using vi to edit this file, you can use whatever text editor you like.
Here is an example of the default .bashrc file:
# Source global definitions
if [ -f /etc/bashrc ]; then
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
Add a line telling bash what the alias is at the end of this file. Using my example, I added the following line for the sdig alias.
alias sdig="dig +short"
Another quick way to add an alias is to just echo the line and redirect it to append at the end of the file like so:
$ echo 'alias sdig="dig +short"' >> ~/.bashrc
NOTE: We wrapped the command in single quotes to the shell does not try to interpret any special characters.
Now that you have your alias line added to
.bashrc, you need to have the shell read, or source, the file so it knows the new alias exists. You only have to do this once, all new shells will automatically read the .bashrc file when they are opened.
$ source ~/.bashrc
Now you can use the sdig command instead of typing the command “dig +short”.
$ sdig putorius.net
You can see how this can be a big time saver. Of course you can use this for just about anything you type on the command line so the you can save yourself a lot of typing and make your workflow more efficient.
Using aliases for your favorite commands can really help the efficiency of the command line. I have seen some really complex uses for alias. I would love to hear how you are using aliases in your environment. Please share in the comments.
This site uses Akismet to reduce spam. Learn how your comment data is processed.