Displaying a Tree Directory Structure in Linux

If your are on Linux and need to list all the files in the form of a tree (a directory, all its subdirs, all subdirs of those, etc.), you can use the very handy tree command to do so. On its man page, Tree is described as a recursive directory listing program that list contents of directories in a tree-like format. This comes in handy if you want to have a depth indented listing of files.

Here is an example output of tree command showing the content of my home directory in a tree format

$ tree
.
├── apache.yml
├── app
│   ├── app.js
│   └── Dockerfile
├── busybox.yml
├── ComposeFiles
│   └── docker-compose.yml
├── Desktop
├── Documents
├── Downloads
├── minikube-linux-amd64
├── Music
├── Pictures
├── Public
├── Templates
└── Videos

10 directories, 6 files

Installing and Using tree


The tree command should be available in most distributions. If not already there, start by installing it.

$ # On Centos/RHEL/Fedora
$ sudo yum install -y tree

$ # On Ubuntu/Debian
$ sudo apt-get install tree

Check if the installation is successful

$ tree --version
tree v1.6.0 (c) 1996 - 2011 by Steve Baker, Thomas Moore, Francesc Rocher, Kyosuke Tokoro

The tree command syntax is as follow

tree path/To/Folder

Here is an example that display the content of / directory in tree format

$ tree /

/
├── 1
├── bin -> usr/bin
├── boot
│   ├── config-3.10.0-514.el7.x86_64
│   ├── config-3.10.0-693.2.2.el7.x86_64
│   ├── config-3.10.0-693.5.2.el7.x86_64
│   ├── efi
│   │   └── EFI
│   │       └── centos
│   ├── grub
│   │   └── splash.xpm.gz
│   ├── grub2 [error opening dir]
│   ├── initramfs-0-rescue-762489019a444b4f9d999f45b5828be3.img
│   ├── initramfs-3.10.0-514.el7.x86_64.img
│   ├── initramfs-3.10.0-514.el7.x86_64kdump.img
│   ├── initramfs-3.10.0-693.2.2.el7.x86_64.img
│   ├── initramfs-3.10.0-693.5.2.el7.x86_64.img
│   ├── initramfs-3.10.0-693.5.2.el7.x86_64kdump.img
│   ├── initrd-plymouth.img
│   ├── symvers-3.10.0-514.el7.x86_64.gz
│   ├── symvers-3.10.0-693.2.2.el7.x86_64.gz
...

Using the tree command without any option should be enough most of the time. However, if you need to customize the output, here are the most common ones.

  • Display all files (hidden ones are not included in the listing).
$ tree -a
  • Display directories only.
$ tree -d
  • Print the username, or UID number if no username is available, of the file..
$ tree -u
  • Display the size in a human readable format
$ tree -h

You can use can use the --help directive to get all the options that can be used with tree.

$ tree --help
usage: tree [-acdfghilnpqrstuvxACDFQNSUX] [-H baseHREF] [-T title ] [-L level [-R]]
        [-P pattern] [-I pattern] [-o filename] [--version] [--help] [--inodes]
        [--device] [--noreport] [--nolinks] [--dirsfirst] [--charset charset]
        [--filelimit[=]#] [--si] [--timefmt[=]<f>] [<directory list>]
  ------- Listing options -------
  -a            All files are listed.
  -d            List directories only.
  -l            Follow symbolic links like directories.
  -f            Print the full path prefix for each file.
  -x            Stay on current filesystem only.
  -L level      Descend only level directories deep.
  -R            Rerun tree when max dir level reached.
  -P pattern    List only those files that match the pattern given.
  -I pattern    Do not list files that match the given pattern.
  --noreport    Turn off file/directory count at end of tree listing.
  --charset X   Use charset X for terminal/HTML and indentation line output.
  --filelimit # Do not descend dirs with more than # files in them.
  --timefmt <f> Print and format time according to the format <f>.
  -o filename   Output to file instead of stdout.
  --du          Print directory sizes.
  --prune       Prune empty directories from the output.
  -------- File options ---------
  -q            Print non-printable characters as '?'.
  -N            Print non-printable characters as is.
  -Q            Quote filenames with double quotes.
  -p            Print the protections for each file.
  -u            Displays file owner or UID number.
  -g            Displays file group owner or GID number.
  -s            Print the size in bytes of each file.
  -h            Print the size in a more human readable way.
  --si          Like -h, but use in SI units (powers of 1000).
  -D            Print the date of last modification or (-c) status change.
  -F            Appends '/', '=', '*', '@', '|' or '>' as per ls -F.
  --inodes      Print inode number of each file.
  --device      Print device ID number to which each file belongs.
  ------- Sorting options -------
  -v            Sort files alphanumerically by version.
  -r            Sort files in reverse alphanumeric order.
  -t            Sort files by last modification time.
  -c            Sort files by last status change time.
  -U            Leave files unsorted.
  --dirsfirst   List directories before files (-U disables).
  ------- Graphics options ------
  -i            Don't print indentation lines.
  -A            Print ANSI lines graphic indentation lines.
  -S            Print with ASCII graphics indentation lines.
  -n            Turn colorization off always (-C overrides).
  -C            Turn colorization on always.
  ------- XML/HTML options -------
  -X            Prints out an XML representation of the tree.
  -H baseHREF   Prints out HTML format with baseHREF as top directory.
  -T string     Replace the default HTML title and H1 header with string.
  --nolinks     Turn off hyperlinks in HTML output.
  ---- Miscellaneous options ----
  --version     Print version and exit.
  --help        Print usage and this help message and exit.

Leave a Comment

Your email address will not be published. Required fields are marked *