How to use aliases with the bash shell and some other useful Linux tricks.

Posted: December 30, 2012. At: 9:06 PM. This was 5 years ago. Post ID: 5080

Are you wanting to create an alias and you want to see how they are done? Well a Linux distribution should come with some aliases that are already created for you and you can type the alias ls command to see how one is created.

[email protected]:~$ alias ls
alias ls='ls --color=auto'

A good one to have is this one. This alias will force rm into interactive mode. This will always ask before deleting a file.

[email protected]:~$ alias rm='rm -i'
[email protected]:~$ alias rm
alias rm='rm -i'

I am using my Ubuntu 10,10 ISO at the moment to create this blog posting; I have added the ISO to the Grub 2 boot menu with Linux Mint 12 Debian Edition and after to boots it has the hard drive which is /dev/sda1 mounted under the /isodevice directory. This is how you determine how much free space there is on that partition using the command-line.

[email protected]:/isodevice$ df -TH /isodevice/
Filesystem    Type     Size   Used  Avail Use% Mounted on
/dev/sda1      xfs     119G    54G    66G  46% /isodevice

And this is is how you find out much space is taken up on the partition by all of the files therein. Using the du command.

[email protected]:/isodevice$ sudo du -ack -h /isodevice/ | tail -n 2
51G	/isodevice/
51G	total

Here are some useful bash aliases that you can paste into your ~/.bashrc and use right away.

alias dir='ls --format="vertical"'
alias vdir='ls --format="long"'
alias verbosetree='tree -A -s -p -f --dirsfirst'
alias psverbose='ps u a x f g'
alias ll='ls -l | sort -nr'
alias la='ls -A'
alias l='ls -CF'
alias dsize="du -ack | sort -nr | head -n 20"
alias psview="pstree -a -u -G -l -h -p"

If you want to list the aliases that are present on the system that you are using; then use the alias command by itself.

[email protected]:~$ alias 
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

This is the list of aliases I have in my ~/.zshrc file.

# Set up aliases
alias mv='nocorrect mv'       # no spelling correction on mv
alias cp='nocorrect cp'       # no spelling correction on cp
alias mkdir='nocorrect mkdir' # no spelling correction on mkdir
alias j=jobs
alias pu=pushd
alias po=popd
alias d='dirs -v'
alias h=history
alias grep=egrep
alias ll='ls -l'
alias la='ls -a'
alias cls='clear'
alias lu='ls -Fula -h'
# More useful Aliases.
alias tarunpack='tar -zxvf'
alias bz2unpack='tar -jxvf'
alias mc='mc -a'
alias rm='rm -i'

This is how the aliases are implemented in the csh shell. This is different to the zsh and bash shells, but still easy to understand.

alias cls 'clear'
alias ls 'ls -hula --color=auto'
alias tarunpack 'tar -xvf'
alias bz2unpack 'tar -jxvf'

As you can see; the way that they are implemented is very similar indeed. If you want to learn more about how to setup aliases for the csh shell; this website will help you. http://www.decf.berkeley.edu/help/unix/csh/aliases.html. Aliases can make life easier when you are using the bash shell; you can shorten frequently used commands and make your time using the shell more productive and faster. Here is another link that contains even more information about using aliases on the csh and tcsh shell: http://home.adelphi.edu/sbloch/class/archive/271/fall2005/notes/aliases.html. But the bash shell is easier to use these days. tcsh is a good shell to use on FreeBSD as it is far better than the default /bin/sh shell. That is only useful when you have nothing else; ie in single user mode. You would not want to use it otherwise.

No comments have been made. Use this form to start the conversation :)

Leave a Reply