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.
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.
/boot
directory -->
All the files which are required during system boot process (static bootloader, kernel executable and configuration files) are placed here.
/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.
/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.
/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.
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
andlib64
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.
/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).
/media
directory -->
Any external storage such as media/cdrom when plugged in will be automatically mounted under /media
directory.
/mnt
directory -->
Directory where sysadmins can mount regular filesystems such as NFS etc. But not used very often nowadays.
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.
/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.
/root
directory -->
Home directory for root
user.
/run
directory -->
This directory acts as a temporary filesystem (tmpfs) which stores system processes volatile runtime data.
/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.
/snap
directory -->
This directory contains the mount-points for your snaps and several symlinks which are needed by snapd.
/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
.
/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.
/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.
/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.
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).
/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.
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.
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!