Posted by Steve on Thu 17 Jul 2008 at 06:42
tree is a very simple utility which will draw a tree of a directory structure. It isn't a command which is generally useful, but it can be very handy to know about if you're writing articles!
The tree command is contained in the tree package. If you don't have it installed you may get it via one of the following two commands:
root@sethra:~# apt-get update && apt-get install tree root@sethra:~# aptitude update && aptitude install tree
Once installed it is invoked by running tree [/path/to/draw. For example I could run:
skx@sethra:~$ tree /boot/ /boot |-- System.map-2.6.18-6-amd64 |-- System.map-2.6.18-6-xen-amd64 |-- config-2.6.18-6-amd64 |-- config-2.6.18-6-xen-amd64 |-- debian.bmp -> /boot/sarge.bmp |-- grub | |-- default | |-- device.map | |-- e2fs_stage1_5 | |-- fat_stage1_5 | |-- jfs_stage1_5 | |-- menu.lst | |-- menu.lst~ | |-- minix_stage1_5 | |-- reiserfs_stage1_5 | |-- stage1 | |-- stage2 | |-- stage2_eltorito | `-- xfs_stage1_5 |-- initrd.img-2.6.18-6-amd64 |-- initrd.img-2.6.18-6-xen-amd64 |-- initrd.img-2.6.18-6-xen-amd64.bak |-- lost+found [error opening dir] |-- vmlinuz-2.6.18-6-amd64 |-- vmlinuz-2.6.18-6-xen-amd64 `-- xen-3.0.3-1-amd64.gz 2 directories, 24 files
The output is customisable via command line flags. For example it is possible to tell the utility to avoid displaying files, (such that only directories are shown), via:
skx@sethra:~$ tree -d /boot/ /boot/ |-- grub `-- lost+found [error opening dir] 2 directories
Similarly "tree -C" causes tree to display files in colour, in a similar manner to that which you might be used to being produced via ls. (When "ls --color=auto" is in use.)
For a full list of options please read the manpage via "man tree" - whilst this utility isn't going to be the most popular command we've ever covered there are times when it is very useful to have to hand!
This article can be found online at the Debian Administration website at the following bookmarkable URL:
This article is copyright 2008 Steve - please ask for permission to republish or translate.