MTREE(8)
π MTREE(8) π
System Manager's Manual, but make it bestie-core. okay so this is literally the manual page for mtree, the map-a-directory-hierarchy icon, and we are going to get into every single detail. no skipping. periodt. π
πΈ NAME πΈ
mtree β map a directory hierarchy β¨
π¦ SYNOPSIS π¦
mtree [-bCcDdejLlMnPqrStUuWx] [-i | -m] [-E tags] [-F flavor] [-f spec]
[-I tags] [-K keywords] [-k keywords] [-N dbdir] [-O onlyfile]
[-p path] [-R keywords] [-s seed] [-X exclude-file]
π DESCRIPTION π
The mtree utility compares a file hierarchy against a specification, creates a specification for a file hierarchy, or modifies a specification. like, it's giving βdirectory hierarchy glow-upβ in three different directions. π«Ά
The default action, if not overridden by command line options, is to compare 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 specification, or which are missing from either the file hierarchy or the specification. so if something's off, mtree is gonna say something. not in a mean way. in a βbestie, this doesn't matchβ way. π
The options are as follows, and yes, we're doing every single one:
β¨ OPTIONS β¨
-bβ Suppress blank lines before entering and after exiting directories. no extra blank-line drama. π-Cβ Convert a specification into a format that's easier to parse with various tools. The input specification is read from standard input or from the file given by-f spec. In the output, each file or directory is represented using a single line (which might be very long). The full path name (beginning with./) is always printed as the first field;-K,-k, and-Rcan be used to control which other keywords are printed;-Eand-Ican be used to control which files are printed; and the-Soption can be used to sort the output. one line per file, potentially huge, but so parseable. π-cβ Print a specification for the file hierarchy originating at the current working directory (or the directory provided by-p path) to the standard output. The output is in a style using relative path names. cute relative-path vibes. πΈ-Dβ As per-C, except that the path name is always printed as the last field instead of the first. path name said βI'll go last,β and honestly? iconic. π-dβ Ignore everything except directory type files. directories only. everyone else can leave. π-E tagsβ Add the comma separated tags to the βexclusionβ list. Non-directories with tags which are in the exclusion list are not printed with-Cand-D. it's giving guest list, but for tags. π«-eβ Don't complain about files that are in the file hierarchy, but not in the specification. no extra yelling about extras. π-F flavorβ Set the compatibility flavor of themtreeutility. The flavor can be one ofmtree,freebsd9, ornetbsd6. The default ismtree. Thefreebsd9andnetbsd6flavors attempt to preserve output compatibility and command line option backward compatibility with FreeBSD 9.0 and NetBSD 6.0 respectively. choose your fighter, but respectfully. π¦-f specβ Read the specification from the filespec, instead of from the standard input.
If this option is specified twice, the two specifications are compared to each other rather than to the file hierarchy. The specifications will be sorted like output generated using -c. The output format in this case is somewhat reminiscent of comm(1), having βin first spec onlyβ, βin second spec onlyβ, and βdifferentβ columns, prefixed by zero, one and two TAB characters respectively. Each entry in the βdifferentβ column occupies two lines, one from each specification. spec vs. spec is honestly a whole rivalry arc. π
-I tagsβ Add the comma separated tags to the βinclusionβ list. Non-directories with tags which are in the inclusion list are printed with-Cand-D. If no inclusion list is provided, the default is to display all files. VIP list behavior, but make it tags. β¨-iβ If specified, set theschgand/orsappndflags. flags are being flagged. π-jβ Indent the output 4 spaces each time a directory level is descended when creating a specification with the-coption. This does not affect either the/setstatements or the comment before each directory. It does however affect the comment before the close of each directory. This is the equivalent of the-ioption in the FreeBSD version ofmtree. nesting but make it readable. πͺ-K keywordsβ Add the specified (whitespace or comma separated) keywords to the current set of keywords. Ifallis specified, add all of the other keywords. keyword collection era. π-k keywordsβ Use the mandatory type keyword plus the specified (whitespace or comma separated) keywords to replace the current set of keywords. Ifallis specified, use all of the available keywords. not adding β replacing. main character energy. π-Lβ Follow all symbolic links in the file hierarchy. symlinks said βcome with me,β andmtreesaid bet. π₯-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 option may not be set at the same time as the-Uor-uoption. permission checks but chill β except for the special bits, which stay strict. no cap. π-Mβ Permit merging of specification entries with different types, with the last entry taking precedence. last one wins, bestie. π-mβ If theschgand/orsappndflags are specified, reset these flags. Note that this is only possible with securelevel less than 1 (i.e., in single user mode or while the system is running in insecure mode). Seeinit(8)for information on security levels. > warning vibes: securelevel has entered the chat. π¨-nβ Do not emit pathname comments when creating a specification. Normally a comment is emitted before each directory and before the close of that directory when using the-coption. no pathname commentary. we're keeping it minimal. π-N dbdirβ Use the user database text filemaster.passwdand group database text filegroupfromdbdir, rather than using the results from the system'sgetpwnam(3)andgetgrnam(3)(and related) library calls. custom database directory? slay. ποΈ-O onlypathsβ Only include files included in this list of pathnames. pathname filter, but make it exclusive. β¨-Pβ Don't follow symbolic links in the file hierarchy, instead consider the symbolic link itself in any comparisons. This is the default. symlink stays symlink. iconic behavior. π¦-p pathβ Use the file hierarchy rooted inpath, instead of the current directory. pick a directory, any directory. π-qβ Quiet mode. Do not complain when a βmissingβ directory cannot be created because it already exists. This occurs when the directory is a symbolic link. shhh. no complaints. π-R keywordsβ Remove the specified (whitespace or comma separated) keywords from the current set of keywords. The type keyword is mandatory and is always retained. Ifallis specified, remove all keywords except type. keyword cleanup era, buttypeis never getting voted off the island. π-rβ Remove any files in the file hierarchy that are not described in the specification. Repeating the flag more than once will attempt to reset all the file flags vialchflags(2)before attempting to remove the file in case the file was immutable. > this one is literally deleting files, bestie β read it twice. π«Ά-Sβ When reading a specification into an internal data structure, sort the entries. Sorting will affect the order of the output produced by the-Cor-Doptions, and will also affect the order in which missing entries are created or reported when a directory tree is checked against a specification.
The sort order is the same as that used by the -c option, which is that entries within the same directory are sorted in the order used by strcmp(3), except that entries for subdirectories sort after other entries. By default, if the -S option is not used, entries within the same directory are collected together (separated from entries for other directories), but not sorted. sorting? sometimes. collected together? by default, yes. π
-s seedβ 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. one checksum to rule them all, but seeded. π§-tβ Modify the modified time of existing files, the device type of devices, and symbolic link targets, to match the specification. time, devices, and symlink targets are getting adjusted. β°-Uβ Same as-uexcept that a mismatch is not considered to be an error if it was corrected. corrected mismatch? not an error. we love growth. π±-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. the βactually fix itβ option, with receipts. π-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. no attribute-setting, just vibes. π-X exclude-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 theexclude-filefile. exclusion patterns, but make them globby. π¦-xβ Don't descend below mount points in the file hierarchy. mount point said βboundary,β andmtreerespected it. β
Specifications are mostly composed of βkeywordsβ, i.e. strings 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 π§
Currently supported keywords are as follows:
cksumβ The checksum of the file using the default algorithm specified by thecksum(1)utility. checksum behavior, but make it default-algorithm cute. πdeviceβ The device number to use forblockorcharfile types. The argument must be one of the following forms:
okay bestie, we're mid-keyword-list and this is where it gets good β these are literally the building blocks of your whole spec file, so like, pay attention π
format,major,minorβ a device withmajorandminorfields, for an operating system specified withformat. Valid formats are listed right below, keep reading β¨format,major,unit,subunitβ a device withmajor,unit, andsubunitfields, for an OS specified withformat. (Currently this is only supported by thebsdosformat, iykyk.)numberβ an opaque number (as stored on the file system).
So the recognized values for format are: native, 386bsd, 4bsd, bsdos, freebsd, hpux, isc, linux, netbsd, osf1, sco, solaris, sunos, svr3, svr4, and ultrix. Periodt.
See mknod(8) for more deets.
flagsβ the file flags as a symbolic name. Peepchflags(1)for info on these names. If no flags are to be set, the stringnonemay be used to override the current default. Heads up though: theschgandsappndflags are treated specially (see the-iand-moptions) πignoreβ ignore any file hierarchy below this file. Justβ¦ don't look at it, bestie π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.nochangeβ make sure this file or directory exists but otherwise ignore all attributes. Like, be here, that's it, no notes π«Άoptionalβ the file is optional; don't complain about the file if it's not in the file hierarchy. So chill.ripemd160digestβ synonym forrmd160.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. Sneaky little commas, iconic πtimeβ the last modification time of the file, in second and nanoseconds. The value should include a period character and exactly nine digits after the period. Nine. Not eight. Not ten. Nine. Iykyk β¨typeβ the type of the file; may be set to any one of these: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.
And the default set of keywords are flags, gid, link, mode, nlink, size, time, type, and uid. Those are the ones you get for free, no cap π
π the four types of lines
There are four types of lines in a specification, bestie. Four. Let's go:
- Set global values for a keyword. This consists of the string
/setfollowed 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. Girl math but make it persistent π - Unset global values for a keyword. This consists of the string
/unset, followed by whitespace, followed by one or more keywords, separated by whitespace. Ifallis specified, unset all of the keywords. Everything. Gone. π« - 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_OCTAL format) to encode path names containing non-printable characters. Whitespace characters are encoded as \040 (space), \011 (tab), and \012 (new line). When flavor netbsd6 is selected, strsvis(3) (in VIS_CSTYLE format) is used and 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. Little escape gremlins, we love them πΈ
Each of the keyword/value pairs consist of a keyword, followed by an equals sign (=), followed by the keyword's value, without whitespace characters. These values override, without changing, the global value of the corresponding keyword. Override, not replace β big difference, bestie π
The first path name entry listed must be a directory named ., as this ensures that intermixing full and relative path names will work consistently and correctly. Multiple entries for a directory named . are permitted; the settings for the last such entry override those of the existing entry.
A path name that contains a slash (/) that is not the first character will be treated as a full path (relative to the root of the tree). All parent directories referenced in the path name must exist. The current directory path used by relative path names will be updated appropriately. Multiple entries for the same full path are permitted if the types are the same (unless -M is given, in which case the types may differ); in this case the settings for the last entry take precedence.
A path name that does not contain a slash will be treated as a relative path. Specifying a directory will cause subsequent files to be searched for in that directory hierarchy.
- A line containing only the string
..which causes the current directory path (used by relative paths) to ascend one level. Up we go β¬οΈ
Empty lines and lines whose first non-whitespace character is a hash mark (#) are ignored. Comments, baby β¨
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. Three whole moods, and 2 is the "we have a problem" one π
π FILES
/etc/mtreeβ system specification directory. That's where the specs live, bestie π
πΈ EXAMPLES
To detect system binaries that have been "trojan horsed", it is recommended that mtree be run on the file systems, and a copy of the results stored on a different machine, or, at 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. If the final checksum value changes, the off-line copies of the specification can be used to detect which of the binaries have actually been modified. Threat model queen π
The -d option can be used in combination with -U or -u to create directory hierarchies for, for example, distributions. Very handy, very slay π
β οΈ COMPATIBILITY
The compatibility shims provided by the -F option are incomplete by design. Known limitations are described below, don't come for me about it π
The freebsd9 flavor retains the default handling of lookup failures for the uname and group keywords by replacing them with appropriate uid and gid keywords rather than failing and reporting an error. The related -w flag is a no-op rather than causing a warning to be printed and no keyword to be emitted. The latter behavior is not emulated as it is potentially dangerous in the face of /set statements. Yeah. Dangerous. We said it π«Ά
The netbsd6 flavor does not replicate the historical bug that reported time as seconds.nanoseconds without zero padding nanosecond values less than 100000000. We fixed the bug, we didn't re-bug it. Growth π
π SEE ALSO
chflags(1), chgrp(1), chmod(1), cksum(1), stat(2), fnmatch(3), fts(3), strsvis(3), mtree(5), chown(8), mknod(8). Read them all, they're all connected bestie, it's giving curriculum π
π°οΈ HISTORY
The mtree utility appeared in 4.3BSD-Reno. The optional keyword appeared in NetBSD 1.2. The -U option appeared in NetBSD 1.3. The flags and md5 keywords, and -i and -m options appeared in NetBSD 1.4. The device, rmd160, sha1, tags, and all keywords, -D, -E, -I, -L, -l, -N, -P, -R, -W, and -X options, and support for full paths appeared in NetBSD 1.6. The sha256, sha384, and sha512 keywords appeared in NetBSD 3.0. The -S option appeared in NetBSD 6.0. A whole glow-up timeline, honestly π
okay that's the whole page, bestie β go forth and verify your file hierarchies π this one's from the Debian crew, dated January 8, 2026, and you can find it filed under MTREE(8). mwah πβ¨
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- 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-1017 on : a language model's rewrite of the original, under its terms