SEARCH

How-To Geek

How to Use Aliases to Customize Ubuntu Commands

Using the command line seems rugged and unpleasant, but Linux has a way to ease things up and help you get things done with the command line by allowing you to use aliases to customize how you type commands.

Setting up Aliases

Aliases are a way for you to customize the commands by giving them aliases (nicknames). You can use them to remember hard commands or make short names to long commands you just hate to type. To setup aliases, right-click and create an empty file in your home directory and name it “.bash_aliases”. Notice the period at the beginning of the name that will make the file hidden. Press “Ctrl+H” to show the hidden files.

Aliases Syntax

Using your favorite text editor open the file you just created and start making your aliases. But there are few things you should keep in mind, the syntax should be:

alias new_name='old_command'

Where “new_name” is the alias, and “old_command” is the command you want to change and is put between quotes. Example:

alias agi='sudo apt-get install'

This will make typing “agi” the same as typing “sudo apt-get install”. This way if you have a dozen of packages to install, your task just got easier. Keep in mind that if you create an alias that looks the same as a command, then the command or alias will not work. Example:

alias install='sudo apt-get install'

The alias in the above example won’t work because there is already a command by the name “install”.

And remember that creating aliases from two words won’t work unless you connect them with a dash. Example:

alias apt install='sudo apt-get install'

alias apt-install='sudo apt-get install'

In the above example, the first alias is invalid as the alias consists of two separate words while the second alias is good to use because the two words are connected with a dash. And the last thing that you shouldn’t do is put any space at the starting of any line. So that’s everything about creating the aliases, but what aliases would you use? Keep reading!

What aliases to use

Now that you know how to setup aliases and create your own. Let’s see what aliases you can use to make the most out of it.

  • Manage packages

If you have to install and remove packages too often then you really are going to like this.

alias agi='sudo apt-get install'

alias agr='sudo apt-get remove'

alias agu='sudo apt-get update'

alias acs='apt-cache search'

The aliases above are made of the first letters of each word in the command. These are just examples of what you can use and you can use them, modify them or create your own.

  • Manage files and folders

alias cp='cp -iv'

alias mv='mv -iv'

alias rm='rm -i'

alias la='ls -alh'

These aliases will make the command line ask you for confirmation on deleting files or overwriting them (if there were duplicates) when copying or moving files as well as give you more information on what is being done. This can prevent you from accidentally overwriting your files or sending them to somewhere you shouldn’t send them to.

  • Navigating the system

alias documents='cd ~/Documents'

alias downloads='cd ~/Downloads'

alias desktop='cd ~/Desktop'

alias music='cd ~/Music'

alias videos='cd ~/Videos'

alias ..='cd ..'

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

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

Now navigating your files and folders can be no easier. Type the directory you want to go to and type dots to go up.

  • Other useful aliases

alias e='exit'

alias s='sudo'

alias shutdown='sudo shutdown –h now'    #requires root password, disable it by "sudo chmod u+s /sbin/shutdown"

alias restart='sudo shutdown –r now'      #requires root password, disable it by "sudo chmod u+s /sbin/shutdown"

alias suspend='sudo pm-suspend'

alias lock='gnome-screensaver-command --lock'

alias mounted='mount | column –t

 

Got any more tips or great aliases? Share them in the comments.

 

 

Omar Hafiz is a geeky Linux user who loves customizing Ubuntu to fit his personal style. Whether it's the login screen or his Gnome panels, he's got them tweaked to perfection.

  • Published 09/19/11

Comments (10)

  1. Sesses

    I’m forever switching between Windows and Linux machines so I find it useful (lazy) to set aliases for the common commands of the other system (ls and dir, etc).

  2. Magnun

    A nifty one that I use a lot is:
    alias grep=”grep –color”

  3. Detron

    alias dirtree=”ls -R | grep “:$” | sed -e ‘s/:$//’ -e ‘s/[^-][^\/]*\//–/g’ -e ‘s/^/ /’ -e ‘s/-/|/’”

    give you a directory tree

    alias fixmbr=’sudo /usr/sbin/grub-install –recheck –no-floppy /dev/sda’

    if grub goes missing and i have to use a boot cd to launch linux, i run this to fix grub

  4. Omar Hafiz

    @Derton

    You definitely need an alias to remember the directory tree command. Thanks for the fixmbr command.

  5. anonBrian

    >The alias in the above example won’t work because there is already a command by the name “install”.

    That’s not true, you can call the alias anything you want, just because it’s not a good idea doesn’t mean you cannot do it. For example try this:

    alias apt-get=’echo “I wont manange your software any more”‘

  6. kjell

    @Detron
    You might want to just install tree.

  7. Blake

    These are a few of mine.

    alias clean=’sudo apt-get clean && sudo apt-get autoclean && sudo apt-get dist-clean’

    alias update=’sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade’

    alias ls=’ls –color=auto’

    alias pyftp=’python -m SimpleHTTPServer 8080′ (makes a ftp in your current directory on port 8080)

  8. Omar Hafiz

    @anonBrian

    If you call an alias the same name as a another existing command then the command won’t work.

  9. Detron

    kjell,

    I do have tree installed, but the reason I still have this in my .bashrc, is I can easily put my .bashrc onto any linux computer and use my alias, but if it has no internet, or I do not has sufficient rights to install it, my alias will work.

    but thanks for the comment, I would hate to know I had missed out on a good application like tree had I not known.

    my .bashrc is in my dropbox, my ubuntuOne, on a CD, on my thumb drive, and I have it on a floppy, sometimes you just never know what you will need

  10. Ridgeland

    # was: eval “`dircolors -b`”
    eval “`dircolors -b /Data/Linux/SystemFiles/Bash/ls_colors`”
    # my preferences:
    alias ls=’ls -FG –color=auto –format=long’
    alias df=’df -B1M’
    alias his=’history | grep ‘
    alias md5sumcd=’md5sum /dev/scd0′
    alias nano=’nano -m’

    $ his ssh -> filter bash history for just the ssh commands used
    ls gives me colors that don’t hurt my eyes, but that’s not an alias issue

Enter Your Email Here to Get Access for Free:

Go check your email!