Skip to content

Instantly share code, notes, and snippets.

@jazzqi
Last active October 16, 2022 20:06
Show Gist options
  • Save jazzqi/3e055f64a083d2bfbdae07c8059980eb to your computer and use it in GitHub Desktop.
Save jazzqi/3e055f64a083d2bfbdae07c8059980eb to your computer and use it in GitHub Desktop.
Linux Filesystem Hierarchy Standard Reference

The Filesystem Hierarchy Standard (FHS) defines the main directories and their contents in Linux and other Unix-like computer operating systems.

Overview

The process of developing a standard FileSystem hierarchy began in August 1993 with an effort to restructure the file and directory structure of Linux. The FSSTND (Filesystem Standard), a filesystem hierarchy standard specific to the Linux operating system, was released on February 14, 1994. Subsequent revisions were released on October 9, 1994 and March 28, 1995.

In early 1996, the goal of developing a more comprehensive version of FSSTND to address not only Linux, but other Unix-like systems was adopted with the help of members of the BSD development community. As a result, a concerted effort was made to focus on issues that were general to Unix-like systems. In recognition of this widening of scope, the name of the standard was changed to Filesystem Hierarchy Standard or FHS for short.

The FHS is maintained by the Free Standards Group, a non-profit organization consisting of major software and hardware vendors, such as HP, IBM and Dell. Still, the vast majority of the Linux distributions, including those developed by members of the Free Standards Group, do not follow this proposed standard. In particular, paths specifically created by the FHS editors, such as /media/ and /srv/, do not see widespread usage. Some Unix and Linux systems break with the FHS in favor of a different approach, as in Gobo Linux.

Directory structure

All files and directories appear under the ?root_directory "/", even if stored on different physical devices.

A description of the hierarchy specified in the FHS

/bin/

Essential command executable (binaries) for all users (e.g., cat, ls, cp) (especially files required to boot or rescue the system)

/boot/

Boot loader, kernels and initrd files

/dev/

devices files (e.g., :/dev/null)

/etc/

Host-specific system-wide configuration files (from et cetera)

/home/

Users' home directories

/lib/

Libraries essential for the binaries in /bin/ and /sbin/ (library required to boot or rescue the system)

/lost+found/

Some files and fragment that were "recovered" during the previous fsck (Not part of FHS)

/mnt/

Temporarily mounted filesystems

/media/

Mount points for removable media such as CD-ROMs (appeared in FHS-2.3)

/opt/

