How To Monitoring Disk Space In Linux

Cyberwizardy23
4 min readDec 19, 2022

Monitoring Disk Space In Linux:

As linux user we need to keep track of the disk usage on the system. Whether we’re running a simple linux desktop or a large Linux server. some command line commands can help us to manage the media environment on our linux system.

Mounting media:

The linux filesystem comabines all media disks into a single virtual directory. Before we use a new media disk on our system, we must place it in the virtual directory. This task is called mounting.

In today’s graphical desktop world, most Linux distribution have the ability to automatically mount specific types of removable media.

The mount command:

The command used to mount media is called mount. By default, the mount command displays a list of media devices currently mounted on the system:

┌──(me㉿kali)-[~/Desktop/medium_blogs]
└─$ mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
udev on /dev type devtmpfs (rw,nosuid,relatime,size=3992744k,nr_inodes=998186,mode=755,inode64)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,noexec,relatime,size=805872k,mode=755,inode64)
/dev/sda2 on / type ext4 (rw,relatime,errors=remount-ro)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,inode64)
tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k,inode64)

The mount command provides four pieces of information:
1) The device filename of the media
2) The mount point in the virtual directory where the media is mounted
3) The filesystem type
4) The access status of the mounted media

For example if we take:

/dev/sda2 on / type ext4 (rw,relatime,errors=remount-ro)

here, 1) sda2 is filename of media, 2) /dev/sda2 is mount point, 3) ext4 is filetype and 4) rw in () is read-write which shows status.

To manually mount a media device in the virtual directory, we must be logged in as root user or use sudo command to run the command as root user.

syntax:

sudo mount -t type device directory # non root user

mount -t type device directory # root user

The type parameter defines the filesystem type under which the disk was formatted. Linux recognize lots of different filesystem types. If you share removable media device with our windows PCs this devices run into these types:

vfat: Windows long filesystem
ntfs: Windows advanced filesystem used in Windows NT, XP, and Vista
iso9660: The standard CD-ROM filesystem

Most USB memory sticks and floppies are formatted using the vfat filesystem. The next two parameters define the location of the device file for the media device and location in virtual directory for the mount point.

For example, to manually mount the USB memory at device /dev/sdb1 at location /media/disk, using following command:

mount -t vfat /dev/sdb1 /media/disk

After a media device is mounted in the virtual directory, the root user has full access to the device, but access by other users is restricted.

The mount Command Parameters

Parameter : → Description
-a : → Mounts all filesystems specified in the /etc/fstab file
-f : → Mounts all filessystems at the same time when used with the -a parameter.
-v : → Explains all the steps required to mount the device; stands for verbose mode
-r : → Mounts the device as read-only
-w : → Mounts the device as read-write
-o : → Adds specific options to the filesystem
-U uuid : → Mounts the device with the specified uuid

The -o option allows you to mount the filesystem with a comma-separated list of additional options. These are popular options to use:

  1. ro: → Mounts as read-only
    2)rw: → Mounts as read-write
    3)user: → Allows an ordinary user to mount the filesystem
    4)check=none: → Mounts the filesystem without performing an integrity check
    5)loop: → Mounts a file

The unmount command:

To remove a removable media device, we should always unmount it first. The command used to unmount devices is umount. syntax is simple:

umount [direcotry | device]

Using the df command:
Sometimes, we need to see how much disk space is available on an individual using df command we can easily do it.

┌──(me㉿kali)-[~]
└─$ df
Filesystem 1K-blocks Used Available Use% Mounted on
udev 3992744 0 3992744 0% /dev
tmpfs 805872 1720 804152 1% /run
tmpfs 4029348 0 4029348 0% /dev/shm
/dev/sda5 1882556 155764 1612824 9% /boot

The df command shows each mounted filesystem that contains data. df command displays :
1) The device location of the device
2) How many 1024-bytes blocks of data it can hold
3) How many 1024-byte blocks are used
4) How many 1024-byte blocks are available
5) The amount of used space as a percentage
6) The mount point where the device is mounted

h parameter shows disk space in human-readable form, M for megabyte and G for gigabyte.

┌──(me㉿kali)-[~]
└─$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 3.9G 0 3.9G 0% /dev
/dev/sda5 1.8G 153M 1.6G 9% /boot

What is du command:

The du command shows the disk usage for a specific directory. by default the du command displays all the files, directories, and subdirectories under the current directory, and it shows how many disk block each fiels or directory takes.

┌──(me㉿kali)-[~]
└─$ du
44 ./Lab/crAPI
40 ./Lab/Pixi/api
480 ./Lab/Pixi/app/public/images
1072 ./Lab/Pixi/app/public/fonts
— more —

The number at the left of each line is the number of disk blocks that each file or directory takes.

Summary

The mount command allows us to mount a physical storage device into the linux virtual directory structure. to remove the device, use the umount command. In next blog we will discuss about shells and subshells used in our script.

Thanks For Reading

--

--

Cyberwizardy23

Linux and Networking Learner , Interested in Cyber Security and Active Learner