Linux Process And Directory Structure Tree Commands

June 24, 2013 | By
| Reply More

Linux provides us with a number of small utilities for system monitoring and logging. These small tools are very helpful for a system administrator. The Unix ‘ls’ and ‘ps’ commands are very useful for listing directories and processes. But if we want the output in tree-like structure, we have tree and pstree commands. This article provides an overview of these two commands that is enough for understanding the commands. For the detailed information about these commands, man pages can be referred to.

Linux tree command

The tree command lists directories and their contents in tree-like format. This command dumps the directory structure on standard output.

To view directory structure of a directory in tree format, use tree command as

tree

By default, if no argument is provided to tree command, it prints tree of current directory.

# tree
.
|-- Desktop
|-- Documents
|-- Downloads
|-- Music
|-- nltk_data
| `-- corpora
| |-- stopwords
| | |-- danish
| | |-- dutch
| | |-- english
| | |-- finnish
| | |-- french
| | |-- german
| | |-- hungarian
| | |-- italian
| | |-- norwegian
| | |-- portuguese
| | |-- README
| | |-- russian
| | |-- spanish
| | |-- swedish
| | `-- turkish
| `-- stopwords.zip
|-- Pictures
|-- Public
|-- Templates
`-- Videos

11 directories, 16 files

The -d option prints only directories:

# tree -d /boot/
/boot/
`-- grub
|-- fonts
|-- i386-pc
`-- locale

4 directories

If the LS_COLORS environment variable is set, the output is colorized.

The -s option shows file size as well.

$ tree -s Python/
Python/
|-- [ 174] 001.helloworld.py
|-- [ 182] file2.txt
|-- [ 191226] icon.ico
|-- [ 128412] icon.png
|-- [ 428] python_textbox.py
|-- [ 0] python_textbox.py~
|-- [ 251] test_file_io.py
|-- [ 351] wxpython.py
`-- [ 0] wxpython.py~

0 directories, 9 files

The permissions of the files can be displayed along with filenames with -p option.

$ tree -p Python/
Python/
|-- [-rwxr--r--] 001.helloworld.py
|-- [-rw-r--r--] file2.txt
|-- [-rw-r--r--] icon.ico
|-- [-rw-r--r--] icon.png
|-- [-rwxr--r--] python_textbox.py
|-- [-rw-r--r--] python_textbox.py~
|-- [-rw-r--r--] test_file_io.py
|-- [-rwxr--r--] wxpython.py
`-- [-rw-r--r--] wxpython.py~

0 directories, 9 files

Linux pstree command

The pstree command also generates the output in tree format, but for processes instead of directories. In other words, pstree shows running processes as tree. The pstree command generated following output on my running system:

$ pstree
init─┬─NetworkManager─┬─dhclient
│ ├─dnsmasq
│ └─2*[{NetworkManager}]
├─SystemToolsBack
├─acpid
├─at-spi-bus-laun─┬─dbus-daemon
│ └─3*[{at-spi-bus-laun}]
├─at-spi2-registr───{at-spi2-registr}
├─atd
├─avahi-daemon───avahi-daemon
├─bluetoothd
├─clock-applet───{clock-applet}
├─console-kit-dae───64*[{console-kit-dae}]
├─cron
├─cupsd
├─2*[dbus-daemon]
├─dbus-launch
├─dconf-service───2*[{dconf-service}]
├─firefox─┬─plugin-containe───5*[{plugin-containe}]
│ └─27*[{firefox}]
├─gconfd-2
├─6*[getty]
├─gnome-keyring-d───6*[{gnome-keyring-d}]
├─gvfs-afc-volume───2*[{gvfs-afc-volume}]
├─gvfs-gphoto2-vo───{gvfs-gphoto2-vo}
├─gvfs-udisks2-vo───{gvfs-udisks2-vo}
├─gvfsd───{gvfsd}
├─gvfsd-fuse───4*[{gvfsd-fuse}]
├─gvfsd-metadata───{gvfsd-metadata}
├─gvfsd-trash───{gvfsd-trash}
├─mate-keyring-da───4*[{mate-keyring-da}]
├─mate-screensave
├─mate-settings-d───{mate-settings-d}
├─mate-terminal─┬─bash───su───bash
│ ├─bash───pstree
│ ├─gnome-pty-helpe
│ └─2*[{mate-terminal}]
├─matecomponent-a───2*[{matecomponent-a}]
├─mateconfd-2
├─mdadm
├─mdm───mdm─┬─Xorg
│ └─mate-session─┬─applet.py
│ ├─caja───{caja}
│ ├─marco───{marco}
│ ├─mate-bluetooth-───2*[{mate-bluetooth-}]
│ ├─mate-panel───{mate-panel}
│ ├─mate-power-mana───{mate-power-mana}
│ ├─mate-volume-con───{mate-volume-con}
│ ├─mintupdate-laun───sh───mintUpdate───2*[{mintUpdate}]
│ ├─nm-applet───2*[{nm-applet}]
│ ├─polkit-mate-aut───{polkit-mate-aut}
│ ├─ssh-agent
│ └─2*[{mate-session}]
├─modem-manager
├─mount.ntfs
├─nmbd
├─notification-ar───{notification-ar}
├─pluma───{pluma}
├─polkitd───{polkitd}
├─pulseaudio─┬─gconf-helper
│ └─2*[{pulseaudio}]
├─rsyslogd───3*[{rsyslogd}]
├─rtkit-daemon───2*[{rtkit-daemon}]
├─smbd───smbd
├─system-tools-ba───{system-tools-ba}
├─transmission-gt───3*[{transmission-gt}]
├─udevd───2*[udevd]
├─udisksd───3*[{udisksd}]
├─upowerd───2*[{upowerd}]
├─upstart-socket-
├─upstart-udev-br
├─wnck-applet───{wnck-applet}
└─wpa_supplicant

