Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a Linux file tree?
3.
What is FHS?
4.
FHS file structure
4.1.
Root Directory
4.2.
Binary Directories
4.3.
Configurational Directories
4.4.
Data Directories
4.5.
Memory Directories
5.
Linux Tree Command
5.1.
What is Linux Tree Command?
5.2.
Uses of the tree command
6.
Frequently Asked Questions
6.1.
How to know the file hierarchy followed by my system?
6.2.
What is the difference between /bin and /usr/bin?
6.3.
Which directory contains every single file and directory of the system?
6.4.
How to know the size of all the directories in the system individually?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Linux File Tree

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In this article, we will learn about the file structure of Linux. This blog would help us understand how the system stores files in Linux and where we can find some particular files. Here we will also cover the most widely used standard directories in the Linux file tree.

Introduction

So, let us begin our journey of exploring the Linux file system with the Linux file tree. Let's see what the Linux file tree is.

What is a Linux file tree?

Linux file tree is the hierarchy structure Linux follows to store files. Every Operating System has its ways and standard protocols for storing files. In the same way, Linux also has some standard protocols to store and manage files in it. To store files, Linux follows Linux File Hierarchy Structure or the Filesystem Hierarchy Standard (FHS).

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is FHS?

FHS

FHS stands for Filesystem Hierarchy Standard. It is a standardized system for Linux/UNIX-like operating systems that they follow to define the file directory structure and its contents. There can be some differences in the filesystems of different Linux distributions. So, to get information about the file system hierarchy followed in your Linux OS, you can use the command “man hier”. This command will open a manual that will give details about the directory structure followed on your computer.

FHS file structure

FHS has a directory structure that starts with a root directory, “/”, in all Linux systems. We can group the directories of the FHS file structure into five types of directories:

  1. Root Directory 
     
  2. Binary Directories
     
  3. Configurational Directories
     
  4. Data Directories
     
  5. Memory directories

Root Directory

The root directory is the primary hierarchical root of the Linux file system. It is represented by a forward slash, “/”. You can find every single file and directory below the root directory. Only the root user has permission to write under this directory. The directories we generally see inside the root directory are:

Root directory

Binary Directories

The files that contain pre-compiled Machine Code or Source code are called binaries. Since binaries are compiled and ready for execution, sometimes they are also referred to as executable files. The binary directories are:

  • /bin: This directory contains standard system utilities that must be available for all users. Example: ls, cat, grep, cp, echo, etc.
/bin
  • Other /bin directories: Many other directories contain /bin sub-directory. Example: A user named shubham can put his programs in the directory /home/shubham/bin.
     
  • /sbin: The binaries required to configure Operating System are stored in sbin. Many of the system binaries require boot permissions to perform certain tasks. So these are typically used by the system administrator for the maintenance of the system.
/sbin
  • /lib: This contains shared libraries essential for binaries found in /bin and /sbin.
/lib
  • /opt: This contains the optional application software packages. Generally, contains different software packages from individual vendors. It may be found to be empty in many systems.
     

Configurational Directories

The configuration directory contains configuration files that set the parameters and initial settings for the computer programs. Configurational directories include:

  • /boot: It is the storage space for all the files required for booting the system. These are rather fixed and do not change very often. The configuration file /boot/grub/grub.cfg inside the grub sub-directory of the boot directory defines the boot menu displayed before the kernel starts.
     
  • /etc: This contains the configuration files and administrative files required by all programs. Most of the time, the name of the configuration file is the same as that of the application, protocol, or daemon, just by adding .config as an extension.
/etc
  • Some important etc subdirectories are:
    • /etc/init.d: This directory contains scripts required to start and stop daemons.
       
    • /etc/X: It contains the configuration files of the graphical display, which is driven by the software from X.org.
       
    • /etc/skel: This is the skeleton directory that usually stores hidden files like the .bashrc script. Whenever a user is newly created, it is copied to the home directory of that user.
       
    • /etc/sysconfig: This is found only in Red Hat Enterprise Linux Operating Systems and contains the configuration files of that system.
       

Data Directories

Data directories are a group of directories that store data about the user or the devices connected to the system. The data directories are the following:

  • /home: This contains users’ home directories. Implying here user stores his personal data and saved files. The home directory of the user also stores the user profile. The general nomenclature for the users’ home directory is /home/$USERNAME. For example,
/home
  • /root: This directory is the home directory of the root user. So, it stores all the personal data and profile information of the root user.
     
  • /srv: This contains site-specific data served by your system. This includes scripts and data for the Web servers, data of the FTP servers, and repositories for version control systems.
         
  • /media: This serves as the mount point for removable devices.
     
  • /mnt: This is generally empty and is only used as a temporary mount point.
     
  • /tmp: This is the directory for temporary files. Users and applications use it to store temporary data when needed. When the system is rebooted, files under this directory get deleted.

Memory Directories

These directories store files of the whole system. Information about all the processes running and all the device information is contained in the memory directories. The Memory Directories include

  • /dev: This is the directory of essential device files. They include usb, terminal devices, or any other device attached to the system. Other than the physical hardware, they also represent some special device files.
/dev
  • /dev/tty and /dev/pts: /dev/tty1 represent the terminal attached to the computer system. When using a graphical interface terminal is represented by /dev/pts/.
     
  • /dev/null: It has unlimited storage, but nothing can be retrieved from it, just like a black hole.
     
  • /proc: This directory acts as a virtual filesystem to provide kernel and process information as files.
     
  • /sys: This directory was specially created for the Linux 2.6 kernel. It contains kernel information about the system hardware.
     
  • /usr: usr stands for Unix System Resources but is generally pronounced as user. It contains shareable data, and it is read-only in nature.
/usr
  • /usr/bin: Application binaries that are to be accessed by locally logged-in users.
     
  • /usr/include: This directory contains “include” files for C/C++.
     
  • /usr/lib: This directory contains libraries that are not directly executed by scripts or users.
     
  • /usr/local: This is used by the administrator to install software locally.
     
  • /usr/src: This is storage for kernel source files.
     
  • /var: var stands for variable data. The file is generally unpredictable in size, such as log files.
/var
  • Some important subdirectories of /var are,
    • /var/log: This contains all the log files of the system.
       
    • /var/cache: This contains cache data of different applications.
       
    • /var/spool: This directory contains spool directories for cron and mail.
       
    • /var/lib: This directory contains information about the application state.

Linux Tree Command

In this sub-section, we will discuss the Linux tree command, what it is, how it is useful, how to execute it, what variations it has, and everything else.  

What is Linux Tree Command?

Tree command in Linux is a command-line program that is used to recursively list the contents of a directory in a tree-like structure. It features many options for output manipulations. This command is available for almost all Linux-based systems, but if it is not installed by default, it can be installed easily using the respective package installer. For example,

Command:

$ sudo apt install tree

Uses of the tree command

We use the tree command without any argument to list all the directories in a tree-like format. It is used to get the contents of the directory currently being used and recursively show all the sub-directories and files. In the end, we also get a summary of the total number of sub-directories and files. 
Command: 
$ sudo tree 

  • To print hidden files, we use -a.
    $ sudo tree -a
     
  • To list the contents with full path prefixes, we use -f.
    $ sudo tree -f
     
  • To print only the sub-directories and not the files in them, we use -d.
    $ sudo tree -d
     
  • The maximum display depth of the tree can be fixed using -L. For example
    $ sudo tree -L 2
     
  • To display only those files which follow a particular pattern, we use -P. For example
    $ sudo tree -P test
     
  • To prune empty directories from the output, we use --prune.
    $ sudo tree --prune
     
  • To print the file type and permissions for each file, we use -p.
    $ sudo tree -p
     
  • Along with the name to print the size of each file, we use -s. If we do not want the size in bytes, we can also use -h and the system will specify a size letter for kilobytes (K), megabytes (M), gigabytes (G), terabytes (T), etc.
    $ sudo tree -s
     
  • If the size of each sub-directory as the sum of the sizes of all its files is required, we use --du.
    $ sudo tree --du
     
  • To display the last modification time of each file and sub-directory, we use -D.
    $ sudo tree -D

Check out this article - File System Vs DBMS

Frequently Asked Questions

How to know the file hierarchy followed by my system?

To know the file hierarchy followed in your computer system, use the command “man hier”. This command will give a description of the filesystem hierarchy followed in your system.

What is the difference between /bin and /usr/bin?

/bin contains executables for standard system functioning at any run level, whereas /usr/bin contains application executables that are to be accessed by locally logged-in users.

Which directory contains every single file and directory of the system?

The root directory, “/” contains all the files and the directories of the system. Below this, you can find everything.

How to know the size of all the directories in the system individually?

To know the size of all the directories in the system, you can use the command “sudo tree --du”, this will tell the size in bytes. To make it easier to understand, we can use “sudo tree --du -h” it will give output in proper format and use K for kilobyte, M for megabyte, and so on.

Conclusion

In this article, we had a deep discussion about the Linux file tree and the hierarchy followed by the operating system in storing your files. We also learned about the tree command of Linux and its wide application. 

We hope that this blog increased your knowledge about the Linux file tree system and the tree command. For more about Linux, go to Linux OSTypes of UNIX OS, and Intro to Linux Shell.

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Keep learning, and Keep Growing!

Happy Learning!

Previous article
Linux Kernel
Next article
wq Command in Linux
Live masterclass