Linux Commands - Disk Commands

    • Commonly used flags:
      • -h : Display result in human readable format.
      • -T: Display the file system type.
      • df [fileName]: Display information about the file system containing the provided file name or directory
    • Example Command:
    • Commonly used flags:
      • -h: Human readable format
      • -s: Summarize i.e display one total for each argument.
      • -c: Prints grand total.
      • -L: Follows symbolic links and calculates the disk usage of target files or directories.
    • Example:
    • Commonly used flags:
      • -a: Display all available block devices
      • -p: Prints full device path for each device.
    • Example:
    • Commonly used flags:
      • -l: Lists the partition table of all attached system on your system.
      • p: Prints the partition table.
      • n: Creates a new partition table.
    • Example:
    • Commonly used flags:
      • -t: Type of filesystem
      • -V: Produce verbose output.
    • Example:
    • Commonly used flags:
      • -t: Type
      • —move: Move mounted tree to another place.
    • Example:

mount: It is used to mount a filesystem or a device to a specific directory making its content accessible to the user.Basic Syntax: mount [OPTIONS] DEVICE MOUNTPOINT

mount /dev/sdb1 /mnt
#view details
mount -l
#result
/dev/sdb1 on /mnt type ext4 (rw,relatime)

Note: Similarly to unmount we can use umount /mnt.

mkfs: It is used to build a linux filesystem on device partition or a specific device.Basic Syntax: mkfs [OPTIONS] DEVICE [SIZE]

#create ext4 filesystem in previously created partition table {/dev/sdb1}
mkfs -t ext4 /dev/sdb1
#result
mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 786432 4k blocks and 196608 inodes
Filesystem UUID: 5053f768-cdb0-40ca-829b-72f1d4e4e6aa
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

fdisk: It is used for managing disk partitions. It allows you to create, delete, modify, and display information about disk partitions on your system.Basic Syntax: fdisk [OPTIONS] DEVICE

fdisk /dev/sdb
#result
Command (m for help): **n**
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): **p**
Partition number (1-4, default 1): **1**
First sector (2048-10485759, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-10485759, default 10485759): **+3G**

Created a new partition 1 of type 'Linux' and of size 3 GiB.

Command (m for help): **p**
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectors
Disk model: VMware Virtual S
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: dos
Disk identifier: 0x83eb5a76

Device     Boot Start     End Sectors Size Id Type
/dev/sdb1        2048 6293503 6291456   3G 83 Linux

Command (m for help): **w**
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

#Note: All the user inputs are highlighted bold

lsblk: Lists information about block devices. The command prints all block devices (except RAM disks) in a tree-like format by default.Basic Syntax: lsblk [OPTIONS] [DEVICE...]

lsblk -p
#result
NAME                                  MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
/dev/loop0                              7:0    0 61.9M  1 loop /snap/core20/1328
/dev/loop1                              7:1    0 67.2M  1 loop /snap/lxd/21835
/dev/loop2                              7:2    0 53.3M  1 loop /snap/snapd/19361
/dev/loop3                              7:3    0 53.2M  1 loop /snap/snapd/19122
/dev/loop4                              7:4    0 63.5M  1 loop /snap/core20/1891
/dev/loop5                              7:5    0 91.9M  1 loop /snap/lxd/24061
/dev/sda                                8:0    0   20G  0 disk
├─/dev/sda1                             8:1    0    1M  0 part
├─/dev/sda2                             8:2    0  1.5G  0 part /boot
└─/dev/sda3                             8:3    0 18.5G  0 part
  └─/dev/mapper/ubuntu--vg-ubuntu--lv 253:0    0 18.5G  0 lvm  /
/dev/sr0                               11:0    1  1.2G  0 rom

du: It estimates file and directory space usage. It is used to check the size of individual files or directories.Basic Syntax: du [OPTIONS] [FILE...]

du -shc backupScript/ ca/ cust_data.sql
#result
4.0K    backupScript/
32K     ca/
204K    cust_data.sql
240K    total

df: It displays the amount of disk space available on the file system containing each file argument. If no any file name is given then it will show the space available on all currently mounted file system.Basic Syntax: df [OPTIONS] [FILESYSTEM...]

df -Th
#result
Filesystem                        Type      Size  Used Avail Use% Mounted on
udev                              devtmpfs  921M     0  921M   0% /dev
tmpfs                             tmpfs     194M  1.4M  192M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv ext4       19G  6.5G   11G  38% /
tmpfs                             tmpfs     966M     0  966M   0% /dev/shm
tmpfs                             tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs                             tmpfs     966M     0  966M   0% /sys/fs/cgroup
/dev/loop0                        squashfs   62M   62M     0 100% /snap/core20/1328
/dev/loop1                        squashfs   68M   68M     0 100% /snap/lxd/21835
/dev/loop2                        squashfs   54M   54M     0 100% /snap/snapd/19361
/dev/loop3                        squashfs   54M   54M     0 100% /snap/snapd/19122
/dev/loop4                        squashfs   64M   64M     0 100% /snap/core20/1891
/dev/loop5                        squashfs   92M   92M     0 100% /snap/lxd/24061
/dev/sda2                         ext4      1.5G  209M  1.2G  16% /boot
tmpfs                             tmpfs     194M     0  194M   0% /run/user/1000