≡ Menu

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

pstree and tree command
Photo courtesy of Steffe

This article is written by SathiyaMoorthy.
 
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 <PID>

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

 
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

 
This article was written by SathiyaMoorthy, author of numbertotext Vim plugin, which will replace the numbers with the equivalent text inside Vim. The Geek Stuff welcomes your tips and guest articles.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Klaus January 4, 2010, 6:33 am

    Thanks for this article, was very helpful …

  • Srikanth Dhondi September 4, 2012, 8:50 pm

    Thanks! Helpful 🙂

  • andy March 15, 2013, 3:52 am

    hi frens!!! can u plz help me to write a script which acts exactly like pstree..