13

I'm trying to learn the POSIX standard. When using Unix, I have /bin, /etc, /dev, /media, and usually /opt, but is that standardized or up to your spec implementation? Could there be many directories in the root, or is it limited by the standard?

HalosGhost
  • 4,732
  • 10
  • 33
  • 41
Niklas Rosencrantz
  • 4,112
  • 6
  • 36
  • 58

4 Answers4

18

According to The Open Group's published standard, the only required directories are:

  • /
  • /dev, which contains console, null, and tty
  • /tmp, guaranteed writable but not necessarily preserved.

The Linux Foundation maintains a Filesystem Hierarchy Standard (FHS) which extends this to include the directories you will typically see on a Linux system:

  • /bin: Essential user command binaries
  • /boot: Static files of the bootloader
  • /dev: Device files
  • /etc: Host-specific system configuration
  • /home: User home directories (optional)
  • /lib: Essential shared libraries and kernel modules
  • /lib<qual>: Alternate format essential shared libraries (optional)
  • /media: Mount point for removable media
  • /mnt: Mount point for a temporarily mounted filesystem
  • /opt: Add-on application software packages
  • /root: Home directory for the root user (optional)
  • /run: Run-time variable data
  • /sbin: System binaries
  • /srv: Data for services provided by this system
  • /tmp: Temporary files
  • As well as the /usr hierarchy and the /var hierarchy

The FHS was designed to be as generic as possible, to allow for incorporation in any UNIX system. The additional directories are likely to exist in any reasonable system, but this is not mandated by POSIX.

However, note that The Open Group also states that

Strictly conforming applications shall not assume the ability to create files in any of these directories, unless specified below.

Since directories are really just files, this implies that a strictly conforming application will not create any files or directories at the root level. Therefore, POSIX does not necessarily limit what a distribution may place at the root level, but does seem to state that an application conforming to its specification cannot assume that it will be able to.

Fox
  • 8,013
  • 1
  • 26
  • 44
14

In 10.1 Directory Structure and Files, POSIX lists directories which must exist. But it specifies no limit on the number of other directories which can exist at the root-level of a filesystem.

For that matter, it does not appear to place limits on the size of other directories.

POSIX's attention in this area is focused on commonality rather than differences.

Thomas Dickey
  • 75,040
  • 9
  • 171
  • 268
  • As on a \*nix system potentially *every* directory (including `/`) can be a root directory, it would be pretty silly to impose an upper limit on the number of entries in a root directory. `/` isn't particularly special in this regard, thus it follows that it would be pretty equally silly to impose an upper limit on the number of entries in `/` specifically. – user Jul 15 '16 at 13:35
9

There is no limitation to the number of entries in a directory, either in POSIX or in typical Unix implementations. There may be an indirect limit for the number of subdirectories, which is the maximum hard link count (each subdirectory's .. entry is a hard link to the directory); that's 216 for many common filesystems, which limits a directory to 65533 subdirectories (at least for those filesystems that store .. entries explicitly). You'll start hitting poor performance before that. According to POSIX, an implementation is allowed to support only 8 hard links on a file (_POSIX_LINK_MAX), but no actual implementation is limited to 6 subdirectories. And anyway, on many filesystems, including ext4, the hard link count is not maintained for .. entries, so the only limit is how much space or how many inodes are available on the filesystem.

POSIX doesn't say much about the organization of files on the system. It only mandates the existence of a few files. The only mandatory entries in the root directory are /dev and /tmp. Other habitual Unix entries such as /usr, /var, /bin, /etc, /lib, /home, etc. are Unix conventions that are not codified by POSIX.

On Linux, the FHS codifies the classics and a couple more. Most Linux distributions stick to the FHS entries. Other Unix systems typically have mostly the same entries, maybe with a few differences, but the number is about the same.

System administrators may create more, although this is discouraged: there are well-defined places for most things (software goes under /usr or /opt, system data goes under /var, user data goes under /net, mount points go under /media or /mnt, etc.), so there is rarely any good reason to create new directories at the top level.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • This is not correct. Most "reasonable" filesystems have a maximum number of subdirectories per directory, or a maximum number of inodes per device. However, it is usually obscenely large relative to the number of directories you're likely to see in the root of the drive. – Kevin Jul 15 '16 at 06:36
  • @Kevin Oh, good point, on common filesystems you'd hit the maximum hard link count first, and that could be as low as 16 bits. – Gilles 'SO- stop being evil' Jul 15 '16 at 14:03
  • @Kevin Though this is not the case on ext4, there doesn't appear to be a limit on the number of subdirectories other than available space (blocks or inodes). Were you thinking of some other limit on the number of subdirectories per directory? – Gilles 'SO- stop being evil' Jul 15 '16 at 14:11
  • A properly designed filesystem does not create hard links to the .. directory entries. In a properly designed filesystem the link count of a directory is 1 unless there are manually created hard links. – schily Jul 15 '16 at 15:15
  • The inode number in a directory entry is not arbitrary precision. @schily: The . and .. entries are hard links. Check the link count of your root dir. – Kevin Jul 15 '16 at 15:30
  • @Kevin But the inode number is limited by the filesystem size before it's limited by the number of bits used to represent it. – Gilles 'SO- stop being evil' Jul 15 '16 at 16:02
  • Either way, there's a maximum number of top-level directories. – Kevin Jul 15 '16 at 16:03
  • 1
    No, there is no such limit on a 100% POSIX compliant filesystem. As mentioned already, POSIX does not require . and .. to be hardlinks to other directories. If they are, this is an implementation detail that is a result of the hacks introduced in the V7 filesystem. There are other POSIX compliant filesystems that work completely different. – schily Jul 18 '16 at 09:43
  • You forgot the text of your footnote. – Martin Schröder Jul 19 '16 at 19:08
  • @MartinSchröder I don't remember what the footnote was supposed to be. I might have changed my mind mid-writing and included the remark in the main text. – Gilles 'SO- stop being evil' Jul 19 '16 at 19:14
2

unlike FAT the filesystems used by UNIX don't have a special size limit on the root directory, but once the partition is full you won't be able to add more.

Jasen
  • 3,715
  • 13
  • 14