Add-on application software packages Pre-compiled, non ".deb" binary distribution (tar'ed..) goes here.

/opt/bin/ : Same as for top-level hierarchy /opt/include/ : Same as for top-level hierarchy /opt/lib/ : Same as for top-level hierarchy /opt/sbin/ : Same as for top-level hierarchy /opt/share/ : Same as for top-level hierarchy

/proc/

Virtual filesystem documenting kernel and process status, mostly text files (e.g., uptime, network)

/root/

Home directory for the root user

/sbin/

System administrative binaries (e.g., init, route, ifup) (system binaries) (files required to boot or rescue the system)

/tmp/

Temporary files

/selinux/

SE-Linux runtime settings (Not part of FHS).

/srv/

Site-specific data which is served by the system (Not part of FHS).

/sys/

The filesystem for exporting kernel objects. (many /proc/* files should have been here...)

/usr/

Secondary hierarchy for shareable, read-only data (formerly from UNIX source repository, now from UNIX system resources) (files that are not-required to boot or rescue the system)

/usr/bin/ : Same as for top-level hierarchy /usr/include/ : Standard include files

/usr/lib/ : Same as for top-level hierarchy /usr/sbin/ : Same as for top-level hierarchy /usr/share/ : Architecture-independent (shared) data

/usr/src/ : Source code (to build debian packages. see also /usr/local/src/)

/usr/X11R6/ : X Window System, Version 11 Release 6

/usr/local/ : Tertiary hierarchy for local data installed by the system administrator

/usr/local/bin : locally compiled binaries, local shell script, etc. /usr/local/src : Source code (place where to extract and build non debian'ized stuffs)

/var/

Variable data, such as logs, databases, websites, and temporary spool (e-mail..) files

External links "The Directory Tree" overview (Debian GNU/Linux Installation Guide i386, amd64..)

/ - Root Directory

As we all know Linux file system starts with /, the root directory. All other directories are 'children' of this directory. The partition which the root file system resides on is mounted first during boot and the system will not boot if it doesn't find it.


/bin - Essential Binaries

This directory contains several useful commands that are of use to both the system administrator as well as non-privileged users.

Unlike /sbin, the bin directory contains several useful commands that are of use to both the system administrator as well as non-privileged users. It usually contains the shells like bash, zsh, etc. and commonly used commands like cp, mv, rm, cat, ls. For this reason and in contrast to /usr/bin, the binaries in this directory are considered to be essential. The reason for this is that it contains essential system programs that must be available even if only the partition containing / is mounted. It also contains programs which boot scripts may depend on.


/boot - Boot Files

This directory contains everything required for the boot process except for configuration files not needed at boot time.

This directory contains everything required for the boot process except for configuration files not needed at boot time (the most notable of those being those that belong to the GRUB boot-loader) and the map installer. Thus, the /boot directory stores data that is used before the kernel begins executing user-mode programs. This may include redundant (back-up) master boot records, sector/system map files, the kernel and other important boot files and data that is not directly edited by hand. Programs necessary to arrange for the boot loader to be able to boot a file are placed in /sbin. Configuration files for boot loaders are placed in /etc. The system kernel is located in either / or /boot


/dev - Device Files

This directory is the location of special or device files.

It is a very interesting directory that highlights one important aspect of the Linux filesystem - everything is a file or a directory. Look through this directory and you should hopefully see hda1, hda2 etc.... which represent the various partitions on the first master drive of the system. The majority of devices are either block or character devices; however other types of devices exist and can be created. In general, 'block devices' are devices that store or hold data, 'character devices' can be thought of as devices that transmit or transfer data. For example, diskette drives, hard drives and CD-ROM drives are all block devices while serial ports, mice and parallel printer ports are all character devices. There is a naming scheme of sorts but in the vast majority of cases these are completely illogical.


/etc - Configuration Files

This directory contains all system related configuration files in here or in its sub-directories.

This is the nerve center of your system, it contains all system related configuration files in here or in its sub-directories. A "configuration file" is defined as a local file used to control the operation of a program; it must be static and cannot be an executable binary. For this reason, it's a good idea to backup this directory regularly. It will definitely save you a lot of re-configuration later if you re-install or lose your current installation. Normally, no binaries should be or are located here.


/home - Users Directories

Each user is assigned a specific directory that is accessible only to them and the system administrator. These are the user home directories, which can be found under this directory.

Linux is a multi-user environment so each user is also assigned a specific directory that is accessible only to them and the system administrator. These are the user home directories, which can be found under '/home/$USER' (~/). It is your playground: everything is at your command, you can write files, delete them, install programs, etc.... Your home directory contains your personal configuration files, the so-called dot files (their name is preceded by a dot). Personal configuration files are usually 'hidden', if you want to see them, you either have to turn on the appropriate option in your file manager or run ls with the -a switch. If there is a conflict between personal and system wide configuration files, the settings in the personal file will prevail. Almost always when a user is created their dotfiles will be taken from the /etc/skel directory where system administrators place a sample file that user's can modify to their hearts content. /home can get quite large and can be used for storing downloads, compiling, installing and running programs, your mail, your collection of image or sound files etc.


/lib - Essential Shared Libraries

This directory contains kernel modules and those shared library images (the C programming code library) needed to boot the system and run the commands in the root filesystem.

The /lib directory contains kernel modules and those shared library images (the C programming code library) needed to boot the system and run the commands in the root filesystem, ie. by binaries in /bin and /sbin. Libraries are readily identifiable through their filename extension of *.so. Windows equivalent to a shared library would be a DLL (dynamically linked library) file. They are essential for basic system functionality. Kernel modules (drivers) are in the subdirectory /lib/modules/'kernel-version'. To ensure proper module compilation you should ensure that /lib/modules/'kernel-version'/kernel/build points to /usr/src/'kernel-version' or ensure that the Makefile knows where the kernel source itself are located.


/lost+found - System Recovery Files

Linux should always go through a proper shutdown. Sometimes your system might crash or a power failure might take the machine down. Either way, at the next boot, a lengthy filesystem check (the speed of this check is dependent on the type of filesystem that you actually use. ie. ext3 is faster than ext2 because it is a journalled filesystem) using fsck will be done. Fsck will go through the system and try to recover any corrupt files that it finds. The result of this recovery operation will be placed in this directory. The files recovered are not likely to be complete or make much sense but there always is a chance that something worthwhile is recovered. Each partition has its own lost+found directory. If you find files in there, try to move them back to their original location. If you find something like a broken symbolic link to 'file', you have to reinstall the file/s from the corresponding RPM, since your file system got damaged so badly that the files were mutilated beyond recognition.


/media - Removable Media

Amid much controversy and consternation on the part of system and network administrators a directory containing mount points for removable media has now been created. Funnily enough, it has been named /media.


/mnt - Temporary Mount Points

Mounting is the process by which you make a filesystem available to the system. After mounting your files will be accessible under the mount-point. This directory usually contains mount points or sub-directories where you mount your floppy and your CD. You can also create additional mount-points here if you wish. Standard mount points would include /mnt/cdrom and /mnt/floppy. There is no limitation to creating a mount-point anywhere on your system but by convention and for sheer practicality do not litter your file system with mount-points. It should be noted that some distributions like Debian allocate /floppy and /cdrom as mount points while Redhat and Mandrake puts them in /mnt/floppy and /mnt/cdrom respectively.


/opt - Optional Packages

This directory is reserved for all the software and add-on packages that are not part of the default installation.


/proc - Kernal & Process Files

This is very special in that it is also a virtual filesystem. It's sometimes referred to as a process information pseudo-file system. It doesn't contain 'real' files but runtime system information (e.g. system memory, devices mounted, hardware configuration, etc). For this reason it can be regarded as a control and information centre for the kernel. In fact, quite a lot of system utilities are simply calls to files in this directory. For example, 'lsmod' is the same as 'cat /proc/modules' while 'lspci' is a synonym for 'cat /proc/pci'. By altering files located in this directory you can even read/change kernel parameters (sysctl) while the system is running. The most distinctive thing about files in this directory is the fact that all of them have a file size of 0, with the exception of kcore, mtrr and self.


/root - Home Directory of Admin User or 'root'

This may be somewhat confusing but in former days, '/' was root's home directory. To keep things tidier, 'root' got his own home directory. '/home' is often located on a different partition or even on another system and would thus be inaccessible to 'root' when - for some reason - only '/' is mounted.


/run - Application State Files

The /run directory is fairly new, and gives applications a standard place to store transient files they require like sockets and process IDs. These files can’t be stored in /tmp because files in /tmp may be deleted.


/sbin - System Admin Binaries

This should contain only binaries essential for booting, restoring, recovering, and/or repairing the system in addition to the binaries in /bin.


/tmp - Temporary Files

This directory contains mostly files that are required temporarily.

Many programs use this to create lock files and for temporary storage of data. Do not remove files from this directory unless you know exactly what you are doing! Many of these files are important for currently running programs and deleting them may result in a system crash. Usually it won't contain more than a few KB anyway. On most systems, this directory is cleared out at boot or at shutdown by the local system.


/usr - User Binaries & Read-Only Data

/usr usually contains by far the largest share of data on a system. Hence, this is one of the most important directories in the system as it contains all the user binaries, their documentation, libraries, header files, etc.... X and its supporting libraries can be found here. User programs like telnet, ftp, etc.... are also placed here. In the original Unix implementations, /usr was where the home directories of the users were placed (that is to say, /usr/someone was then the directory now known as /home/someone). In current Unices, /usr is where user-land programs and data (as opposed to 'system land' programs and data) are. The name hasn't changed, but it's meaning has narrowed and lengthened from "everything user related" to "user usable programs and data". As such, some people may now refer to this directory as meaning 'User System Resources' and not 'user' as was originally intended.


/var - Variables Data Files

Contains variable data like system logging files, mail and printer spool directories, and transient and temporary files.

Some portions of /var are not shareable between different systems. For instance, /var/log, /var/lock, and /var/run. Other portions may be shared

/srv :主要用来存储本机或本服务器提供的服务或数据。(用户主动生产的数据、对外提供服务)

/srv contains site-specific data which is served by this system.

/var :系统产生的不可自动销毁的缓存文件、日志记录。(系统和程序运行后产生的数据、不对外提供服务、只能用户手动清理)(包括mail、数据库文件、日志文件)

/var contains variable data files. This includes spool directories and files, administrative and logging data, and transient and temporary files. Some portions of /var are not shareable between different systems. For instance, /var/log, /var/lock, and /var/run. Other portions may be shared, notably /var/mail, /var/cache/man, /var/cache/fonts, and /var/spool/news. /var is specified here in order to make it possible to mount /usr read-only. Everything that once went into /usr that is written to during system operation (as opposed to installation and software maintenance) must be in /var. If /var cannot be made a separate partition, it is often preferable to move /var out of the root partition and into the /usr partition. (This is sometimes done to reduce the size of the root partition or when space runs low in the root partition.) However, /var must not be linked to /usr because this makes separation of /usr and /var more difficult and is likely to create a naming conflict. Instead, link /var to /usr/var. Applications must generally not add directories to the top level of /var. Such directories should only be added if they have some system-wide implication, and in consultation with the FHS mailing list.

/tmp :保存在使用完毕后可随时销毁的缓存文件。(有可能是由系统或程序产生、也有可能是用户主动放入的临时数据、系统会自动清理)

The /tmp directory must be made available for programs that require temporary files. Programs must not assume that any files or directories in /tmp are preserved between invocations of the program.


所以,服务器被用作Web开发时,html文件更应该被放在/srv/www下,而不是/var/www下(因为/srv目录是新标准中才有的,出现较晚;而且Apache将/var/www设为了web默认目录,所以现在绝大多数人都把web文件放在/var/www,这是个历史遗留问题)。

如ftp、流媒体服务等也应该被放在/srv对应的目录下。如果对应目录太大,应该另外挂载分区。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment