The mtree utility compares the file hierarchy rooted in the current directory against a specification read
from the standard input. Messages are written to the standard output for any files whose characteristics do
not match the specifications, or which are missing from either the file hierarchy or the specification.

Use

mtree can be used to create a template directory structure. If you've got a simple structure, say one or two levels deep with only a handful of sub directories, this is easier accomplished with a simple shell script. For this use, simply create your directory structure, and create a specification file with the mtree -c > outputfile command. You may hand-edit this file to adjust certain settings, such as ownership. mtree -f outputfile -U should create the structure for you.

Example Use

There are many uses for mtree, as I'm discovering. Today, I used mtree to quickly repair permissions on a large file structure which I had to restore from backups. Our backup system is a disk-based server utilizing rsync to transfer the files. Restoring about 300GB of data yesterday, I erroneously used rsync -rv directory host:/directory instead of rsync -av directory host:/directory. This meant that ALL files were owned by the user which restored the files, root.

I thought it would be a simple matter of running rsync again, but rsync appeared to want to transfer all the files again, which I didn't have the patience for. Along came mtree to the rescue.

Using mtree, I created a specification file from the backup copy (with the correct permissions) with the following command:

cd /path/to/directory
mtree -c > spec.txt

This done, I copied the file to the system which had the restore operation, and ran the following command to fix all the file meta data: