Linux: File System

In this article, we are discussing the Linux file system in detail. We are considering all the aspects of the file system.

NOTES

In Linux file system everything is considered as a file.

Extension of files is not as important as in other operating systems(like Windows). We can see files without any extension, everywhere in the file system.

Linux File System
Linux File System

The root of the Linux file system is denoted by a forward slash (/). so / this means the root of the file system, which contains all the files and directories.

NOTES

Linux files and directory names are case-sensitive. So be careful when you type any file and/or directory name.

If you go to the root(/) of the Linux file system and check the files and directories there, we will see a structure like this-

$ ls -la /

total 2097232

drwxr-xr-x  20 root root       4096 Aug  3 12:56 .
drwxr-xr-x  20 root root       4096 Aug  3 12:56 ..
lrwxrwxrwx   1 root root          7 Feb 16 18:37 bin -> usr/bin
drwxr-xr-x   4 root root       4096 Aug  3 13:00 boot
dr-xr-xr-x   2 root root       4096 Aug  3 12:44 cdrom
drwxr-xr-x  20 root root       4080 Aug  3 13:05 dev
drwxr-xr-x  96 root root       4096 Aug  3 13:17 etc
drwxr-xr-x   3 root root       4096 Aug  3 13:00 home
lrwxrwxrwx   1 root root          7 Feb 16 18:37 lib -> usr/lib
lrwxrwxrwx   1 root root          9 Feb 16 18:37 lib32 -> usr/lib32
lrwxrwxrwx   1 root root          9 Feb 16 18:37 lib64 -> usr/lib64
lrwxrwxrwx   1 root root         10 Feb 16 18:37 libx32 -> usr/libx32
drwx------   2 root root      16384 Aug  3 12:53 lost+found
drwxr-xr-x   2 root root       4096 Feb 16 18:37 media
drwxr-xr-x   2 root root       4096 Feb 16 18:37 mnt
drwxr-xr-x   2 root root       4096 Feb 16 18:37 opt
dr-xr-xr-x 190 root root          0 Aug  3 13:05 proc
drwx------   4 root root       4096 Aug  3 13:00 root
drwxr-xr-x  31 root root        880 Aug  3 13:16 run
lrwxrwxrwx   1 root root          8 Feb 16 18:37 sbin -> usr/sbin
drwxr-xr-x   6 root root       4096 Feb 16 18:52 snap
drwxr-xr-x   2 root root       4096 Feb 16 18:37 srv
-rw-------   1 root root 2147483648 Aug  3 12:56 swap.img
dr-xr-xr-x  13 root root          0 Aug  3 13:05 sys
drwxrwxrwt  12 root root       4096 Aug  3 13:17 tmp
drwxr-xr-x  14 root root       4096 Feb 16 18:37 usr
drwxr-xr-x  13 root root       4096 Feb 16 18:46 var
Bash

NOTES

File and directory names starting with dot(.) are considered as hidden. Directories for caching(like .cache, .ssh, etc.) and files for configuration(like .bashrc, .profile, etc.)

The files and directories are not shown by default when we use the “ls” command.

We have to pass option “-a” for all, “ls” to show the hidden files and directories, and use the command like – “ls -a

Here is the brief of what these directories refer to-

DirectoryRefers toContent of the Directory
/Root of the file system
/binBinariesApplications or programs like ls, cd, etc., and other binary files used by the system.
/bootSystem boot filesContains files that the operating system needs to boot. The boot loaders are in this directory.
/devDevicesContains files and directories to represent devices, like- disk, USB, CPU, keyboard, mouse, and other input/output devices.
/etcEt cetera (etc.)Contains configuration for different applications and system-wide configuration.
/homeUser homeHome directory for user. Files related to the user exists here.
/libLibrariesLibrary files required for running the applications(that stays in /bin and /sbin directory)
/mediaMounted drives (auto mount by OS)Used by the OS to mount USB, Dard drive, and other mounted drives.
/mntMounted drives (manual mount)Use this to manually mount USB, Dard drive, and other mounted drives.
/optOptionalContains manually installed software and drivers. Also, applications installed by some vendors are installed here.
/procProcessFiles containing information about different processes that run on the system.
/rootroot user homeHome directory for the root user(super use)
/runRuntime dataStore runtime stats for different processes
/sbinSystem binariesContains system binaries that the system administrators use.
Standard users do not have permission to these binaries, without proper permission.
/snapSnap packagesContains snap packages
/srvServiceServices like a Web server or FTP server-related data/files are stored here.
/sysSystemSystem-related files. Created every time the system boots (not written to the physical disk)
/tmpTemporaryTemporary files stored by different applications, during processing.
/usrUser ApplicationsContains programs or applications used by the user.
/varVariableContains files and directories that are expected to grow in size, over time. For example, log files.

Now let’s discuss the directories in detail-

Root Directory (/)

This is where the file system structure begins. All the other directories and files belong to the root. We can go to this location using the following command-

$ cd /
Bash

/bin

This directory contains the binary programs. The program/applications that we need to execute different commands (like ls, cd, rm, etc.) stays in this directory.

/boot

