Categories: File Management

Directory Hierarchy in Linux [Basic Guide]

The need to store information in objects that could later be accessed in an organized way comes from afar in operating systems. For this purpose, file systems were created. Each file system maintains a hierarchy of directories that defines its organization and utility, and in Linux it is no different. Basically in Linux everything is a file, which, depending on its type, can become a common file, a directory, a link, a socket, a driver, a device descriptor, etc.

In file systems, in general, objects are organized in a hierarchical manner and each object has a unique identification within a table.

As an example, let’s look at Microsoft’s Windows operating system: it organizes your files in a partition called the “C:” drive, and in it we have the Windows directory, which contains the operating system; the “Program Files” directory, which contains most of the installed products, and so on. The files are divided into different directories and subdirectories for reasons of affinity and organization. In the same way, we have an organization on Linux.

Identifying the objects of a file system in Linux is known as inode. It loads information about where the object is located on the disk, security information, date and time of creation and last modification, among others. When we create a file system on Linux, each device has a finite number of inodes that will be directly proportional to the number of files that this device can accommodate.

Basically, in Linux everything is a file, which, depending on its type, can become a common file, a directory, a link, a socket, a conductor, a device descriptor, etc.

Compared to Windows, the organization of directories in Linux is a bit more complex. The file system in Linux is like an upside-down tree. At the top of the Linux hierarchy is a root directory simply named root and identified as the “/” sign. Don’t confuse root directory with the root superuser.

The structure of the Linux file system is defined by a market standard called Filesystem Hierarchy Standard or FHS, created by the Linux community in 1994 and maintained by the Linux Foundation. The FHS is currently in version 3.0. Distributions are not required to follow this standard, but they understand the importance of locating standard files and directories.

The entire system directory structure is created under root:

Linux FHS

/

File system root directory;

/bin

They contain commands that can be used by users and the system administrator. They are required in single-user or single-user mode and may also contain commands that are used indirectly by some scripts. It contains executable files, such as: cat, chgrp, chmod, chown, date, dd, df, dmesg, echo, hostname, kill, ln, more, mount, mv , ps, pwd, rm, rmdir, sed , umount and uname;

/boot

Static files required for the system load. This is where the Kernel image, initramfs, and some Grub files are located. This directory contains everything needed to load the system, except for the configuration files and the boot manager. /boot includes the master boot sectors and sector map files.

/dev

Kernel abstraction containing the files for access by system devices, such as disks, cd-roms, pendrives, serial ports, terminals, etc. The files are descriptors that facilitate access to devices. This directory is a pseudo-filesystem, and does not exist on disk. Your content, for instance, has device descriptors such as /dev/sda, /dev/cdrom, etc.

/etc

Files required for system configuration. They are required to load the operating system. It contains important files such as: fstab, exports, passwd, shadow, group, hosts, hosts.allow, hosts.deny, inittab, ld.so.conf, mtab, profile, services, etc. It also contains configuration files for network interfaces.

Typically /etc has two subdirectories:

/etc/X11: Configuration files for the Linux graphical interface (X Window System);

/etc/skel: Skeletons of user configuration. The “template” files for users are located in the /etc/skel directory. The contents of this directory are replicated to the new users’ home directory when they are created on the system.

/home

It is usually in this directory where the users’ home directories are located. In these directories, each user’s profile and bash shell load scripts are located.

/lib

Library files essential to the system, used by /bin programs and Kernel modules. It is common to have a /lib [architecture] directory. On 64-bit processors, there is the /lib64 directory. On 32-bit processors, there must be a /lib32 directory.

/mnt

Empty directory used as a mounting point for devices on the machine. Used by the administrator to mount disks.

/media

Empty directory used as a mounting point for devices on the machine, such as cdrom, DVD, pendrives, etc.

/proc

Kernel and process information. It’s a pseudo-filesystem and doesn’t really exist on disk. In this directory are the descriptor abstractions of everything in the system’s Kernel. It is possible not only to read the data, but also to make changes to the behavior of the Kernel by altering the content of files in /proc. Making an abstraction from Windows, this directory would be the system’s “registry”.

/opt

Applications that are not from the Linux distribution are installed in this directory, such as third-party databases, CAD vector software, etc.

/root

Home directory of the root superuser. Depending on the distribution, it may be present or not;

/run

This directory contains system information since the last load. The files in this directory are deleted or reset at the start of the boot process. Files such as those that indicate the PID of the running process must reside in this directory.

/sbin

Essential system files, such as applications and utilities for machine administration. Normally only the superuser has access to these files, such as: fdiskm fsck, ifconfig, init, mkfs, mkswap, route, reboot, swapon and swapoff.

/tmpf

Temporary file directory. In some distributions this directory is mounted in memory. It is recommended that your content be deleted from time to time or with each reboot.

/usr

User-owned files and the second largest directory hierarchy in Linux. Its content can be distributed via the network to several Linux systems of the same distribution without any problem. It has a few subdirectories namely:

/usr/bin: Tools and auxiliary user commands, as well as programming interpreters, such as perl, python, etc.;

/usr/include: C language headers and libraries;

