Useful Bash aliases

By | 2016-06-24

An alias is nothing but the shortcut to command or group of commands. It allows to use options and filenames. You can add your own user-defined aliases to ~/.bashrc file. You can cut down typing time with these aliases, work smartly, and increase productivity at the command prompt.

More about aliases

Use alias command without any parameter or option to display the list of all defined aliases.

user@server:~$ alias
alias ls='ls --color=auto'

To create the alias use the following syntax:

alias name=value
alias name='command'
alias name='command arg1 arg2'
alias name='/path/to/script'
alias name='/path/to/script.pl arg1'

For example to create alias for clear command just enter:

user@server:~$ alias c=clear

After this command you can clear the screen entering c then press ENTER.

You can able to temporary disable the alias using \ character. For example to disable the ‘c’ alias:

user@server:~$ \c
-bash: c: command not found

You can remove or undefine the alias with command unalias.

user@server:~$ unalias c
user@server:~$ c
-bash: c: command not found

If you define the alias in terminal then it remains active until the end of session. If you want it make permanent you should edit your ~/.bashrc file with your favorite text editor. For example with vi.

You can use the alias command similar to any other commands. You can use it to shorten the privileged command access.

# if user is not root, pass all commands via sudo
if [ $UID -ne 0 ]; then
    alias reboot='sudo reboot'
    alias update='sudo apt-get upgrade'
fi

You can use same .bashrc file to different systems easily:

# Get os name via uname
myos="$(uname)"
 
# add alias as per os using $_myos variable
case $myos in
   Linux) alias foo='/path/to/linux/bin/foo';;
   FreeBSD|OpenBSD) alias foo='/path/to/bsd/bin/foo' ;;
   SunOS) alias foo='/path/to/sunos/bin/foo' ;;
   *) ;;
esac

 

Useful shortenings

Aliases for ls

Colorize the ls output

alias ls='ls --color=auto'

Use a long listing format

alias ll='ls -la'

Show hidden files

alias l.='ls -d .* --color=auto'

Aliases to changing directories

get rid of command not found

alias cd..='cd ..'

a quick way to get out of current directory

alias ..='cd ..'
alias ...='cd ../../../'
alias ....='cd ../../../../'
alias .....='cd ../../../../'
alias .4='cd ../../../../'
alias .5='cd ../../../../..'

 

 

More info: Cyberciti