Introduction To Linux File System
What Is It?
File system is basically a system software component to help users to manage data on their storages. File system is the foundation of your Operating System. Pretty much everything is stored on your file system. Getting familiar with Linux file system is one of the first steps towards learning Linux.
How Does It Work?
You can think of the file system stack as three layers from top to bottom:
- File system: File system defines a specific format on a block device. The format allows us to create and delete files on it.
- Block device: This is the virtual representation of a hardware device in Linux kernel.
- Hardware storage: This is the real hardware storage. Typically, a hardware storage is a disk, but it could also be memory.
On Linux, you can use
lsblk command to list your block devices:
swe@ubuntu-server:~$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT loop0 7:0 0 63.3M 1 loop /snap/core20/1778 loop1 7:1 0 141.4M 1 loop /snap/docker/2285 loop2 7:2 0 55.6M 1 loop /snap/core18/2667 loop3 7:3 0 55.4M 1 loop /snap/core18/2066 loop4 7:4 0 131.6M 1 loop /snap/docker/796 loop5 7:5 0 67.6M 1 loop /snap/lxd/20326 loop6 7:6 0 49.6M 1 loop /snap/snapd/17883 loop7 7:7 0 49.8M 1 loop /snap/snapd/17950 loop8 7:8 0 91.9M 1 loop /snap/lxd/24061 sda 8:0 0 32G 0 disk ├─sda1 8:1 0 1M 0 part └─sda2 8:2 0 32G 0 part / sr0 11:0 1 4M 0 rom sr1 11:1 1 1024M 0 rom swe@ubuntu-server:~$
In the above example,
/sda is my physical storage disk. You can also use
fdisk command to check the details of that block device or even update it:
swe@ubuntu-server:~$ sudo fdisk -l /dev/sda Disk /dev/sda: 32 GiB, 34359738368 bytes, 67108864 sectors Disk model: QEMU HARDDISK Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: gpt Disk identifier: DEED7D2C-0ED2-409D-8019-83AA10EDE0D6 Device Start End Sectors Size Type /dev/sda1 2048 4095 2048 1M BIOS boot /dev/sda2 4096 67106815 67102720 32G Linux filesystem swe@ubuntu-server:~$
A block device can be mounted to a certain directory.
swe@ubuntu-server:~$ df -lhT Filesystem Type Size Used Avail Use% Mounted on udev devtmpfs 1.9G 0 1.9G 0% /dev tmpfs tmpfs 394M 1.1M 393M 1% /run /dev/sda2 ext4 32G 8.0G 22G 27% / tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/loop0 squashfs 64M 64M 0 100% /snap/core20/1778 /dev/loop1 squashfs 142M 142M 0 100% /snap/docker/2285 /dev/loop3 squashfs 56M 56M 0 100% /snap/core18/2066 /dev/loop2 squashfs 56M 56M 0 100% /snap/core18/2667 /dev/loop4 squashfs 132M 132M 0 100% /snap/docker/796 /dev/loop5 squashfs 68M 68M 0 100% /snap/lxd/20326 /dev/loop6 squashfs 50M 50M 0 100% /snap/snapd/17883 /dev/loop7 squashfs 50M 50M 0 100% /snap/snapd/17950 /dev/loop8 squashfs 92M 92M 0 100% /snap/lxd/24061 tmpfs tmpfs 394M 0 394M 0% /run/user/1001 tmpfs tmpfs 394M 0 394M 0% /run/user/1000
The above command suggests that my
/dev/sda2 is mounted to my root directory.
Note that another block can be mounted to a sub-directory, even if that sub-directory belongs to directory that another device mounts to. For example, in my case,
/dev/shm is mounted to the memory (which is typically called ramdisk).
File System Type
Linux supports a few different file system formats, e.g., ext4, NTFS, FAT32. ext4 is the most commonly used file system type today. The above example device
/dev/sda2 is formated as ext4.
Linux System Directories
If you go to your root directory and do an
ls, you’ll see something similar to this
swe@ubuntu-server:/$ ls bin boot cdrom dev etc home lib lib32 lib64 libx32 lost+found media mnt opt proc root run sbin snap srv swap.img sys tmp usr var swe@ubuntu-server:/$
These directories belong to your Linux system. Here are what they do.
|This is the directory for your executable binaries.|
|This directory contains the information needed for Linux bootup, e.g., grub files.|
|This is a virtual directory, which includes your device files.|
|This directory includes a lot of the Linux configuration files, e.g., your apt source repos.|
|This is where you’ll find your and others’ home directories are in.|
|This is the library directory.|
|This is a directory where your system will automatically mount external storage devices to.|
|This is typically where people manually mount storage devices to.|
|This is reserved for the installation of add-on application software packages.|
|This is also a virtual directory, just like the |
|This is the home directory for the root user.|
|This is a directory where some processes dump data to.|
|This is similar to the |
|This is a directory for servers.|
|This is another virtual directory. It contains information from devices connected to your computer.|
|This directory is for temporary files.|
|This directory includes user related data, e.g., applications, libraries, documentation, wallpapers, icons.|
For basic file system operations, please check out commands in this section.