File systems in Linux are the way that the operating system organizes and stores data on a storage device, such as a hard drive or SSD. Understanding how file systems work and the commands used to interact with them is essential for managing and maintaining your Linux system.
There are several different types of file systems used in Linux, including the most commonly used file system, the ext4 file system. Each file system has its own set of commands and tools for managing the data stored within it.
In this blog post, we will cover some of the most commonly used file system commands in Linux. Whether you are a beginner or an experienced Linux user, this guide will provide you with the information you need to effectively manage and maintain your file systems.
There are several different file systems available for Linux. Below is the table that is self-explanatory.
File System | Description | Advantages | Disadvantages |
---|---|---|---|
Ext2, Ext3, Ext4 | Most commonly used file systems for Linux |
|
|
XFS | High-performance file system optimized for large-scale storage systems |
|
|
Btrfs | Modern copy-on-write file system with advanced features |
|
|
NTFS | File system used by Microsoft Windows |
|
|
FAT 16/32 | Legacy file system widely used on floppy disks and other removable media |
|
|
Each file system has its own strengths and weaknesses, and the choice of file system will depend on the specific requirements and constraints of the system. The key is to understand the trade-offs involved and choose the file system that best meets the needs of the system.
mkfs
(make file system)
The mkfs
command is used to create a new file system on a disk partition. It can be used to format a partition with any of the supported file systems, including Ext2, Ext3, Ext4, XFS, NTFS, and FAT16/32. The mkfs
command takes a number of options and arguments to specify the type of file system to be created, the size of the file system, and other parameters. The syntax for creating a new file system using mkfs
is as follows:
mkfs [options] [device]
Where device
is the name of the block device on which the file system is to be created, and options
are any additional options required to specify the type of file system to be created and the desired parameters for that file system. Some of the most commonly used options for mkfs
include:
Option | Description |
---|---|
-t | Specifies the file system type to be created, such as ext4 or XFS |
-m | Specifies the percentage of the file system space to reserve for the root user |
-c | Checks the device for bad blocks before formatting |
-L | Specifies a label for the file system |
-v | Enables verbose output, providing information about the progress of the format process |
You can read man page of mkfs for more details.
Here’s an example of how you could use the mkfs
command to format a block storage device with the ext4 file system and a label:
mkfs -t ext4 -L mylabel /dev/sdb1
df
(disk free)
The df
command displays information about disk space usage on different file systems. It provides information about the total size of a file system, the amount of disk space that is available, and the amount of disk space that is in use. The output of the df
command can be used to monitor disk usage and detect any potential disk space issues. The syntax for using the df
command is as follows:
df [options] [filesystem]
Where filesystem
is the name of the file system to be displayed, and options
are any additional options required to modify the output format. Some of the most commonly used options for the df
command include:
Option | Description |
---|---|
-h | Displays the output in human-readable format, using units like MB, GB, and TB instead of blocks |
-T | Displays the file system type for each file system |
-x | Excludes file systems of the specified type, such as tmpfs or proc |
-a | Includes all file systems, including pseudo, duplicate, and inaccessible file systems |
You can read more about options of df command on man page.
Here’s an example of how you could use the df
command to display information about the disk space usage on a Linux system in human-readable format:
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 5.5G 14G 29% /
tmpfs 3.9G 0 3.9G 0% /dev/shm
/dev/sdb1 40G 7.5G 32G 19% /mnt/data
The output of the df
command shows the file system name, total size, used space, available space, and the percentage of disk space used. In this example, the disk space usage of the root file system is displayed, as well as a tmpfs file system and a data file system mounted at /mnt/data
. This information can be useful in determining the disk space usage on a system, and can help you plan and manage disk space usage effectively.
du
(disk usage)
The du
command displays the amount of disk space used by files and directories. It provides information about the size of individual files and directories, and the total amount of disk space used by a file system. The du
command is often used to identify large files or directories that are consuming a significant amount of disk space. The syntax for using the du
command is as follows:
du [options] [directory]
Where directory
is the name of the directory to be displayed, and options
are any additional options required to modify the output format. Most commonly used options for the du
command include:
Option | Description |
---|---|
-h | Displays the output in human-readable format, using units like MB, GB, and TB instead of blocks |
-c | Displays a grand total of the disk usage in the end of the output |
-a | Displays disk usage for all files, not just directories |
-d | Specifies the maximum depth to follow symbolic links |
You can read more about option available for du command on man page.
Here’s an example of how you could use the du
command to display the disk usage of the current directory in human-readable format:
$ du -h
4.0K ./test1
8.0K ./test2
12K .
The output of the du
command shows the disk usage of each directory, in this case the test1
and test2
directories, as well as the current directory. The -h
option provides the output in human-readable format, making it easier to interpret the results.
lsblk
(list block devices)
The lsblk
command lists all available block devices and their associated information. It provides information about the name, size, type, and other properties of each block device, making it useful for managing and troubleshooting disk-related issues. The syntax for using the lsblk
command is as follows:
lsblk [options]
Where options
are any additional options required to modify the output format. Some of the most commonly used options for the lsblk
command include:
Option | Description |
---|---|
-o | Specifies which columns of information to display, such as NAME, SIZE, FSTYPE, etc. |
-l | Displays information about the block devices in a list format, without any hierarchy |
-p | Displays the disk partitions, including the file system type, start, and size |
-n | Displays information without attempting to resolve device names to more human-readable form |
You can read more about the available option on its man page.
Here’s an example of how you could use the lsblk
command to display information about all the block devices on a Linux system:
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 20G 0 part /
└─sda2 8:2 0 1M 0 part
sdb 8:16 0 40G 0 disk
└─sdb1 8:17 0 40G 0 part /mnt/data
sr0 11:0 1 56M 0 rom
The output of the lsblk
command shows the name of each block device, its major and minor numbers, the size, whether it is read-only, the type of device, and the file system type. In this example, two hard disk drives, sda
and sdb
, are shown, as well as a CD/DVD-ROM drive sr0
.
This information can be useful in determining the type and size of block devices on a system, as well as their mount points, and can help you manage storage resources effectively.
tune2fs
The tune2fs command is used to adjust various parameters of an ext2, ext3, or ext4 file system. It can be used to change the number of mount count before a file system is checked for consistency, the interval between two file system checks, the maximum mount count for which a file system will be checked for consistency, and many other parameters. The syntax for using thetune2fs` command is as follows:
tune2fs [options] [device]
Where device
is the name of the file system to be modified, and options
are any additional options required to specify the parameters to be changed. Some of the most commonly used options for the tune2fs
command include:
Option | Description |
---|---|
-l | Displays information about the file system, such as the volume name, block count, and reserved block count |
-c | Specifies the maximum mount count between file system checks. A value of 0 disables periodic checks |
-i | Specifies the interval (in days) between file system checks |
-m | Specifies the percentage of the filesystem which may be filled up before a check is run |
You can read more about its option on its man page.
Here’s an example of how you could use the tune2fs
command to display information about an ext4 file system:
$ tune2fs -l /dev/sda1
tune2fs 1.43.4 (31-Jan-2023)
Filesystem volume name: <none>
Last mounted on: /
Filesystem UUID: 1d23b85f-01e1-4d4c-adf6-ea3390fbbc80
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags: signed_directory_hash
Default mount options: (none)
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Block size: 4096
Fragment size: 4096
Reserved block count: 524288
Free blocks: 12033218
Free inodes: 819200
First block: 0
Block count: 31457280
Reserved GDT blocks: 1022
Blocks per group: 32768
Fragments per group: 32768
Inodes per group: 8192
Inode blocks per group: 512
Flex block group size: 16
Filesystem created: Wed Dec 6 16:24:53 2017
Last mount time: Tue Feb 8 10:24:49 2022
Last write time: Tue Feb 8 10:24:49 2022
Mount count: 20
Maximum mount count: -1
Last checked: Tue Dec 6 16:24:53 2016
Check interval: 0 (<none>)
Lifetime writes: 199 GB
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 256
Required extra ispace: 28
Desired extra ispace: 28
Journal inode: 8
Default directory hash: half_md4
Directory Hash Seed: 5f8c0e82-aee1-47b0-b74e-d2bb94bfc0e7
Journal backup: inode blocks
ls
ls
is a command in Linux and Unix-like operating systems that is used to list the files and directories in a directory.
Here are some commonly used options for the ls
command:
Option | Description |
---|---|
-l | Displays the files and directories in a long format, including information such as permissions, ownership, size, and modification time |
-a | Shows hidden files and directories |
-h | Displays file sizes in a human-readable format (e.g., 1K, 234M, 2G) |
-r | Displays the files and directories in reverse order |
-t | Sorts the files and directories by modification time, with the most recently modified files and directories appearing first |
You can read the man page to know more about available options.
Here’s an example of how you could use the ls
command to list the files and directories in a directory in long format:
$ ls -l
total 16
drwxrwxr-x 2 user1 user1 4096 Feb 8 10:24 dir1
drwxrwxr-x 2 user2 user2 4096 Feb 9 11:13 dir2
-rw-rw-r-- 1 user1 user1 12K Feb 7 08:36 file1.txt
-rw-rw-r-- 1 user2 user2 9K Feb 8 09:23 file2.txt
In this example, the ls
command is used to list the files and directories in the current directory in long format. The output shows that there are two directories (dir1
and dir2
) and two files (file1.txt
and file2.txt
) in the directory. The columns in the output show information such as permissions, ownership, size, and modification time.
You can read Linux File Permission to understand it in more deep.
mkdir
mkdir
is a command in Linux and Unix-like operating systems that is used to create a new directory. The syntax for the mkdir
command is as follows:
$ mkdir [OPTIONS] directory_name
Where directory_name
is the name of the directory that you want to create, and OPTIONS
are any optional parameters that you want to use.
Here are some commonly used options for the mkdir
command:
Option | Description |
---|---|
-p | Creates parent directories as needed. This means that if the directory that you want to create is nested within other directories, those directories will be created if they do not already exist. |
-m | Changes the permissions on the newly created directory. You can specify the permissions using a numeric mode or a symbolic mode (e.g., 777 or rwxrwxrwx ). |
-v | Displays verbose output, showing each directory that is created. |
Please read the man page for more details about the options available for mkdir.
Here’s an example of how you could use the mkdir
command to create a new directory called newdir
:
$ mkdir newdir
In this example, the mkdir
command is used to create a new directory called newdir
. No output is displayed because the command ran successfully. To verify that the directory was created, you can use the ls
command to list the contents of the current directory:
$ ls
newdir
In this case, the output shows that the newdir
directory has been created and is now listed as a file in the current directory.