Linux mtree utility

You will be able in 3 minutes:
  1. You compile and install last mtree version.
  2. Use it to create directory digest and check directory exactly match by created digest.
You can copy-paste following command into non-root terminal to get the goal.
We're using Fedora and CentOS but fell free to use any other distro.
Just substitute yum install with apt-get install for Debian based OS.
Please do not be confused, the mtree is not the same with tree linux utility.

Building from sources and installing mtree

Now installing the most recent mtree version.
Please do not abuse root powers when building - use non-privileged Unix account to build.
And use sudo to evaluate command to root.
Alternatively you can paste all sudo .. related commands straight to the root terminal, and you may to remove sudo prefix then.
$ mtree
bash: mtree: command not found

open https://github.com/archiecobbs/mtree-port/wiki/Downloads
$ wget https://s3.amazonaws.com/archie-public/mtree-port/mtree-1.0.3.tar.gz
$ tar zxpf mtree-1.0.3.tar.gz
$ cd mtree-1.0.3
$ sudo yum install openssl-devel #you have to install this dependency to build mtree.
$ ./configure --prefix=/opt/mtree-1.0.3-bin
$ make -sw
$ sudo make install
$ sudo ln -s /opt/mtree-1.0.3-bin/bin/mtree /usr/local/bin/mtree
Now you have mtree installed.

mtree usage examples

Creating directory digest


# This represent the default -k option set, you can just omit -k the result will be the same:
$ mtree -p direcory_name -c -k flags,gid,mode,nlink,size,link,time,uid > ./direcory_name.mtree 

# Create digest where every file will be digested by its permission mode:
$ mtree -p direcory_name -c -k size,mode > ./direcory_name.mtree

# Create digest with all default keywords , plus sha1 sum will be included.
# Note of capital -K - it appends keywords you specify to default keywords set.
$ mtree -p direcory_name -c -K sha1digest > ./direcory_name.mtree

# Create digest with only SHA1 checksums:
$ mtree -p direcory_name -c -k sha1digest > ./direcory_name.mtree

Useful keywords can be used: cksum,md5digest,sha1digest,sha256digest,ripemd160digest
see man 8 mtree for full list of keywords.

Verifying directory by created digest

$ mtree -p direcory_name < /direcory_name.mtree

mtree command alternative

You can use standard Linux tools to create simple digest.
# To create
$ cd ./direcory_name ; find -type f -print0 | xargs -0 sha1sum > ../direcory_name.sha1sum

# To check
$ cd ./direcory_name ; sha1sum -c ../direcory_name.sha1sum