Unix Bash Alias Tutorial – Handle Alias Command Like Jennifer Garner

by Sasikala on April 14, 2010

Photo Courtesy: pchow98

While most of us cannot kick someone ass like Jennifer Garner in Alias, we can at least try to use the Linux alias command effectively.

An alias command is simple string substitution of one text for another, when it is used as the first word of a simple command.

In this article let us review how to set / unset bash aliases permanently and temporarily. Let us also review some useful bash alias examples that you can use in your bash startup files.

This article is part of the on-going bash tutorial series. Refer to our earlier tutorial about bash introduction and bash exit status.

How to Set an Alias

Aliases can be defined on the command line, in .bash_profile, or in .bashrc, using the following syntax:

$ alias name='unix command with options'
  • alias – is a shell built-in
  • name – any user-defined simple name for the alias.
  • command – any unix command, which could be with the options.

This means that name is an alias for command. Whenever name is typed as a command, bash will substitute the corresponding command along with the options in its place.

Note: There are no spaces on either side of the equal sign. Quotes around command are necessary if the string being aliased consists of more than one word.

Executing this command in command line makes this as temporarily alias. That is, this alias is available until you exit the shell. Storing the alias in bash startup files makes it as permanent alias.

The following aliases might be useful. You can set these aliases in ~/.bashrc file.

Most Common Alias Examples

Following aliases are ready for you to use straight away. What is your favorite alias?

Open last modified file in vim

alias Vim="vim `ls -t | head -1`"

Find top 5 big files

alias findbig="find . -type f -exec ls -s {} \; | sort -n -r | head -5"

Grep for a bash process

alias psg="ps -aux ¦ grep bash"

List including hidden files with indicator and color

alias ls='ls -aF --color=always'

List in long format

alias ll='ls -l'

To clear all the history and screen

alias hcl='history -c; clear'

Make basic commands interactive, and verbose

alias cp="cp -iv"      # interactive, verbose
alias rm="rm -i"      # interactive
alias mv="mv -iv"       # interactive, verbose
alias grep="grep -i"  # ignore case

Easy to use aliases for frequently used commands

alias x="exit"

Clear the screen and list file

alias cls='clear;ls'

Filesystem diskspace usage

alias dus='df -h'

To navigate to the different directories

alias ..='cd ..'
alias ...='cd ../..'

Refer this article for more cd command aliases.

Alias examples that should be modified for your environment

The alias examples provided in this section should be modified according to your environment before using them.

Remove the firefox lock

alias rm_fire_lock='/bin/rm .mozilla/firefox/NAME.default/.parentlock' # edit NAME

To login to the remote machine through ssh with loginname

alias server_name='ssh 192.168.1.1 -l tom' # change the ip & user name
alias ser2='ssh www.dbserver.com -l kgf' # create as many alias as required.

To login to remote cvs server

export CVS_RSH=/usr/local/bin/ssh
alias cvl='cvs -d :ext:username@cvs.server.com:/usr/local/cvsroot'  # change required.

Unmounting the cdrom

alias umnt='umount /mnt/cdrom'  # cdrom / thumb drive.

How to view all the aliases

Execute alias without arguments to view list of aliases set in a shell.

$ alias
alias ..='cd ..'
alias ...='cd ../..'
alias mnt='mount /mnt/cdrom'
alias umnt='umount /mnt/cdrom'
alias dus='df -h'

To view a particular alias, enter the command the format “alias aliasname” as shown below.

$ alias dus
alias dus='df -h'

How to temporarily stop using aliases

When you want to call the command instead of the alias, then you have to escape it and call.

$ \aliasname

For Example, alias cp=”cp -iv”, will ask you confirmation if you are about to overwrite a file. This can be annoying when you are copying lot of files that you already know you are going to overwrite. Probably you might want to temporarily use the regular cp command instead of the cp-alias.

So, if an alias cp exists, but you want to use the cp-command instead, escape the alias temporarily as shown below:

\cp * /backup/files/

How to remove an alias

unalias is a shell built-in to remove an alias. To remove a particular alias:

$ unalias hcl
	where unalias is a shell built-in.
	mnt is an alias name.
$ hcl
-bash: hcl: command not found

How to remove all aliases

unalias with -a option, removes all the aliases.

$ unalias -a
$ alias

NOTE: Shell functions are faster. Aliases are looked up after functions and thus resolving is slower. While aliases are easier to understand, shell functions are preferred over aliases for almost every purpose. You should be very careful replacing a standard command with an alias or a function.

The aliases shown in this article are available in this sample bash aliases files, which you can copy/paste and use it on your bash startup file.

Aliases are very useful things, but I hope that you will find bash functions at least as interesting and even more useful which we will discuss in our next bash article.


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 16 comments… read them below or add one }

1 Chris F.A. Johnson April 14, 2010 at 2:36 am

From the bash man page:

“For almost every purpose, aliases are superseded by shell functions.”

2 Anurag Rana April 14, 2010 at 8:14 am

it’s nice…

one query..
“is it true that all unix commands run in linux but all linux commands not in unix”

thanxs

3 SamD April 14, 2010 at 6:04 pm

Nice article. One small point — in the example for unalias hcl: the second line under that should say “hcl is …” vice “mnt is …”.

4 Harry April 14, 2010 at 7:35 pm

Anurag Rana: No thats not true always most command do run on linux, like for instance prstat on solaris is similar to top on linux, different command but similar output

shareall is another command for sharing nfs in solaris world in unix we have exportfs in linux world

many more examples…

This is where alias play a vital role if you are used to a command you can always substitute it

5 BalaC April 15, 2010 at 4:18 am

You can also add the alias to your bashrc file to make the change work everytime you login via SSH

6 Shantanu Oak April 17, 2010 at 4:42 am

After adding the command to bashrc you need to either restart or start another instance. I could not use the command immediately after adding it to .bashrc file.

vi ~/.bashrc
alias mysql_new=’mysql -uroot -p –prompt=”(\r:\m)\_mysql>” –show-warnings –tee=”/home/sqltee.txt”‘

I use the above alias to start mysql from command prompt and found it useful enough to share.

7 Priya April 20, 2010 at 3:22 am

How to use alias to remove the “crontab -r” command to “crontab -l “

8 NCM May 5, 2010 at 2:42 am

@Priya: If you are intending to avoid accidental deletion, you may do the following:
alias crontab=’crontab -i’
This will prompt you before deleting :)

9 RAGHU SUBRAMANIAM June 2, 2010 at 2:28 am

@Shantanu – after making the changes in .bashrc, if you want to use the alias immediately you can source the file.
source ~/.bashrc
or
. ~/bashrc
This will make the changes available to the current shell itself.

10 Chris F.A. Johnson June 2, 2010 at 2:32 am

“is it true that all unix commands run in linux but all linux commands not in unix”

What do you mean by “Unix”?

Most commands written for one *nix system can be compiled for any *nix system.

11 gotham February 2, 2012 at 5:54 am

well guys let me how to add alias permanently ?????????

12 RAGHU February 2, 2012 at 11:23 pm

@gotham – put the alias in .bashrc file and relogin.

13 Cair February 22, 2013 at 3:55 pm

I’m trying to set up an email on UNIX. I can’t seem to create an alias for a group of names on it can anyone help me out?

14 Jim W from Merritt Island May 1, 2013 at 11:47 am

@Cair – you can add as many email addresses (or userIDs if they exist on the same server) in a file called $HOME/.forward.

Then, whenever userID that owns the .forward file in their home directory gets an email, it will automatically be forwarded to every email address listed in the .forward file.

15 DjebbZ July 12, 2013 at 2:43 am

Thanks Mr. TGS. I have a question : how to include subcommands in alias ? I need to do :
alias myAlias=”command options $(subcommand options)”
but when I type “alias” in the bash, I see
alias myAlias=”command options “.
Seems the whole subcommand has been stripped out. Is there a solution ? Or should I create a function instead ? TIA

16 Faisal Rehman August 18, 2013 at 11:14 pm

Thanks for a lot
This site is very beautiful I realy need it
THANKS

Leave a Comment

Previous post:

Next post: