Linux Directory Structure Explained!

Linux Directory Structure Explained!

How Files and Folders are organized in Linux!

In Linux everything is a File. When I say this I mean yes! Even a directory aka folder is also considered as a file at first place.

In this article I will demonstrate how Linux Operating System organizes files and directories.

Linux Directory Structure

A standard Linux distribution follows the directory structure known as Filesystem Hierarchy Standard (FHS) maintained by the Linux Foundation.

I love to deal with Linux or any Unix like servers using CLI. Here as well I will try to make you understand you the directory structure from my own system's terminal which is an Ubuntu 18.04 LTS server.

Linux_directory_structure.png

Here I have used tree command to list down the content of a directory in a tree-like format recursively.

You can install tree using following command and then list only the 1st Level of the directory tree starting at / -

# apt install tree -y
# tree -L 1 /
  • / (root) directory -->

This directory (denoted by a slash \) is called as “root.” The starting point for the file system hierarchy. Please note that this has nothing to do with the root superuser account. Everything in Linux resided under root directory even partitions. Only the root user has the right to perform write operations under this directory.

  • /bin directory -->

The /bin directory contains executable files. Many of them are required during single-user-mode (maintenance mode) or need to be used when performing file system repairing etc. For e.g. ls, cp, cat, echo, df etc.

bin_dir.png

  • /boot directory -->

All the files which are required during system boot process (static bootloader, kernel executable and configuration files) are placed here.

boot_dir.png

  • /dev directory -->

It contains device files for all the hardware devices part of your system e.g. cdrom, cpu, terminal devices, usb etc. Linux system treats devices also like files and you can read and write devices like they were files. These are not device drivers.

dev_dir.png

  • /etc directory -->

The /etc directory contains the core configuration files of the system, mostly plain text files required by all programs. Mostly System Administrators and DevOps folks will be dealing with these files only. These files includes username, password, network config, application specific config, system startup/shutdown files etc.

etc_dir.png

  • /home directory -->

This directory contains home directories for all users to store their personal files. When ever a new user is created, it will be assigned a directory with it's name under home directory.

home_dir.png

On my system I have two users named vagrant and lco_user. You can see their personal files and directories present under their respective home directories.

root user's home directory is /root.

  • /lib and lib64 directories -->

These directories contains shared library files that are required to boot the system. They are similar to DLL's on Windows.

/lib is for 32-bit compatibility libraries and /lib64 is for 64-bit libraries.

lib_lib64_dir.png

  • /lost+found directory --> This directory primarily used to store files that are found to be corrupted after a system crash and provide a way to try recover data from them. Each partition has its own /lost+found directory. It is used by file system check tools (fsck).

lost_found_dir.png

  • /media directory -->

Any external storage such as media/cdrom when plugged in will be automatically mounted under /media directory.

media_dir.png

  • /mnt directory -->

Directory where sysadmins can mount regular filesystems such as NFS etc. But not used very often nowadays.

mnt_dir.png You don't see anything here as no filesystem is mounted under /mnt on my system.

  • /opt directory -->

This directory contain any third party applications available from individual vendors. It is recommended to install third part applications under /opt or /opt sub directory.

opt_dir.png

  • /proc directory -->

This directory contains information about system processes with a particular process id or pid. Also known as a virtual/pseudo filesystem which has text information about system resources such as cpu/memory. Under this directory the files and directories gets generated as and when system starts or something changes on the system.

proc_dir.png

  • /root directory -->

Home directory for root user.

root_dir.png

  • /run directory -->

This directory acts as a temporary filesystem (tmpfs) which stores system processes volatile runtime data.

run_dir.png

  • /sbin directory -->

This directory contains system executables used for system administration for maintenance. It is similar to /bin, but it contains only the superuser required applications. For e.g. fdisk, fsck, reboot, shutdown, iptables.

sbin_dir.png

  • /snap directory -->

This directory contains the mount-points for your snaps and several symlinks which are needed by snapd.

snap_dir.png

  • /srv directory -->

This directory contains data for services provided by your server. For example you are hosting a web server on your Linux box, your web files for your site would go into /srv/http (or /srv/www). In case of FTP server it would go into /srv/ftp.

srv_dir.png

  • /sys directory -->

This is another virtual directory similar to /proc and /dev containing information from devices connected to your computer. It allows modification of the devices connected to the system.

sys_dir.png

  • /tmp directory -->

It contains temporary files. Many applications keep their temporary files here which contain data that an application might not need right now, but may need later on. These files will be kept until next boot or application restart.

tmp_dir.png

  • /usr directory -->

This directory contains applications, libraries, docs, icons, images and other files which needs to be shared by applications and services. It is basically shareable, read-only data.

usr_dir.png

There are further sub directories within /usr directory. /usr/bin :- Non-essential command binaries (not needed in single-user mode); for all users.

/usr/include :- Standard include files.

/usr/lib :- Libraries for the binaries in /usr/bin and /usr/sbin.

/usr/lib<qual> :- Alternative-format libraries (e.g., /usr/lib32 for 32-bit libraries on a 64-bit machine (optional)).

/usr/local :- Contains local data specific to this host. Typically has further subdirectories (e.g., bin, lib, share)

/usr/sbin :- Non-essential system binaries (e.g., daemons for various network services).

/usr/share :- Architecture-independent (shared) data.

/usr/src :- Source code (e.g., the kernel source code with its header files).

usr_sub_dirs.png

  • /var directory -->

This directory contains variable files such as log files, lock, mail, cache and temp files that changes constantly when the system is running and are expected to grow further.

var_dir.png

There are further sub directories within /var directory.

/var/cache :- Application cache data.

/var/lib :- State information. Persistent data modified by programs as they run (e.g., databases, packaging system metadata, etc.).

/var/lock :- Lock files. Files keeping track of resources currently in use.

/var/log :- Log files. Various logs.

/var/mail :- Mailbox files. In some distributions, these files may be located in the deprecated /var/spool/mail.

/var/opt :- Variable data from add-on packages that are stored in /opt.

/var/run :- Run-time variable data. This directory contains system information data describing the system since it was booted. In FHS 3.0, /var/run is replaced by /run.

/var/spool :- Spool for tasks waiting to be processed (e.g., print queues and outgoing mail queue).

/var/tmp :- Temporary files to be preserved between reboots.

var_sub_dir.png

That's all for this tutorial. By now you all will be very clear about how a Linux Operating System organizes files and which place meant for storing what kind of files. It is very crucial to know all this.

Hope you like the article. Stay Tuned for more.

Thank you. Happy learning!

Did you find this article valuable?

Support Learn Code Online by becoming a sponsor. Any amount is appreciated!