Thursday, January 19, 2012

Unix Bash Alias Tutorial


An alias command is simple string substitution of one text for another, when it is used as the first word of a simple command.
In this article let us review how to set / unset bash aliases permanently and temporarily. Let us also review some useful bash alias examples that you can use in your bash startup files.

How to Set an Alias

Aliases can be defined on the command line, in .bash_profile, or in .bashrc, using the following syntax:
$ alias name='unix command with options'
  • alias – is a shell built-in
  • name – any user-defined simple name for the alias.
  • command – any unix command, which could be with the options.
This means that name is an alias for command. Whenever name is typed as a command, bash will substitute the corresponding command along with the options in its place.
Note: There are no spaces on either side of the equal sign. Quotes around command are necessary if the string being aliased consists of more than one word.
Executing this command in command line makes this as temporarily alias. That is, this alias is available until you exit the shell. Storing the alias in bash startup files makes it as permanent alias.
The following aliases might be useful. You can set these aliases in ~/.bashrc file.

Most Common Alias Examples

Following aliases are ready for you to use straight away. What is your favorite alias?

Open last modified file in vim

alias Vim="vim `ls -t | head -1`"

Find top 5 big files

alias findbig="find . -type f -exec ls -s {} \; | sort -n -r | head -5"

Grep for a bash process

alias psg="ps -aux ¦ grep bash"

List including hidden files with indicator and color

alias ls='ls -aF --color=always'

List in long format

alias ll='ls -l'

To clear all the history and screen

alias hcl='history -c; clear'

Make basic commands interactive, and verbose

alias cp="cp -iv"      # interactive, verbose
alias rm="rm -i"      # interactive
alias mv="mv -iv"       # interactive, verbose
alias grep="grep -i"  # ignore case

Easy to use aliases for frequently used commands

alias x="exit"

Clear the screen and list file

alias cls='clear;ls'

Filesystem diskspace usage

alias dus='df -h'

To navigate to the different directories

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

Alias examples that should be modified for your environment

The alias examples provided in this section should be modified according to your environment before using them.

Remove the firefox lock

alias rm_fire_lock='/bin/rm .mozilla/firefox/NAME.default/.parentlock' # edit NAME

To login to the remote machine through ssh with loginname

alias server_name='ssh 192.168.1.1 -l tom' # change the ip & user name
alias ser2='ssh www.dbserver.com -l kgf' # create as many alias as required.

To login to remote cvs server

export CVS_RSH=/usr/local/bin/ssh
alias cvl='cvs -d :ext:username@cvs.server.com:/usr/local/cvsroot'  # change required.

Unmounting the cdrom

alias umnt='umount /mnt/cdrom'  # cdrom / thumb drive.

How to view all the aliases

Execute alias without arguments to view list of aliases set in a shell.
$ alias
alias ..='cd ..'
alias ...='cd ../..'
alias mnt='mount /mnt/cdrom'
alias umnt='umount /mnt/cdrom'
alias dus='df -h'
To view a particular alias, enter the command the format “alias aliasname” as shown below.
$ alias dus
alias dus='df -h'

How to temporarily stop using aliases

When you want to call the command instead of the alias, then you have to escape it and call.
$ \aliasname
For Example, alias cp=”cp -iv”, will ask you confirmation if you are about to overwrite a file. This can be annoying when you are copying lot of files that you already know you are going to overwrite. Probably you might want to temporarily use the regular cp command instead of the cp-alias.
So, if an alias cp exists, but you want to use the cp-command instead, escape the alias temporarily as shown below:
\cp * /backup/files/

How to remove an alias

unalias is a shell built-in to remove an alias. To remove a particular alias:
$ unalias hcl
 where unalias is a shell built-in.
 mnt is an alias name.
$ hcl
-bash: hcl: command not found

How to remove all aliases

unalias with -a option, removes all the aliases.
$ unalias -a
$ alias
NOTE: Shell functions are faster. Aliases are looked up after functions and thus resolving is slower. While aliases are easier to understand, shell functions are preferred over aliases for almost every purpose. You should be very careful replacing a standard command with an alias or a function.


tree and pstree: Visualize Unix Processes and Directories in Hierarchical Structure


Using the traditional ps command, you have to manually look at the PID and Parent PID to identify the dependencies between the processes. Sometimes it may be helpful to view the Linux processes in hierarchical format showing the child processes under the parent process. pstree command (Process Tree) will display the processes hierarchically.
 
Using the ls command, you typically view the content of a directory structure. Sometimes it may be very helpful to view the directory structure in a hierarchical display. tree command (Directory Tree) will display the directory structures hierarchically.
 
In this article let us review how to view the processes and directories in tree like format with detailed examples
 