By default pstree command shows process tree for all processes. But a PID (Process ID) can be provided as an argument.

# pstree 2034
mate-session─┬─applet.py
├─caja───{caja}
├─marco───{marco}
├─mate-bluetooth-───2*[{mate-bluetooth-}]
├─mate-panel───{mate-panel}
├─mate-power-mana───{mate-power-mana}
├─mate-volume-con───{mate-volume-con}
├─mintupdate-laun───sh───mintUpdate───2*[{mintUpdate}]
├─nm-applet───2*[{nm-applet}]
├─polkit-mate-aut───{polkit-mate-aut}
├─ssh-agent
└─2*[{mate-session}]

The processes for a particular user can also be displayed by providing that user's login name as an argument.

# pstree testuser1
at-spi-bus-laun─┬─dbus-daemon
└─3*[{at-spi-bus-laun}]

at-spi2-registr───{at-spi2-registr}

clock-applet───{clock-applet}

dbus-daemon

dbus-launch

dconf-service───2*[{dconf-service}]

firefox─┬─plugin-containe───5*[{plugin-containe}]
└─27*[{firefox}]

gconfd-2

gnome-keyring-d───6*[{gnome-keyring-d}]

gvfs-afc-volume───2*[{gvfs-afc-volume}]

gvfs-gphoto2-vo───{gvfs-gphoto2-vo}

gvfs-udisks2-vo───{gvfs-udisks2-vo}

gvfsd───{gvfsd}

gvfsd-fuse───4*[{gvfsd-fuse}]

gvfsd-metadata───{gvfsd-metadata}

gvfsd-trash───{gvfsd-trash}

mate-keyring-da───4*[{mate-keyring-da}]

mate-screensave

mate-settings-d───{mate-settings-d}

mate-terminal─┬─bash───su───bash───pstree
├─bash
├─gnome-pty-helpe
└─2*[{mate-terminal}]

matecomponent-a───2*[{matecomponent-a}]

mateconfd-2

mate-session─┬─applet.py
├─caja───{caja}
├─marco───{marco}
├─mate-bluetooth-───2*[{mate-bluetooth-}]
├─mate-panel───{mate-panel}
├─mate-power-mana───{mate-power-mana}
├─mate-volume-con───{mate-volume-con}
├─mintupdate-laun───sh───mintUpdate───2*[{mintUpdate}]
├─nm-applet───2*[{nm-applet}]
├─polkit-mate-aut───{polkit-mate-aut}
├─ssh-agent
└─2*[{mate-session}]

notification-ar───{notification-ar}

pluma───{pluma}

pulseaudio─┬─gconf-helper
└─2*[{pulseaudio}]

transmission-gt───3*[{transmission-gt}]

wnck-applet───{wnck-applet}

Filed Under : LINUX COMMANDS

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.