Please feel free to email me with any questions at with "blog" as the subject.

Search my blog:

Loading...

Thursday, 16 September 2010

Bash Aliases

Linux users who commonly use the terminal often get bored of typing horribly long command sequences with arguments for routine jobs.

This is where aliases can come in useful.

(Presuming you use Bash and not sh)

With aliases you can set a command such as Pacman -suy to the name update, so that when you type bash$update in a shell it runs pacman -Suy.

There is more than one way to do this. You can just add the alises to the .bashrc file in your home folder and if you wish to do it this way I recommend you do it at the end.

The other way is to use a seperate file.

If you wish to use a seperate file for your aliases like me, then carry on reading. If you want to add them directly to the .bashrc then skip down a few steps.


Firstly you must create an alises file if one isn't already created on your system.

The usual name for the alias file is .bash_alises the "." at the beginning making it a hidden file.

You can call it what you want.

You must then set that aliases file to run in the .bashrc.

To do this edit the .bashrc file in your home folder and near the end add:

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


or if you named your aliases file something different swap .bash_alises for the name of your file.

Some Linux operating systems have this pre-added along with an empty aliases file.


You can now add aliases to your system either in the .bashrc or .bash_alises (or your own) file.
Aliases must always use the correct format :

alias new-command='horribly long command -arguments'

You can also make system wide aliases by adding them to your /etc/bashrc

You can also put a hash [#] after an alias and then type a quick description so you remember what its for.

You can view all aliases in use by typing bash$ alias in a terminal.


For example, some useful aliases pulled from the internet:

# ls
alias ls='ls --color' # Use colors
alias lsd="ls */ -d" # Display only directories
alias l="ls -l" # Long format
alias la="ls -la" # Long format with hidden files

# cd
alias ..="cd .." # Drop up a dir with ..
alias ...="cd ..; cd .."

#cp/mv
#Prompt for overwrite, verbose
alias cp="cp -iv"
alias mv="mv -iv"

# misc
alias fproc="ps aux | grep " # Find running processes
alias cls=clear
alias gop='gnome-open'
alias df="df -h" # Human readable format
alias du="du -h" # Human readable format
alias mkdir="mkdir -v" # Verbose directory creation
alias grep="grep --color" # Highlight search word
alias oo="ooffice"

# chmod shortcuts
alias "x+"="chmod +x"
alias "x-"="chmod -x"
alias "w+"="chmod +w"
alias "w-"="chmod -w"
alias "r+"="chmod +r"
alias "r-"="chmod -r"

alias shutdown='sudo shutdown -h now'


From the arch wiki for pacman:

# Pacman alias examples
alias pacupg='sudo pacman -Syu' # Synchronize with repositories before upgrading packages that are out of date on the local system.
alias pacin='sudo pacman -S' # Install specific package(s) from the repositories
alias pacins='sudo pacman -Up' # Install specific package not from the repositories but from a file
alias pacre='sudo pacman -R' # Remove the specified package(s), retaining its configuration(s) and required dependencies
alias pacrem='sudo pacman -Rns' # Remove the specified package(s), its configuration(s) and unneeded dependencies
alias pacrep='pacman -Si' # Display information about a given package in the repositories
alias pacreps='pacman -Ss' # Search for package(s) in the repositories
alias pacloc='pacman -Qi' # Display information about a given package in the local database
alias paclocs='pacman -Qs' # Search for package(s) in the local database



Here are some popular ones for ubuntu pulled from the forums:


alias agi='sudo apt-get install'
alias agr='sudo apt-get remove'
alias agp='sudo apt-get purge'
alias acs='apt-cache search'
alias agu='sudo apt-get update'
alias agg='sudo apt-get upgrade'


Sadly you cannot use sudo with aliases for example sudo [alias]. However you can do this by adding:
alias sudo='sudo '
to your aliases file.

Of course you can use very diverse names and make some just for fun.

I have seen alias icanhas='sudo apt-get install' several times.