MTREE(8)

๐Ÿ“ฆ
this version ยท 6 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

๐Ÿ’– 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 spec in 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 -R can be used to control which other keywords are printed, and -E and -I can 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. ๐Ÿ™ƒ
  • -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 -D. ๐Ÿšซ
  • -e โ€” don't complain about files that are in the file hierarchy, but not in the specification. chill mode for extras. ๐Ÿ˜Œ
  • -f spec โ€” read the specification from file, instead of from the standard input. ๐Ÿ“‚
  • -I tags โ€” 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 the schg and/or sappnd flags. ๐Ÿ”’
  • -K keywords โ€” add the specified (whitespace or comma separated) keywords to the current set of keywords. If โ€˜allโ€™ is specified, add all of the other keywords. โž•
  • -k keywords โ€” use the type keyword 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 the type keyword 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 mode 0444 will pass a check for mode 0644. โ€œ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 -u or -U flags. โš ๏ธ
  • -L โ€” follow all symbolic links in the file hierarchy. ๐Ÿ‡
  • -m โ€” if the schg and/or sappnd flags 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). See init(8) for information on security levels. ๐Ÿ”“
  • -M โ€” permit merging of specification entries with different types, with the last entry take precedence. ๐Ÿค
  • -N dbdir โ€” use the user database text file master.passwd and group database text file group from dbdir, rather than using the results from the system's getpwnam(3) and getgrnam(3) (and related) library calls. ๐Ÿ—ƒ๏ธ
  • -p path โ€” 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 ๐Ÿ‘‹๐Ÿ’€
  • -R keywords โ€” remove the specified (whitespace or comma separated) keywords from the current set of keywords. If โ€˜allโ€™ is specified, remove all of the other keywords. โž–
  • -s seed โ€” display a single checksum to the standard error output that represents all of the files for which the keyword cksum was 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 -i option is given, the schg and sappnd flags will not be set, even if specified. If -m is given, these flags will be reset. Exit with a status of 0 on success, 2 if the file hierarchy did not match the specification, and 1 if any other error occurred. ๐Ÿ› ๏ธโœจ
  • -U โ€” same as -u except 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 -u or -U. ๐Ÿ™…โ™€๏ธ
  • -x โ€” don't descend below mount points in the file hierarchy. ๐Ÿ›‘
  • -X exclude-file โ€” the specified file contains fnmatch(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 the cksum(1) utility. ๐Ÿงฎ
  • device โ€” the device number to use for block or char file types. The argument must be one of the following forms:
    • format,major,minor โ€” a device with major and minor fields, for an operating system specified with format. See below for valid formats.
    • format,major,unit,subunit โ€” a device with major, unit, and subunit fields, for an operating system specified with format. (Currently this is only supported by the bsdos format.)
    • 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. See chflags(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 the schg and sappnd flags are treated specially (see the -i and -m options). ๐Ÿšฉ
  • 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 for md5. ๐Ÿชž
  • 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 for rmd160. ๐Ÿชž
  • sha1 โ€” the SHA-1 cryptographic message digest of the file. ๐Ÿ”
  • sha1digest โ€” synonym for sha1. ๐Ÿชž
  • sha256 โ€” the 256-bits SHA-2 cryptographic message digest of the file. ๐Ÿ”
  • sha256digest โ€” synonym for sha256. ๐Ÿชž
  • sha384 โ€” the 384-bits SHA-2 cryptographic message digest of the file. ๐Ÿ”
  • sha384digest โ€” synonym for sha384. ๐Ÿชž
  • sha512 โ€” the 512-bits SHA-2 cryptographic message digest of the file. ๐Ÿ”
  • sha512digest โ€” synonym for sha512. ๐Ÿชž
  • size โ€” the size, in bytes, of the file. ๐Ÿ“
  • tags โ€” comma delimited tags to be matched with -E and -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 device
    • char โ€” character special device
    • dir โ€” directory
    • fifo โ€” fifo
    • file โ€” regular file
    • link โ€” symbolic link
    • socket โ€” 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 ๐Ÿ“

  1. 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. ๐Ÿ”ง
  2. 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. ๐Ÿงน
  3. 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.

  1. 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. 0 good, 1 broke, 2 mismatch. 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 mtree utility appeared in 4.3BSD-Reno.
  • The optional keyword appeared in NetBSD 1.2.
  • The -U flag appeared in NetBSD 1.3.
  • The flags and md5 keywords, and -i and -m flags, appeared in NetBSD 1.4.
  • The device, rmd160, sha1, tags, and all keywords, the -D, -E, -I, -l, -L, -N, -P, -R, -W, and -X flags, and support for full paths appeared in NetBSD 1.6.
  • The sha256, sha384, and sha512 keywords appeared in NetBSD 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