/usr/lib and/ usr/lib64: user application libraries;

/usr/libexec: binaries that are not normally executed by users;

/usr/local: hierarchy of directories for installing local applications on the system. It has several subdirectories such as bin, etc., include, lib, man, sbin, share and src;

/usr/sbin: contains tools that are not essential but are exclusive to system administration.

/usr/share: read-only files with independent architecture. They are files that can be shared between the same Linux distributions, regardless of the architecture. The man directory, for example, is where the command manuals reside and is located in /usr/share.

/usr/src: may contain program source code files.

/var

Directory where variable information about the system is stored, such as log files, e-mail files, etc. It has important subdirectories, namely:

/var/cache: maintains application cache information such as calculations, etc.;

/var/lib: maintains application status information;

/var/lock: maintains lock files that hold devices for the exclusive use of a particular application;

/var/log: maintains system log files, such as messages, lastlog, and wtm.

/var/mail: maintains users’ email account directories;

/var/opt: keeps application files variable;

/var/run: the functionality of this directory has been moved to /run.

/var/spool: maintains data from processes that maintain file queues, such as printing and email output;

/var/tmp: maintains temporary application files that need to be preserved between the system’s reboot

Hidden Files and Directories

To hide files and directories on Linux, simply rename your name, starting it with a dot “.”.

For example, when listing content from a folder, the “songs” directory is present:

$ ls music files

If we rename the music directory, starting with a” . “, it will no longer appear in the list of files and directories:

$ mv songs .musicas

The “mv” command is used to move, or rename files and directories.

When listing the folder, the “songs” directory will not appear:

$ ls file

Hidden files and directories will only appear in the list with the ls command “-a” option:

$ ls -a
. .. .songs file

Current Directory and Previous Directory

Every directory on Linux has two special, hidden files.

The first file is the dot “.

It is used to reference the current directory.

The second file is the dot dot “..

It is used to reference the previous directory.

Thus, these two special files are used to refer to system files and directories. It is called relative because the reference depends on the starting point, on the current directory.

In contrast, an absolute reference, or full path, is the reference to a file or directory based on the root directory “/”.

To know the current directory, you must use the “pwd” command.

Note the current directory:

$ pwd 
/home/carlacruz

If we want to list the contents of the directory, we can use the “ls” command:

$ ls bin public_html

To list the content of a file within the public_html directory, you can use the relative reference, starting from the current directory, using the file “.”:

$ cat ./public_html/file
This is a sample text file

Or use the absolute path:

$ cat /home/carlacruz/public_html/file
This is a sample text file

If we want to list the contents of the /home directory, we can also use the special file “..” that references the previous directory:

$ ls ../

Carlacruz Uiraribeiro Arthur

The special file “..” can also be used to browse directories with the “cd” command:

$ cd .. 

$ pwd
/home

$ cd .. 

$ pwd
/

Note that a space is needed between the command “cd” and the special file “..” to separate the command “cd” and the special file “..”.

Home Directory

Every user on Linux has a unique special directory, called the HOME directory.

It is in your HOME directory that each user can create, delete, move, and rename files and folders at will.

No other ordinary user of the system can access another user’s home directory. Only the root super user can access any directory on the system.

Special user profile and desktop configuration files, as well as their private access to other hosts and encryption keys, also reside in the home directory.

The Linux file hierarchy standard dictates that users’ HOME directory be created within the /home directory.

It is in the user account file /etc/passwd that the path to the users’ home directory is defined:

$ cat /etc/passwd | grep Carla Carla: 1000:1000:Carla Cruz: /home/carlacruz: /bin/bash

The $HOME variable holds the path to the HOME directory of the user logged in to the system:

$ echo $HOME 
/home/carlacruz

Linux maintains a shortcut to the directory HOME of the user connected using the tilde symbol “~”:

$ cd ~ 
$ pwd 
/home/carlacruz

In this way, the “~” path can be used to refer to the files and directories in the connected user’s HOME directory:

$ cat ~/public_html/file
This is an example text file. 

Conclusion

Knowing the directory hierarchy in Linux is essential for Linux/Unix administrators. Explore each of your directories, commands, and files.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Share
Published by
Uirá Endy Ribeiro

Recent Posts

Sudo command on Linux (privilege scale) [Basic Guide]

The sudo command on Linux executes a given command as if it were another user.…

2 years ago

SS command on Linux (investigate the network) [Basic Guide]

The ss command on Linux is extremely useful for investigating sockets, providing various information about…

2 years ago

Free Linux command (memory usage) [Basic Guide]

Free Linux command shows the amount of total memory in use and available, as well…

2 years ago

Linux while command (loop – while) [Basic Guide]

The shell has structures for testing conditions and executing certain program sequences several times (loop),…

2 years ago

Linux fstab file (disk mount setup) [Basic Guide]

The /etc/fstab file stores the configuration of which devices should be mounted and what is…

2 years ago

Netcat command on Linux (Swiss network knife) [Basic Guide]

The Netcat Command in Linux or nc is a utility used to do “almost anything”…

2 years ago

This website uses cookies.