Java Programming Tutorials

Java programming tutorials with many code examples!

Bash command alias

Problem:

Bash alias is a great way to create a simple name for complex commands. Use it to make your life in Bash command line simpler!

Solution:

First, lets use alias command to see what Bash aliases are already defined:

$> alias
alias cljsbuild='lein trampoline cljsbuild '
alias la='ls -A'
alias ll='ls -lAF'

alias command just prints list of all defined aliases. It’s the same as passing -p parameter: alias -p.

Now we can define our own Bash alias for frequently used command, which in our example is listing all files in reverse order by time:

$> alias lt='ls -ltr'

Let’s verify that the alias is defined:

$> alias
alias cljsbuild='lein trampoline cljsbuild '
alias la='ls -A'
alias ll='ls -lAF'
alias lt='ls -ltr'

As you can see it’s the 4th of the aliases. From now on you can run it using the new command: lt.

So, let’s execute it:

$> lt /boot
total 122927
drwxr-xr-x 2 root root    12288 Mar 31  2013 lost+found
-rw-r--r-- 1 root root   178680 Mar 12  2014 memtest86+_multiboot.bin
-rw-r--r-- 1 root root   178176 Mar 12  2014 memtest86+.elf
-rw-r--r-- 1 root root   176500 Mar 12  2014 memtest86+.bin
-rw------- 1 root root  6412672 Apr 10 21:03 vmlinuz-3.16.0-34-generic
-rw------- 1 root root  3502413 Apr 10 21:03 System.map-3.16.0-34-generic
-rw-r--r-- 1 root root   171819 Apr 10 21:03 config-3.16.0-34-generic
-rw-r--r-- 1 root root  1207327 Apr 10 21:03 abi-3.16.0-34-generic
-rw-r--r-- 1 root root 30304372 Apr 14 16:04 initrd.img-3.16.0-34-generic
-rw------- 1 root root  6414272 Apr 14 22:48 vmlinuz-3.16.0-36-generic
-rw------- 1 root root  3502913 Apr 14 22:48 System.map-3.16.0-36-generic
-rw-r--r-- 1 root root   171808 Apr 14 22:48 config-3.16.0-36-generic
-rw-r--r-- 1 root root  1206938 Apr 14 22:48 abi-3.16.0-36-generic
-rw-r--r-- 1 root root 30311331 May  4 08:54 initrd.img-3.16.0-36-generic
-rw------- 1 root root  6416256 May  5 16:46 vmlinuz-3.16.0-37-generic
-rw------- 1 root root  3502913 May  5 16:46 System.map-3.16.0-37-generic
-rw-r--r-- 1 root root   171808 May  5 16:46 config-3.16.0-37-generic
-rw-r--r-- 1 root root  1206938 May  5 16:46 abi-3.16.0-37-generic
-rw-r--r-- 1 root root 30310319 May 11 11:20 initrd.img-3.16.0-37-generic
drwxr-xr-x 5 root root     1024 May 11 11:20 grub

It’s good idea to put frequently used aliases in ~/.bash_aliases file and include from ~/.bashrc like so:

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

This makes them always accessible when you use Bash command line.

Share with the World!