List A Directory With Tree Command On Mac OS X

I needed a solution to create a directory listing backup for my external hard drives. This backup shouldn’t contain any physical data, just a simple directory listing.

From Debian, I already knew the tree command which lists a directory structure recursively.

Make The Tree Command Available On Mac OS X

To install command line tools like the tree command on Mac, you can use the Homebrew package manager as well.

You can read here how to install Homebrew. If you’re able to install Homebrew, you can install the tree command line tool with the command:brew install tree

rschulz@MacBook-Pro:~/$ brew install tree
==> Downloading http://mama.indstate.edu/users/ice/tree/src/tree-1.7.0.tgz
######################################################################## 100.0%
==> make prefix=/usr/local/Cellar/tree/1.7.0 MANDIR=/usr/local/Cellar/tree/1.7.0/share/man/man1 CC=clang CFLAGS=-fomit-frame-pointer LDFLAGS= OBJS=tree.o unix.o html.o xml.o hash.o
?  /usr/local/Cellar/tree/1.7.0: 7 files, 128K, built in 4 seconds
rschulz@MacBook-Pro:~/$

Now you can test the tree command and see what you can do with the tree tool by executing the command tree --help . This will print a list with all the available arguments including a short explanation of what each argument can do:

rschulz@MacBook-Pro:~/$ tree --help
usage: tree [-acdfghilnpqrstuvxACDFJQNSUX] [-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>]
    [--sort[=]<name>] [--matchdirs] [--ignore-case] [--] [<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.
  --ignore-case Ignore case when pattern matching.
  --matchdirs   Include directory names in -P pattern matching.
  --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.
  -------- 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.
  -t            Sort files by last modification time.
  -c            Sort files by last status change time.
  -U            Leave files unsorted.
  -r            Reverse the order of the sort.
  --dirsfirst   List directories before files (-U disables).
  --sort X      Select sort: name,version,size,mtime,ctime.
  ------- Graphics options ------
  -i            Don't print indentation lines.
  -A            Print ANSI lines graphic indentation lines.
  -S            Print with CP437 (console) graphics indentation lines.
  -n            Turn colorization off always (-C overrides).
  -C            Turn colorization on always.
  ------- XML/HTML/JSON options -------
  -X            Prints out an XML representation of the tree.
  -J            Prints out an JSON 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.
  --            Options processing terminator.

Use It

In my case, I only needed to list the first directory level with an alphanumerical file sort and utf-8 character encoding for my file output. The command for that is the following: tree -v -L 1 --charset utf-8. With the additional argument, -o backup.txt you can save the whole tree output in a file. You can also save the tree output bash-like with > backup.txt instead of using -o backup.txt.

If you’re using a daily, weekly, monthly or other scheduled backup, you need to make the backup file more unique. A sexy way is to use a date-time expression in a combination with the output file name. Here’s my final tree command which I use for my weekly backups:

tree -v -L 1 --charset utf-8 > backup-whateverfolder-$(date +%Y-%m-%d).txt

To see the tree output in action, here’s a truncated tree output of my photo library directory:

rschulz@MacBook-Pro:~/Pictures/Photos Library.photoslibrary$ tree -v --charset utf-8
.
├── Attachments
├── Masks
├── Masters
│   └── 2015
│       └── 07
│           ├── 14
│           │   └── 20150714-142123
│           │       └── P1170680.JPG
│           ├── 15
│           │   ├── 20150715-172903
│           │   │   ├── P1170765.JPG
│           │   │   ├── P1170766.JPG
│           │       ├── P1170741.JPG
│           │       ├── P1170743.JPG
│           │       ├── P1170763.JPG
│           │       └── P1170764.JPG
│           └── 17
│               └── 20150717-120015
│                   ├── P1170773.JPG
│                   ├── P1170774.JPG
├── Plugins
├── Previews
│   └── 2015
│       └── 07
│           └── 15
│               └── 20150715-173315
│                   └── Yj0H7vneTS6scsK+hXZjoA
│                       └── VID_20150705_215026.jpg
├── Thumbnails
│   ├── 2015
│   │   └── 07
│   │       ├── 14
│   │       │   └── 20150714-142123
│   │       │       └── LHrHP4n8RM6TGwQXgZ3eFQ
│   │       │           ├── thumb_P1170680.jpg
│   │       │           └── thumb_P1170680_1024.jpg
│   │       ├── 15
│   │       │   ├── 20150715-172903
│   │       │   │   ├── 4Rq1ENccTYua%ZQV3gozFg
│   │       │   │   │   ├── thumb_P1170770.jpg
│   │               ├── qGoJeyiLQmGY6b0RSiOAJQ
│   │               │   ├── thumb_P1170781.jpg
│   │               │   └── thumb_P1170781_1024.jpg
│   │               └── qMM82eauRiiIoLl4mgs6+Q
│   │                   ├── thumb_P1170778.jpg
│   │                   └── thumb_P1170778_1024.jpg
│   └── Segments
│       ├── Thumb64Segment_0.data
│       └── ThumbJPGSegment_0.data
├── database
│   ├── DataModelVersion.plist
│   ├── ImageProxies.apdb
│   ├── ImageProxies.apdb-wal
│   ├── Library.apdb
│   ├── Library.apdb-wal
│   ├── Person.db
│   ├── Person.db-wal
│   ├── Properties.apdb
│   ├── Properties.apdb-wal
│   ├── RKAlbum_name.skindex
│   ├── RKVersion_searchIndexText.skindex
│   ├── metaSchema.db
│   └── metaSchema.db-wal
├── private
│   ├── com.apple.MediaLibraryService
│   ├── com.apple.PhotoIngestService
│   ├── com.apple.Photos
│   │   ├── appPrivateData.plist
│   │   └── faces
│   │       ├── incrClstrSnapshot
│   │       └── sync-marker.plist
│   ├── com.apple.cloudphotosd
│   ├── com.apple.photomodel
│   └── com.apple.photomoments
└── resources

01010101 directories, 010101 files

With the UTF-8 character encoding, you will have the same output in your backup file as you can see in the example output above.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn