Sunday, January 5, 2020




Understanding Linux Directory Structure for Beginners

I have mostly been working on windows servers/environments. Now, moving to cloud, I felt that I now need to get hold of the other side of the world too and hence I am now learning the Linux too. I thought to share my journey with you all so that, if anyone of you there, like me, can be benefitted.

To start with my first doubt was to understand the Linux directory structure. Thus, this tutorial explains the Linux directory structure. You’ll learn the Linux filesystem hierarchy along with the purpose of the various directories on a Linux system. If you are even faintly acquainted with Linux, you might have heard the terms root, lib, bin etc. These are various directories that you’ll find in all Linux distributions. This is possible because the Linux Foundation maintains a Filesystem Hierarchy Standard (FHS). This FHS defines the directory structure and the content/purpose of the directories in Linux distributions. Thanks to this FHS, you’ll find the same directory structure in (almost) all the Linux distributions.

Let’s see the Linux directory structure in detail. Since Linux is based on UNIX and hence it borrows its filesystem hierarchy from.
/ – The root directory
Everything, all the files and directories, in Linux are located under ‘root’ represented by ‘/’. If you look at the directory structure, you’ll realize that it is similar to a plant’s root.




Below is the Linux Directory Structure. When first logged into the server with your login id you land up under /home/user directory, you can find out the current working directory by pwd (present working directory). You need to go to the root to find all the directories in the server.

Go to root i.e., cd / and then ls -ltar to see the list of directory

Since all other directories or files are descended from root, the absolute path of any file is traversed through root. For example, if you have a file in /home/user/documents, you can guess that the directory structure goes from root->home->user->documents.

The directory listing is very colourful, isn’t it? What does these colour signifies? Well these colours differentiates directories, files etc., The colours can be changed and you can define your own colours too, which is a different topic for a another day. The basic colours and what they represent are defined below.
·         Blue: Directory
·         Green: Executable or recognized data file
·         Sky Blue: Symbolic link file
·         Yellow with black background: Device
·         Pink: Graphic image file
·         Red: Archive file
·         Red with black background: Broken link

Now back to directories/files
/dev – Device files
This directory only contains special files, including those relating to the devices. These are virtual files, not physically on the disk.
Some interesting examples of these files are:
/dev/null: can be sent to destroy any file or string
/dev/zero: contains an infinite sequence of 0
/dev/random: contains an infinite sequence of random values

/etc – Configuration files

The /etc directory contains the core configuration files of the system, used primarily by the administrators and services, such as the password file and networking files. If you need to make changes in system configuration (for example changing the hostname), this is where you’ll find the respective files.

/usr – User binaries and program data
in ‘/usr’ go all the executable files, libraries, source of most of the system programs. For this reason, most of the files contained therein is read­only (for the normal user)
‘/usr/bin’ contains basic user commands
‘/usr/sbin’ contains additional commands for the administrator
‘/usr/lib’ contains the system libraries

/home – User personal data
Home directory contains personal directories for the users. The home directory contains the user data and user-specific configuration files. As a user, you’ll put your personal files, notes, programs etc in your home directory. Every user for your Linux system should have a home directory like /home/partha, but only the user should have access to his/her home, others will get access denied.

/lib – Shared libraries
Libraries are basically codes that can be used by the executable binaries. The /lib directory holds the libraries needed by the binaries in /bin and /sbin directories. Libraries needed by the binaries in the /usr/bin and /usr/sbin are located in the directory /usr/lib.

/sbin – System binaries
This is similar to the /bin directory. The only difference is that is contains the binaries that can only be run by root or a sudo user. You can think of the ‘s’ in ‘sbin’ as super or sudo.

/tmp – Temporary files
As the name suggests, this directory holds temporary files. Many applications use this directory to store temporary files. Even you can use directory to store temporary files. Please note the files/contents of the /tmp directories are deleted when you restart your system so don’t store anything important here.

/var – Variable data files
Var, short for variable, is where programs store runtime information like system logging, user tracking, caches, and other files that system programs create and manage. The files stored here are NOT cleaned automatically and hence it provides a good place for system administrators to look for information about their system behavior. For example, if you want to check the login history in your Linux system, just check the content of the file in /var/log/wtmp.

/boot – Boot files
The ‘/boot’ directory contains the files of the kernel and boot image, in addition to LILO and Grub. It is often advisable that the directory resides in a partition at the beginning of the disc.

/proc – Process and kernel files
The ‘/proc’ directory contains the information about currently running processes and kernel parameters. The content of the proc directory is used by a number of tools to get runtime system information. For example, if you want to check processor information in Linux, you can simply refer to the file /proc/cpuinfo. You want to check memory usage of your Linux system, just look at the content of /proc/meminfo file.

/opt – Optional software
Traditionally, the /opt directory is used for installing/storing the files of third-party applications that are not available from the distribution’s repository. The normal practice is to keep the software code in opt and then link the binary file in the /bin directory so that all the users can run it.

/root – The home directory of the root
There is /root directory as well and it works as the home directory of the root user. So instead of /home/root, the home of root is located at /root. Do not confuse it with the root directory (/).

/media – Mount point for removable media
When you connect a removable media such as USB disk, SD card or DVD, a directory is automatically created under the /media directory for them. You can access the content of the removable media from this directory.

/mnt – Mount directory
This is similar to the /media directory but instead of automatically mounting the removable media, mnt is used by system administrators to manually mount a filesystem.

/srv – Service data
The /srv directory contains data for services provided by the system. For example, if you run a HTTP server, it’s a good practice to store the website data in the /srv directory.

I will keep publishing my Linux learning series here. The above directory description is enough to head start your journey.

If you have queries, do drop in your queries below.
...HaPpY CoDiNg
Partha 

References: