According to the UNIX tradition,there is a root filesystem which starts at the slash (/) directory, and all filesystems or partitions can be found in a tree descending from there.
Other partitions, whether they're on the local machine or somewhere on the network, are mounted at various subdirectory points under that root filesystem.
The Linux Foundation administers the Filesystem Hierarchy Standard, the FHS, which specifies what the main directory should be, and what their purposes are.
Most Linux distributions try to respect the FHS, but none of them follow it exactly.
One reason being, that as new requirements develop, people sometimes make changes, and it takes time for that to migrate into the FHS, and be respected by other distributions as well.
Here is a list of the main directories which should be present under /:
Main Directories
Directory | In FHS? | Purpose |
---|---|---|
/ | Yes | Primary directory of the entire filesystem hierarchy |
/bin | Yes | Essential executable programs that must be available in single user mode |
/boot | Yes | Files needed to boot the system, such as the kernel, initrd or initramfs images, and boot configuration files and bootloader programs |
/etc | Yes | System-wide configuration files |
/home | Yes | User home directories, including personal settings, files, etc. |
/lib | Yes | Libraries required by executable binaries in /bin and /sbin |
/lib64 | No | 64-bit libraries required by executable binaries in /bin and /sbin, for systems which can run both 32-bit and 64-bit programs |
/media | Yes | Mount points for removable media such as CD’s, DVD’s, USB sticks etc. |
/mnt | Yes | Temporarily mounted filesystems |
/opt | Yes | Optional application software packages |
/proc | Yes | Virtual pseudo-filesystem giving information about the system and processes running on it; can be used to alter system parameters |
/sys | No | Virtual pseudo-filesystem giving information about the system and processes running on it; can be used to alter system parameters, is similar to a device tree and is part of the Unified Device Model |
/root | Yes | Home directory for the root user |
/sbin | Yes | Essential system binaries |
/srv | Yes | Site-specific data served up by the system; seldom used |
/tmp | Yes | Temporary files; on many distributions lost across a reboot and may be a ramdisk in memory |
/usr | Yes | Multi-user applications, utilities and data; theoretically read-only |
/var | Yes | Variable data that changes during system operation |
A system should be able to boot and go into single user, or recovery mode, with only the /bin, /sbin, /etc, /lib and /root directories mounted, while the contents of the /boot directory are needed for the system to boot in the first place.
Many of these directories (such as /etc and /lib) will generally have subdirectories associated either with specific applications or sub-systems, with the exact layout differing somewhat by Linux distribution. Two of them, /usr and /var, are relatively standardized and worth looking at.
Directories Under /usr
Directory | Purpose |
---|---|
/usr/bin | Non-essential binaries and scripts, not needed for single user mode; generally this means user applications not needed to start system |
/usr/include | Header files used to compile applications |
/usr/lib | Libraries for programs in /usr/bin and /usr/sbin |
/usr/lib64 | 64-bit libraries for 64-bit programs in /usr/bin and /usr/sbin |
/usr/sbin | Non-essential system binaries, such as system daemons |
/usr/share | Shared data used by applications, generally architecture-independent |
/usr/src | Source code, usually for the Linux kernel |
/usr/X11R6 | X Window files; generally obsolete |
/usr/local | Local data and programs specific to the host; subdirectories include bin, sbin, lib, share, include, etc. |
Directories Under /var
Directory | Purpose |
---|---|
/var/ftp | Used for ftp server base |
/var/lib | Persistent data modified by programs as they run |
/var/lock | Lock files used to control simultaneous access to resources |
/var/log | Log files |
/var/mail | User mailboxes |
/var/run | Information about the running system since the last boot |
/var/spool | Tasks spooled or waiting to be processed, such as print queues |
/var/tmp | Temporary files to be preserved across system reboot; sometimes linked to /tmp |
/var/www | Root for website hierarchies |
Under Linux, disks are divided into partitions; the term slices is not often used, but when it is, it is used interchangeably with the term partitions.
Up to four primary partitions can be created and information stored about them in the MBR (Master Boot Record).
More flexibility can be obtained by creating up to three primary partitions and an extended partition, which can contain as many logical partitions as can be accommodated, which may depend on the type of disk involved. For example, SCSI disks can have only up to sixteen partitions.
The Linux kernel discovers all pre-attached hard disks during system boot, and there is normally no configuration files required to inform about what is present. In hotplug situations, the udev system will find disks upon insertion in the system and read in their partition tables.
The command line utility for creating and examining hard disk partitions is fdisk; to see all currently attached device, you can do:
user@debian:~$ PATH="/sbin:$PATH"
user@debian:~$ command -v fdisk
/sbin/fdisk
user@debian:~$ fdisk -l
The fdisk utility can be used to create and remove partitions and change their type.
Note that fdisk does not allow you to move partitions or resize them. Resizing has to be done in two steps; if you are increasing, you have to increase the size of the partition, and then increase the filesystem size (for example, with resize2fs); if you are decreasing the size, you have to decrease the size of the filesystem and then the partition.
Partitions can be formatted for various filesystems with the mkfs command, or more usually, with specific commands for each type of filesystem. For example, either of the two following commands:
$ sudo mkfs -t ext4 /dev/sda10
$ sudo mkfs.ext4 /dev/sda10
will place an ext4 filesystem on /dev/sda10 with default options.
The gparted utility (and some equivalents) let you do all these operations in a graphical user-friendly manner.
Create a simple executable file with the name ls in your current directory, which we will assume to be /tmp:
$ cd /tmp
$ echo echo Hello, This is MY ls program > ls
$ chmod +x ls
You can run this directly by doing:
$ ./ls
but just typing ls will bring up the normal /bin/ls, which can be verified by typing which ls.
If you do:
$ export PATH=/tmp:$PATH
then typing ls will bring up your program no matter where you are sitting on the filesystem.
This is different than doing:
$ export PATH=./:$PATH
which puts the current directory first in the path, no matter where you are, or
which will put the current working directory at this time in your future path.
$ export PATH=$CWD:$PATH
Prepending your current directory to the path is generally a bad idea, as it makes trojan horses easy to implement.