MTREE(8)
๐ MTREE ๐
mtree โ section 8 of the System Manager's Manual, aka the girl who maps a directory hierarchy โจ
๐ธ NAME ๐ธ
mtree โ map a directory hierarchy ๐บ๏ธ๐ซถ
๐ฆ SYNOPSIS ๐ฆ
mtree [-cCdDelLMPruUWx] [-i | -m] [-f spec] [-p path] [-k keywords]
[-K keywords] [-R keywords] [-E tags] [-I tags] [-N dbdir]
[-s seed] [-X exclude-file]
๐ DESCRIPTION ๐
okay so bestie, here's the whole vibe: mtree takes the file hierarchy rooted in the current directory and compares it against a specification that it reads from standard input. ๐
Every time a file's characteristics don't match the spec โ or a file is missing from either the file hierarchy or the specification โ it writes a message to standard output. No cap, that's the whole job. ๐
The options are as follows:
โจ the options (all of them, we're thorough in this house) โจ
-cโ prints a specification for the file hierarchy to the standard output. ๐-dโ ignore everything except directory type files. dirs only, periodt.-Cโ print (โdumpโ) the specification as provided by-f specin a format that's easier to parse with various tools. ๐ ๏ธ The full path name is always printed as the first field, and-k,-K, and-Rcan be used to control which other keywords are printed, and-Eand-Ican be used to control which files are printed.-Dโ as per-C, except that the path name is always printed as the last field instead of the first. ๐-Etags โ add the comma separated tags to the โexclusionโ list. Non-directories with tags which are in the exclusion list are not printed with-D. ๐ซ-eโ don't complain about files that are in the file hierarchy, but not in the specification. chill mode for extras. ๐-fspec โ read the specification from file, instead of from the standard input. ๐-Itags โ add the comma separated tags to the โinclusionโ list. Non-directories with tags which are in the inclusion list are printed with-D. If no inclusion list is provided, the default is to display all files. โ-iโ if specified, set theschgand/orsappndflags. ๐-Kkeywords โ add the specified (whitespace or comma separated) keywords to the current set of keywords. If โallโ is specified, add all of the other keywords. โ-kkeywords โ use thetypekeyword plus the specified (whitespace or comma separated) keywords instead of the current set of keywords. If โallโ is specified, use all of the other keywords. If thetypekeyword is not desired, suppress it with-R type. ๐-lโ do โlooseโ permissions checks, in which more stringent permissions will match less stringent ones. For example, a file marked mode0444will pass a check for mode0644. โLooseโ checks apply only to read, write and execute permissions โ in particular, if other bits like the sticky bit or suid/sgid bits are set either in the specification or the file, exact checking will be performed. This flag may not be set at the same time as the-uor-Uflags. โ ๏ธ-Lโ follow all symbolic links in the file hierarchy. ๐-mโ if theschgand/orsappndflags are specified, reset these flags. Note that this is only possible with securelevel less than1(i.e. in single user mode or while the system is running in insecure mode). Seeinit(8)for information on security levels. ๐-Mโ permit merging of specification entries with different types, with the last entry take precedence. ๐ค-Ndbdir โ use the user database text filemaster.passwdand group database text filegroupfrom dbdir, rather than using the results from the system'sgetpwnam(3)andgetgrnam(3)(and related) library calls. ๐๏ธ-ppath โ use the file hierarchy rooted in path, instead of the current directory. ๐-Pโ don't follow symbolic links in the file hierarchy, instead consider the symbolic link itself in any comparisons. This is the default. ๐ฏ-rโ remove any files in the file hierarchy that are not described in the specification. bye bestie ๐๐-Rkeywords โ remove the specified (whitespace or comma separated) keywords from the current set of keywords. If โallโ is specified, remove all of the other keywords. โ-sseed โ display a single checksum to the standard error output that represents all of the files for which the keywordcksumwas specified. The checksum is seeded with the specified value. ๐ฑ-uโ modify the owner, group, permissions, and flags of existing files, the device type of devices, and symbolic link targets, to match the specification. Create any missing directories, devices or symbolic links. User, group, and permissions must all be specified for missing directories to be created. Note that unless the-ioption is given, theschgandsappndflags will not be set, even if specified. If-mis given, these flags will be reset. Exit with a status of0on success,2if the file hierarchy did not match the specification, and1if any other error occurred. ๐ ๏ธโจ-Uโ same as-uexcept that a mismatch is not considered to be an error if it was corrected. forgiving queen ๐-Wโ don't attempt to set various file attributes such as the ownership, mode, flags, or time when creating new directories or changing existing entries. This option will be most useful when used in conjunction with-uor-U. ๐ โ๏ธ-xโ don't descend below mount points in the file hierarchy. ๐-Xexclude-file โ the specified file containsfnmatch(3)patterns matching files to be excluded from the specification, one to a line. If the pattern contains a โ/โ character, it will be matched against entire pathnames (relative to the starting directory); otherwise, it will be matched against basenames only. Comments are permitted in the exclude-list file. ๐
๐ the keywords ๐
Specifications are mostly composed of โkeywordsโ, i.e. strings that that specify values relating to files. No keywords have default values, and if a keyword has no value set, no checks based on it are performed. ๐คทโ๏ธ
Currently supported keywords are as follows:
cksumโ the checksum of the file using the default algorithm specified by thecksum(1)utility. ๐งฎdeviceโ the device number to use forblockorcharfile types. The argument must be one of the following forms:format,major,minorโ a device withmajorandminorfields, for an operating system specified with format. See below for valid formats.format,major,unit,subunitโ a device withmajor,unit, andsubunitfields, for an operating system specified with format. (Currently this is only supported by thebsdosformat.)numberโ opaque number (as stored on the file system).
The following values for format are recognized: native, 386bsd, 4bsd, bsdos, freebsd, hpux, isc, linux, netbsd, osf1, sco, solaris, sunos, svr3, svr4, and ultrix.
See mknod(8) for more details. ๐ง
flagsโ the file flags as a symbolic name. Seechflags(1)for information on these names. If no flags are to be set the string โnoneโ may be used to override the current default. Note that theschgandsappndflags are treated specially (see the-iand-moptions). ๐ฉignoreโ ignore any file hierarchy below this file. ๐gidโ the file group as a numeric value. ๐ขgnameโ the file group as a symbolic name. ๐ท๏ธlinkโ the file the symbolic link is expected to reference. ๐md5โ the MD5 cryptographic message digest of the file. ๐md5digestโ synonym formd5. ๐ชmodeโ the current file's permissions as a numeric (octal) or symbolic value. ๐๏ธnlinkโ the number of hard links the file is expected to have. โ๏ธoptionalโ the file is optional; don't complain about the file if it's not in the file hierarchy. ๐ซถrmd160โ the RMD-160 cryptographic message digest of the file. ๐rmd160digestโ synonym forrmd160. ๐ชsha1โ the SHA-1 cryptographic message digest of the file. ๐sha1digestโ synonym forsha1. ๐ชsha256โ the 256-bits SHA-2 cryptographic message digest of the file. ๐sha256digestโ synonym forsha256. ๐ชsha384โ the 384-bits SHA-2 cryptographic message digest of the file. ๐sha384digestโ synonym forsha384. ๐ชsha512โ the 512-bits SHA-2 cryptographic message digest of the file. ๐sha512digestโ synonym forsha512. ๐ชsizeโ the size, in bytes, of the file. ๐tagsโ comma delimited tags to be matched with-Eand-I. These may be specified without leading or trailing commas, but will be stored internally with them. ๐ท๏ธtimeโ the last modification time of the file. โฐtypeโ the type of the file; may be set to any one of the following:blockโ block special devicecharโ character special devicedirโ directoryfifoโ fifofileโ regular filelinkโ symbolic linksocketโ socket
uidโ the file owner as a numeric value. ๐ขunameโ the file owner as a symbolic name. ๐ท๏ธ
The default set of keywords are flags, gid, link, mode, nlink, size, time, type, and uid. ๐
๐ the four types of lines in a specification ๐
- Set global values for a keyword. This consists of the string โ
/setโ followed by whitespace, followed by sets of keyword/value pairs, separated by whitespace. Keyword/value pairs consist of a keyword, followed by an equals sign (โ=โ), followed by a value, without whitespace characters. Once a keyword has been set, its value remains unchanged until either reset or unset. ๐ง - Unset global values for a keyword. This consists of the string โ
/unsetโ, followed by whitespace, followed by one or more keywords, separated by whitespace. If โallโ is specified, unset all of the keywords. ๐งน - A file specification, consisting of a path name, followed by whitespace, followed by zero or more whitespace separated keyword/value pairs.
The path name may be preceded by whitespace characters. The path name may contain any of the standard path name matching characters (โ[โ, โ]โ, โ?โ or โ*โ), in which case files in the hierarchy will be associated with the first pattern that they match. mtree uses strsvis(3) (in VIS_CSTYLE format) to encode path names containing non-printable characters. Whitespace characters are encoded as โ\sโ (space), โ\tโ (tab), and โ\nโ (new line). โ#โ characters in path names are escaped by a preceding backslash โ\โ to distinguish them from comments. ๐
Each of those keyword/value pairs is a keyword, then an equals sign (=), then the keyword's value โ no whitespace characters allowed in there, bestie, not even one. And these values override the global value of that same keyword. They don't change it, they just... override it for that entry. Slay.
The first path name entry you list must be a directory named . โ because that's what makes mixing full and relative path names actually work consistently and correctly. Multiple entries for a directory named . are totally permitted btw; the settings from the last such entry win over the existing one. Periodt.
A path name with a slash (/) in it that is not the first character? That's treated as a full path (relative to the root of the tree). All the parent directories referenced in that path name must exist, no skipsies. And the current directory path used by relative path names gets updated appropriately. Multiple entries for the same full path are allowed if the types are the same (unless -M is given, and then the types may differ) โ in that case, the settings for the last entry take precedence. ๐
A path name with no slash at all is treated as a relative path. And specifying a directory means subsequent files get searched for in that directory hierarchy. Iconic behavior honestly.
- A line containing only the string
..โ this causes the current directory path (the one used by relative paths) to ascend one level. Go up, girl.
Empty lines, and lines whose first non-whitespace character is a hash mark (#), are ignored. Living rent free, contributing nothing, and honestly? Valid.
The mtree utility exits with a status of 0 on success, 1 if any error occurred, and 2 if the file hierarchy did not match the specification.
Bestie. Memorize those numbers.0good,1broke,2mismatch. That's the whole vibe check right there. ๐
๐ FILES ๐
/etc/mtreeโ system specification directory
โจ EXAMPLES โจ
So you wanna detect system binaries that have been โtrojan horsedโ? Okay, listen up, this is the important part:
It is recommended that mtree be run on the file systems, and that a copy of the results be stored on a different machine โ or, at the very least, in encrypted form. The seed for the -s option should not be an obvious value, and the final checksum should not be stored on-line under any circumstances! ๐ญ
Then, periodically, mtree should be run against the on-line specifications, and the final checksum compared with the previous value.
While it is possible for the bad guys to change the on-line specifications to conform to their modified binaries, it shouldn't be possible for them to make it produce the same final checksum value. So if that final checksum value changes โ boom โ the off-line copies of the specification can be used to detect which of the binaries have actually been modified. ๐ต๏ธโโ๏ธโจ
The -d and -u options can be used in combination to create directory hierarchies for distributions and other such things. Two options, one slay.
๐ SEE ALSO ๐
chflags(1), chgrp(1), chmod(1), cksum(1), stat(2), fnmatch(3), fts(3), strsvis(3), chown(8), mknod(8)
The whole friend group. Go say hi. ๐ซถ
๐ HISTORY ๐
- The
mtreeutility appeared in4.3BSD-Reno. - The
optionalkeyword appeared inNetBSD 1.2. - The
-Uflag appeared inNetBSD 1.3. - The
flagsandmd5keywords, and-iand-mflags, appeared inNetBSD 1.4. - The
device,rmd160,sha1,tags, andallkeywords, the-D,-E,-I,-l,-L,-N,-P,-R,-W, and-Xflags, and support for full paths appeared inNetBSD 1.6. - The
sha256,sha384, andsha512keywords appeared inNetBSD 3.0.
Look at that glow-up timeline. From 4.3BSD-Reno all the way to NetBSD 3.0 โ a true main character arc. ๐
And that's the whole page, bestie!! ๐ This one comes to us from Debian, dated September 12, 2006 โ yeah, she's been iconic for a minute. Go forth and checksum responsibly. No cap. โจ๐ฆ๐
๐ฆ where this page comes from
- debian bookworm: mtree-netbsd 20180822-7 ยท BSD-3-Clause
- debian trixie: mtree-netbsd 20180822-9 ยท BSD-3-Clause
- ubuntu jammy: mtree-netbsd 20180822-6 ยท BSD-3-Clause
- ubuntu noble: mtree-netbsd 20180822-8 ยท BSD-3-Clause
- ubuntu questing: mtree-netbsd 20180822-9
- ubuntu resolute: mtree-netbsd 20180822-9build1 ยท BSD-3-Clause
- ubuntu stonking: mtree-netbsd 20180822-9build1 ยท BSD-3-Clause
- Copyright (c) 1989, 1990, 1993 The Regents of the University of California. All rights reserved
- Copyright (c) 2001-2004 The NetBSD Foundation, Inc
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms