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
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