Use alias to create shortcuts for commands

crismblog
  12 years ago
  19

The average linux users use the terminal commands for many tasks and some certain commands are very frequently used. For example, if you are a user of Ubuntu, you have to type " sudo apt-get install" every time you want to install a new package or the command "xset dpms force off" to turn off the desktop/laptop monitor. To save time, you can create shortcuts for these commands by adding aliases into the bash configuration file (~/.bashrc or /etc/bash.bashrc). The syntax to use aliases is:

  • alias shortcut-command="regular-command"

For example:

  • alias install="sudo apt-get install"
  • alias monitoroff="xset dpms force off"
  • alias poweroff="sudo shutdown -h now"

After adding these lines into the bash configuration file and log out and log in again, you can use the new aliases. For example, to install new packages, you can use the following command after you used the above aliases:

  • install package1 package2
Comments
roxymarks 9 years ago

In Ubuntu ~/.bashrc checks if ~/.bash_aliases file exists, so it's nice to add aliases to ~/.bash_aliases instead. Or you can just add

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

You don't need to logout and login after changing the aliases, just do (can also use a dot (.) instead of "source", as "." is an alias for "source"):
source ~/.bashrc


jahid_0903014 10 years ago

one command! makes things so simple! great....


russellz 11 years ago

Some suggestions for frequently used commands:
alias cd..='cd ..'
alias cp='cp -i'
alias d='ls'
alias df='df -h -x supermount'
alias du='du -h'
alias egrep='egrep --color'
alias fgrep='fgrep --color'
alias grep='grep --color'
alias l='ls'
alias la='ls -a'
alias ll='ls -l'
alias ls='ls -F --color=auto'
alias lsd='ls -d */'
alias md='mkdir'
alias mv='mv -i'
alias p='cd -'
alias rd='rmdir'
alias rm='rm -i'


IDex 11 years ago

Very good, simple and to the point.


yves910 12 years ago

Thanks, a newbie.