Kernel, boot loader, and the initial RAM disk image stay here. The most important file here is /boot/grub/grub.conf

/dev

Contains device nodes. A list of devices is maintained by the kernel.

/etc

Contains files for system wide configuration. Configurations are kept as readable(text) files for the system. Some shell scripts stays here, which are required during boot time.

Here are some significant files that we find in the /etc directory-

  • /etc/crontab : used from cronjob defination.
  • /etc/fstab : used for storage devices and mounting information.
  • /etc/passwd : used for user account information.

/home

By default when a user is created, a directory inside the /home directory is created for that user. And each user can go to it’s own home directory by using “cd ~”.

/lib

Contains the library files used by the core system.

/media

Used as a mounting point for media like- CD-ROM, USB, and other devices. These devices are mounted automatically by the system.

/mnt

Used as a mounting point for devices that are mounted manually.

/opt

Used to install optional software. Used by the commertial softwares installe on the machine.

/proc

Files in the/proc directory are created by the kernel, based on which processes are running in the system right now. These are readable files and has the information of relevent process.

/root

Home directory for the root user.

/sbin

Contains the binary files used by the system. These binary files are used by root user.

/tmp

Stores the temporary files used by the system and other programs. These temoprary files are deleted on system boot.

/usr

Contains the programs and related files used by the regular user.

/usr/bin

Contains executables installed by the linux distribution. These are the programs that do not usually come as with the distrubution.

/usr/lib

Shared library used by the program installed in /usr/bin.

/usr/local

Programs installed later which are used systemwide are contained here.

/usr/sbin

Contains application for administration of the system. These programs are used by the administrator in addtion to the system programs installed in /sbin directory.

/usr/share

Contains all shared data used by the programs /usr/bin and /usr/sbin

/var

Files which are intended to grow over time are contained in this directory. Like- log files, mail information, web service files etc. are contained in this file

/var/log

Log file for web server, database are added here. All types of logs – error log, access log, etc. can be found in this directory.

/var/www

File for the web applications are stored here. This is a predefined locaiton for web appliation files like html, javascript, python, ruby, php, etc.

Thought the web appication can be installed in any directory, and can be configured. But /var/www works as the default location for web appliation files.

File/Directory Links

When we explore the file system, we would encounter files which look different. For example running “ls -la” shows the list of files in some directory, and one of the files(in my case), looks like below-

lrwxrwxrwx  1 root root       13 Aug 14 22:05 html -> /var/www/html
lrwxrwxrwx  1 root root       10 Aug 14 22:04 newt -> /etc/newt/
lrwxrwxrwx  1 root root       18 Aug 14 21:54 nftables.conf -> /etc/nftables.conf
Bash

In the listing there are some files and/or directory, for which we notice something different-

  • First letter of the permission part has “l“, which indicates it is a link.
  • After the file/directory name there is an arrow and then another location.

These files/directories are different. Though it shows in the current location, but these are not actual file/directory.

These are link to file/directory which is in another location. i.e. the ‘html’ is link to the actual location ‘/var/www/html’.

These are called symbolic link or symlink.

Linking a file/directory can be done in 2 ways-

  • Hard link
  • Soft link(Symbolic link)

Hard Link

By default files in the Linux file system have a hard link to the given file name. When we create another hard link we just create a new dictionary item for that file.

  • Hard links can point to files only (not to directories).
  • If the original file or the linked file is deleted, we can still access the other remaining reference(original or link). So deleting one(original or link) does not affect the other one.
  • Hard links can only link to files that are in the same file system.
Linux File Hard Link
Linux File Hard Link

NOTES

The file content stays on the disk until all hard links(the original file name and other hard links) are deleted.

Until every hard link of that file is deleted all other stays as it is and keep pointing to the original file content.

Here is how we create a hard link-

# hard link for a file
$ ln /original/file/path /link/file/path
Bash

Soft/Symbolic Link

Symbolic links are used to some limitations of hard links. Here is how soft links behave-

  • Symbolic links point to a file or directory.
  • Deleting the symbolic link does not delete the actual file or directory.
  • If the original file or directory is deleted, the symbolic link still exists but becomes broken.
  • If the soft link is deleted, it will not affect the original file.
  • Soft links can link to files or directories which are in different file systems.

Here is how you create a soft/symbolic link-

# Symbolic link for a file
$ ln -s /original/file/path /link/file/path

# Symbolic link for a directory
$ ln -s /original/directory/path /link/directory/path
Bash

NOTES

In both cases we are using the “ln” command.

Only difference is the ‘-s‘ option. Using the ‘-s‘ option creates a soft link, and without the ‘-s‘ option it becomes a hard link.

Soft and Hard Link Comparison

Here is the comparison between soft/symbolic links and hard links-

CriteriaSoft/Symbolic LinkHard Link
Can point to a fileYesYes
Can point to directoryYesNo
Can point to file/directory in different partition(file system)YesNo
Link is deletedThe file can still be accessed by using the hard link.Generally do not affect the original file.
But can affect the original file, if this is the last and only link existing for that file content.
The original file is deletedThe original file is deletedThe file content can still be accessed by using the hard link.

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.