If you have recently switched to Linux from Windows, the lack of C Drive, D Drive, and such familiar naming conventions may confuse you. But fret not, because once you understand how the Linux directory structure is laid out, all your doubts will be clarified.
The Linux Directory Structure
The development of Linux was inspired by Unix hence it follows a similar file system hierarchy as Unix. You will also notice the resemblance in other Unix-based operating systems like macOS and BSD. The Linux directory structure is laid out like a tree with the root (/) directory being the trunk of the tree from which the rest of the directories branch out.
The directories contain mainly three types of files:
- General files: General files are ordinary files that comprise ASCII or binary data. Videos, images, documents, etc. are common examples of general files.
- Directory files: In Linux, directories are also classified as files that serve as a storage space for other file types and directories.
- Device files: You might be familiar with seeing an F: or G: drive created whenever you plug in a USB in Windows. Similarly, in Linux, USB and other hardware components are represented as /dev/device_id files.
Let’s learn about the different directories and their uses in Linux. We recommend you follow along by moving into the directories with the cd command and inspecting their contents with the ls command.
If you want to learn more about the Linux hierarchy system, you can make use of the man hier command which displays the manual page describing the Linux filesystem hierarchy in detail.
1. The Root Directory (/)
The root directory, represented by a forward slash (/), stores all the directories in Linux. If you cd into this directory and use the ls command, you will find a list of all the directories on your machine. The absolute path of every file passes through the root directory as it is the parent to all other directories.
The boot directory contains important files needed by the boot loader. The initial ram file system or initramfs is also stored here along with the kernel. You shouldn’t tinker with this directory on your primary machine. If you wish to experiment, feel free to do so in a virtual machine.
Linux treats everything as a file, and hardware is not an exception to this rule. The /dev directory contains special, virtual files representing hardware components like a mouse, keyboard, storage devices, etc., connected to your system.
The /etc directory contains vital system configuration files such as startup scripts, networking files, user account-related files, etc. You have to edit configuration files in the /etc directory to make any system-wide changes.
The /home directory stores an individual user’s home directory. If you’re switching from a Windows environment, you will find the /home directory akin to the C:/Users directory. It contains user-specific configurations inside each user’s directory.
The /bin directory contains system commands and other executable programs. The ls command that you use to list out the subdirectories along with many other useful commands is located within the /bin directory.
The /opt directory contains optional software packages to facilitate better compatibility of certain applications. When you install a third-party application that is not available in the official distribution repository, its software code gets stored in the /opt directory.
The /proc directory is a pseudo-filesystem containing information about processes and kernel parameters. It is populated with data during boot-up and is cleaned when you shut down your Linux machine.
The /proc directory is also home to system information such as memory usage, processor information, and so on.
The /tmp directory is used by the system and its applications to store temporary files. You can also store temporary data in this folder, but remember that the data will be deleted upon rebooting your system.
In Linux, every user has a separate home folder, and the root user is no exception to this. The /root directory is the home folder of the root user. You won’t be able to access this directory without superuser privileges.
The /usr directory contains most of the files, libraries, programs, and system utilities. The /bin folder is symbolically linked to /usr/bin. The same goes for the /sbin and /lib directories.
The /var directory is the storage space for system-generated variable files, and it includes logs, caches, and spool files. The data in /var isn’t automatically deleted, so sysadmins can collect and investigate system logs if need be.
When you connect any removable media device such as a USB thumb drive, CD, or DVD, Linux creates a subdirectory under /media where the contents of the device are laid out. This is usually done automatically by the system as soon as you plug the device in. When you remove the device, the system deletes the corresponding subdirectory.
The /mnt directory is used to mount storage devices in the system temporarily. However, some Linux distributions also use /mnt as a permanent storage solution. Unlike /mediathe storage device isn’t automatically mounted at /mnt by the system. Sysadmins have to manually mount a storage device and populate the file system table accordingly.
A library is a collection of pre-compiled code that executable binaries can use. In Linux, the /lib directory serves as the storage space for all libraries needed by the binaries in the /bin directory.
The /sys directory contains information about the various system components and drivers. It’s akin to /proc but structured differently. Sysadmins use /proc and /sys inter-changeably to collect data.
The /run directory logs system information since boot time. You can find information about the daemons that are running, logged-in users, and more. The data stored in the /run directory can give you an idea of how the system resources are being utilized since startup.
Configuring Files and Folders in Linux
Now that you understand what the Linux filesystem hierarchy looks like, you can get to configuring your Linux machine’s files and folders to build a personalized and more secure environment.
If you’re unsure how to modify files and folders on Linux, playing around with access permissions is a good place to start.