2-Minute Linux Tip: Learn how to use the alias command

Network World | Apr 17, 2018

In today’s 2-minute tip, we’re going to look at the alias command – a tool that can save you a lot of time and trouble and maybe even a few brain cells.

Similar
Hi, this is Sandra Henry-Stocker, author of the “Unix as a Second Language” blog on NetworkWorld.
In today’s 2-minute tip, we’re going to look at the alias command – a tool that can save you a lot of time and trouble and maybe even a few brain cells.
What alias does is allow you to take a command – maybe a very complicated one or one that you use a lot – and give it another name, one that’s easier to remember or just saves you a lot of typing.
To create an alias, all you have to do is type “alias”, a name for the alias, an = sign, and the command you want the alias to represent. One alias that I use all of the time is defined like this:
$ alias c=clear
I clear my screen often and prefer to do it with a single letter. Let’s try it …
$ c
Here’s an alias for looking at my recent logins:
$ alias rec='last | grep $USER | head -3'
$ rec
shs pts/0 192.168.0.15 Wed Apr 4 11:09 still logged in
shs pts/0 192.168.0.15 Wed Apr 4 10:46 - 11:09 (00:22)
shs pts/2 192.168.0.15 Tue Apr 3 10:36 - 10:42 (00:05)
I can list all of my aliases by typing “alias”. Note that some of these are fairly complex.
$ alias
shs@stinkbug:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias c='clear'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias hello='echo "Hi, nice to see you, $USER"'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
Here’s an alias for looking at the last few times you’ve logged in:
alias rec='last | grep $USER | head -3'
At any time in a terminal window, you can create an alias and then use it. But, if you want an alias to be available every time you log in, you need to add it to your .profile or .bashrc file.
That’s your 2-minute Linux tip for today. If you liked this video, please hit the like and share buttons. For more Linux tips, be sure to follow us on Facebook, YouTube and NetworkWorld.com.
Popular