I. Process Tree – pstree command

1. Display Process Tree based on PID

Syntax: pstree 
 
In the following example, I gave the process id of my gdm process, which displays all the child process of my gdm process. i.e it displays the xsession, the konsole that is opened, nautilus, ssh and other process that are associated with the gdm process.
$ pstree 5038
gdm---gdm-+-Xorg
`-x-session-manag-+-gnome-panel---{gnome-panel}
|-gnome-settings--+-pulseaudio-+-gconf-helper
|                 |            `-2*[{pulseaudio}]
|                 `-{gnome-settings-}
|-konsole---3*[bash]
|-metacity
|-nautilus---{nautilus}
|-python
|-seahorse-agent
|-ssh-agent
|-trackerd---{trackerd}
`-{x-session-manag}

2. Display Process Tree for an User

Syntax: pstree 
 
The following example indicates that the userid ramesh have su-ed into the machine, and has 3 shell scripts running, and opened a vim editor.
$ pstree ramesh
su---bash-+-3*[sh---sleep]
`-vim---{vim}

3. Display All Processes in Hierarchical Structure

Syntax: pstree
 
pstree without argument will display all the process in hierarchical structure. Please note that only partial output is shown below.
$ pstree | more
init-+-acpid
|-avahi-daemon---avahi-daemon
|-bonobo-activati---{bonobo-activati}
|-cron
|-cupsd
|-gdm---gdm-+-Xorg
|           `-x-session-manag-+-gnome-panel---{gnome-panel}
|                             |-gnome-settings--+-pulseaudio-+-gconf-helper
|                             |                 |            `-2*[{pulseaudio}]
|                             |                 `-{gnome-settings-}
|                             |-konsole---3*[bash]
|                             |-metacity
|                             |-ssh-agent
|                             `-{x-session-manag}
|-getty
|-konsole-+-2*[bash]
|         |-bash---vim
|         `-bash-+-pstree
|                `-vim
|-thinMS
`-udevd

4. Display Command Line Arguments Given to a Process

Syntax: pstree -a
 
Use option -a as shown below, to display the command line arguments associated with a particular process.
$ pstree -a ramesh
su bk
`-bash
|-sh ./test.sh
|   `-sleep 1000
|-sh ./test.sh
|   `-sleep 1000
|-sh ./test.sh
|   `-sleep 1000
`-sh test.sh [1-9]
`-sleep 1000

II. Directory Tree – tree command

1. View Different Directory Structures

  • # tree / | more – Execute this command to browse through the directory structure of the whole OS. Typically it doesn’t make sense to do tree on the root directory structure, unless you are in a learning mode and would like to understand the directory hierarchy of the Linux.
  • # tree $HOME – Use this to cross-verify the home directory structure content.
  • # tree PROJECT-DIR – Checking the directory structure of a project.

2. Changing the output of the tree command.

  • # tree -d will display only the directories. i.e Files will not be displayed.
  • # tree -a will display hidden files along with directories and files.
  • # tree -s will display the file size as shown below. While using this option, it prints out the size of the files along with the file names.
# tree -s
.
|-- [       4096]  Articles
|   `-- [       4096]  Tree
|       `-- [       5489]  article
|-- [       4096]  Compression
|   |-- [       2584]  article
|   `-- [       4223]  article.safe
`-- [       4096]  DiskSpace
|-- [        722]  article
`-- [        530]  old_article
4 directories, 5 files
  • # tree -p will display the permissions along with the files. While using this option, it prints out the permissions of the files along with the file names as shown below.
# tree -p
.
|-- [drwx------]  Articles
|   `-- [drwx------]  Tree
|       `-- [-rw-------]  article
|-- [drwx------]  Compression
|   |-- [-rw-------]  article
|   `-- [-rw-------]  article.safe
`-- [drwx------]  DiskSpace
|-- [-rw-------]  article
`-- [-rw-------]  old_article

4 directories, 5 files

3. Generate HTML output from tree command

You can also redirect the output of the tree command to a html file as shown below using the -H and -o option.
# tree -H . -o output.html
 
Definition of Option -H from the man page:
  • -H baseHREF: Turn on HTML output, including HTTP references. Useful for ftp sites. baseHREF gives the base ftp location when using HTML output. That is, the local directory may be `/local/ftp/pub’, but it must be referenced as `ftp://hostname.organization.domain/pub’

4. Display tree output based on the specified pattern

List the files that match the pattern using option -P as shown below.
Syntax: tree -P PATTERN

$ tree -P *.safe
.
|-- Articles
|   `-- Tree
|-- Compression
|   `-- article.safe
`-- DiskSpace
 
List the files that does not match the pattern using option -I as shown below.
Syntax: tree -I PATTERN

$ tree -I *.safe
.
|-- Articles
|   `-- Tree
|       `-- article
|-- Compression
|   `-- article
|-- DiskSpace
|   |-- article
|   `-- old_article
`-- t.html

4 Ways to Kill a Process – kill, killall, pkill, xkill


Kill command is use to send signal to a process or to kill a process. We typically use kill -SIGNAL PID, where you know the PID of the process.
There are other ways to effectively kill a process — killing a process by name, killing a process by specifying part of the name, killing a process by pointing out the process with cursor etc.,
In this article, let us review 4 ways to kill a process.

1. Kill Command – Kill the process by specifying its PID

All the below kill conventions will send the TERM signal to the specified process. For the signals, either the signal name or signal number can be used. You need to lookup the pid for the process and give it as an argument to kill.
$ kill -TERM pid

$ kill -SIGTERM pid

$ kill -15 pid
Example: Kill the firefox process.
$ ps -ef | grep firefox
1986 ?        Sl     7:22 /usr/lib/firefox-3.5.3/firefox

$ kill -9 1986

2. Killall Command – Kill processes by name

Instead of specifying a process by its PID, you can specify the name of the process. If more than one process runs with that name, all of them will be killed.
Example: Kill all the firefox processes
$ killall -9 firefox

3. Pkill Command – Send signal to the process based on its name

You can send signal to any process by specifying the full name or partial name. So there is no need for you to find out the PID of the process to send the signal.
Example: Send SIGTERM to all the process which has sample in its name.
$ pkill sample

Pkill Example:

Before sending signal, you can verify which are all the process is matching the criteria using “pgrep -l”, which displays the process ID and process name of the matching processes.
In this example, all the processes are designed to log the signal to signal-log, along with its PID.
$ pgrep -l sample
12406 sample-server.p
12425 sample-server.p
12430 sample-garbagec

$ pkill -USR1 sample

$ cat signal-log
Name: ./sample-server.pl Pid: 12406 Signal Received: USR1
Name: ./sample-server.pl Pid: 12425 Signal Received: USR1
Name: ./sample-garbagecollector.pl Pid: 12430 Signal Received: USR1
Note: The part of name which you specify should be in the character within the first 15 character of the process name.

4. Xkill Command – kill a client by X resource

xkill is the simplest way to kill a malfunctioning program. When you want to kill a process, initiate xkill which will offer an cross-hair cursor. Click on the window with left cursor which will kill that process.
$ xkill
Select the window whose client you wish to kill with button 1....
xkill:  killing creator of resource 0x1200003
Note: Actually, xkill instructs XServer to terminate the client.

UNIX / Linux: 7 Practical PS Command Examples for Process Monitoring


1. List Currently Running Processes (ps -ef, ps -aux)

Its a commonly used example with a ps command to list down all the process which are currently running in a machine. The following example shows the options of ps command to get all the processes.
$ ps -ef
root     26551     5  0 Feb10 ?        00:03:41 [pdflush]
root     26570     5  0 Feb10 ?        00:00:20 [pdflush]
root     30344  3382  0 Feb21 ?        00:00:11 sshd: root@pts/14
root     30365 30344  0 Feb21 pts/14   00:00:02 -bash
root     30393  3382  0 Feb21 ?        00:00:10 sshd: root@pts/15
Where:
  • -e to display all the processes.
  • -f to display full format listing.
In case of BSD machines, you can use ‘ps -aux’ will give the details about all the process as shown above.
$ ps -aux

2. List the Process based on the UID and Commands (ps -u, ps -C)

Use -u option to displays the process that belongs to a specific username. When you have multiple username, separate them using a comma. The example below displays all the process that are owned by user wwwrun, or postfix.
$ ps -f -u wwwrun,postfix
UID        PID  PPID  C STIME TTY          TIME CMD
postfix   7457  7435  0 Mar09 ?        00:00:00 qmgr -l -t fifo -u
wwwrun    7495  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
wwwrun    7496  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
wwwrun    7497  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
wwwrun    7498  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
wwwrun    7499  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
wwwrun   10078  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
wwwrun   10082  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
postfix  15677  7435  0 22:23 ?        00:00:00 pickup -l -t fifo -u
Often ps is used with grep like “ps -aux | grep command” to get the list of process with the given command.
But ps command itself has an option to achieve the same. The following example shows that all the processes which has tatad.pl in its command execution.
$ ps -f -C tatad.pl
UID        PID  PPID  C STIME TTY          TIME CMD
root      9576     1  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root      9577  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root      9579  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root      9580  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root      9581  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root      9582  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root     12133  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
Note: We can create aliases for ps command to list processes based on commands, users or groups.

3. List the processes based on PIDs or PPIDs (ps -p, ps –ppid)

Each process will be assigned with the unique Process ID (PID).
When you launch some application, it might fork number of processes and each sub process will have its own PID. So, each process will have its own process id and parent processid.
For all the processes that a process forks will have the same PPID (parent process identifier). The following method is used to get a list of processes with a particular PPID.
$ ps -f --ppid 9576
UID        PID  PPID  C STIME TTY          TIME CMD
root      9577  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root      9579  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root      9580  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root      9581  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root      9582  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root     12133  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
The following example is to list the processes which has given PID.
$ ps -f  -p 25009,7258,2426
UID        PID  PPID  C STIME TTY          TIME CMD
root      2426     4  0 Mar09 ?        00:00:00 [reiserfs/0]
root      7258     1  0 Mar09 ?        00:00:00 /usr/sbin/nscd
postfix  25009  7435  0 00:02 ?        00:00:00 pickup -l -t fifo -u

4. List Processes in a Hierarchy (ps –forest)

The example below display the process Id and commands in a hierarchy. –forest is an argument to ps command which displays ASCII art of process tree. From this tree, we can identify which is the parent process and the child processes it forked in a recursive manner.
$ ps -e -o pid,args --forest
  468  \_ sshd: root@pts/7
  514  |   \_ -bash
17484  \_ sshd: root@pts/11
17513  |   \_ -bash
24004  |       \_ vi ./790310__11117/journal
15513  \_ sshd: root@pts/1
15522  |   \_ -bash
 4280  \_ sshd: root@pts/5
 4302  |   \_ -bash
Note: You can also use tree and pstree command to displays process in a nice tree structure.

5. List elapsed wall time for processes (ps -o pid,etime=)

If you want the get the elapsed time for the processes which are currently running ps command provides etime which provides the elapsed time since the process was started, in the form [[dd-]hh:]mm:ss.
The below command displays the elapsed time for the process IDs 1 (init) and process id 29675.
For example “10-22:13:29″ in the output represents the process init is running for 10days, 22hours,13 minutes and 29seconds. Since init process starts during the system startup, this time will be same as the output of the ‘uptime’ command.
# ps -p 1,29675 -o pid,etime=
  PID
    1 10-22:13:29
29675  1-02:58:46

6. List all threads for a particular process (ps -L)

You can get a list of threads for the processes. When a process hangs, we might need to identify the list of threads running for a particular process as shown below.
$ ps -C java -L -o pid,tid,pcpu,state,nlwp,args
  PID   TID %CPU S NLWP COMMAND
16992 16992  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 16993  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 16994  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 16995  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 16996  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 16997  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 16998  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 16999  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 17000  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 17001  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 17002  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 17003  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 17024  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 15753  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 15754  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
-L option is used to display the list of threads for a process which has the command given. And it also displays nlwp, which represents number of light weight processes. In the above example, a total of 15 java threads are running.

7. Finding memory Leak (ps –sort pmem)

A memory leak, technically, is an ever-increasing usage of memory by an application.
With common desktop applications, this may go unnoticed, because a process typically frees any memory it has used when you close the application.
However, In the client/server model, memory leakage is a serious issue, because applications are expected to be available 24×7. Applications must not continue to increase their memory usage indefinitely, because this can cause serious issues. To monitor such memory leaks, we can use the following commands.
$ ps aux --sort pmem

USER       PID %CPU %MEM   VSZ  RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  1520  508 ?        S     2005   1:27 init
inst  1309  0.0  0.4 344308 33048 ?      S     2005   1:55 agnt (idle)
inst  2919  0.0  0.4 345580 37368 ?      S     2005  20:02 agnt (idle)
inst 24594  0.0  0.4 345068 36960 ?      S     2005  15:45 agnt (idle)
root 27645  0.0 14.4 1231288 1183976 ?   S     2005   3:01 /TaskServer/bin/./wrapper-linux-x86-32
In the above ps command, –sort option outputs the highest %MEM at bottom. Just note down the PID for the highest %MEM usage. Then use ps command to view all the details about this process id, and monitor the change over time. You had to manually repeat ir or put it as a cron to a file.
$ ps ev --pid=27645
PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND
27645 ? S 3:01 0 25 1231262 1183976 14.4 /TaskServer/bin/./wrapper-linux-x86-32

$ ps ev --pid=27645
PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND
27645 ? S 3:01 0 25 1231262 1183976 14.4 /TaskServer/bin/./wrapper-linux-x86-32
Note: In the above output, if RSS (resident set size, in KB) increases over time (so would %MEM), it may indicate a memory leak in the application.