File System and Disk Management

LinuxLinuxBeginner
Practice Now

Introduction

Welcome to this lab on File System and Disk Management in Linux! This lab is designed for beginners who are just starting to explore the world of Linux system administration. We'll guide you through essential commands and concepts related to managing disk space, creating virtual disks, and maintaining filesystems. By the end of this lab, you'll have hands-on experience with fundamental Linux disk management tools.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/head("`File Beginning Display`") linux/TextProcessingGroup -.-> linux/sort("`Text Sorting`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/SystemInformationandMonitoringGroup -.-> linux/dd("`File Converting/Copying`") linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") linux/SystemInformationandMonitoringGroup -.-> linux/du("`File Space Estimating`") linux/SystemInformationandMonitoringGroup -.-> linux/mount("`File System Mounting`") subgraph Lab Skills linux/head -.-> lab-17999{{"`File System and Disk Management`"}} linux/sort -.-> lab-17999{{"`File System and Disk Management`"}} linux/mkdir -.-> lab-17999{{"`File System and Disk Management`"}} linux/sudo -.-> lab-17999{{"`File System and Disk Management`"}} linux/dd -.-> lab-17999{{"`File System and Disk Management`"}} linux/df -.-> lab-17999{{"`File System and Disk Management`"}} linux/du -.-> lab-17999{{"`File System and Disk Management`"}} linux/mount -.-> lab-17999{{"`File System and Disk Management`"}} end

Viewing Disk Usage with df

The df (disk free) command is your go-to tool for checking disk space usage on your Linux system. Let's explore how to use it:

  1. Open your terminal. You should be in the /home/labex/project directory. If you're not sure, you can always check your current directory with the pwd command.

  2. Run the following command to view disk usage:

    df

    You'll see output similar to this:

    Filesystem     1K-blocks      Used Available Use% Mounted on
    overlay         20971520    128744  20842776   1% /
    tmpfs              65536         0     65536   0% /dev
    tmpfs            3995004         0   3995004   0% /sys/fs/cgroup
    shm                65536         0     65536   0% /dev/shm
    /dev/vdb       104806400  57754052  47052348  56% /etc/hosts

    Don't worry if this looks confusing at first! Let's break it down:

    • Filesystem: This column shows the name of the disk or partition.
    • 1K-blocks: This is the total size of the filesystem in 1-kilobyte blocks.
    • Used: This shows how much space is currently in use.
    • Available: This shows how much free space is left.
    • Use%: This shows the percentage of the filesystem that is in use.
    • Mounted on: This shows where in the directory tree the filesystem is mounted.
  3. Now, let's make this output more human-readable. Run:

    df -h

    The -h option stands for "human-readable". You'll see output like this:

    Filesystem      Size  Used Avail Use% Mounted on
    overlay          20G  126M   20G   1% /
    tmpfs            64M     0   64M   0% /dev
    tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
    shm              64M     0   64M   0% /dev/shm
    /dev/vdb        100G   56G   45G  56% /etc/hosts

    Much better, right? Now the sizes are in GB and MB, which is easier to understand.

  4. If you want to check the space on a specific filesystem, you can specify it:

    df -h /dev/vdb

    This will show information only for the /dev/vdb filesystem.

The df command is incredibly useful for quickly checking how much disk space you have left. If you ever run into issues where your system says it's running out of space, df is often the first command you'll use to investigate.

Examining Directory Sizes with du

While df gives us an overview of disk usage, sometimes we need to dig deeper. That's where du (disk usage) comes in. It helps us understand which directories and files are taking up the most space.

  1. Let's start by using du in its simplest form. Run:

    du ~

    You'll see a long list of numbers and directory names. Each number represents the size of the directory in kilobytes. This can be overwhelming, so let's make it more manageable.

  2. For a more readable output, use the -h option:

    du -h ~

    The -h option, just like with df, makes the output human-readable. You'll see sizes in KB, MB, or GB as appropriate.

  3. Often, we just want to know the total size of a directory. For this, use:

    du -sh ~

    Here, -s means "summarize" and ~ represents your home directory. This command will show you the total size of everything in your home directory.

  4. To view the sizes of immediate subdirectories in your home, use:

    du -h --max-depth=1 ~

    This shows the size of each subdirectory one level deep. The --max-depth=1 option limits how far du will recurse into subdirectories.

  5. Let's check the size of items in your home directory:

    du -sh ~/*

    This will show the size of each item directly under your home directory.

  6. Here's a powerful command to find the largest items in your home directory:

    du -h ~ | sort -rh | head -n 10

    Let's break this down:

    • du -h ~ lists all files and directories in your home directory with their sizes
    • sort -rh sorts this list in reverse order (largest first) and in human-readable format
    • head -n 10 shows only the first 10 lines of output
    • | is a pipe, which passes the output of one command as input to the next

    This command is a great example of how we can combine simple Linux commands to perform more complex operations.

The du command is invaluable when you're trying to free up disk space. It helps you identify which directories or files are taking up the most room, so you know where to focus your cleanup efforts.

Creating and Managing a Virtual Disk

Now we're going to create a virtual disk file, format it with a filesystem, and mount it. This process is similar to setting up a new hard drive, but we're doing it all within a file.

  1. First, let's create a 256MB virtual disk using the dd command:

    dd if=/dev/zero of=virtual.img bs=1M count=256

    Here's what this command does:

    • if=/dev/zero: Input file is /dev/zero, a special file that provides as many null bytes as are read from it
    • of=virtual.img: Output file is virtual.img
    • bs=1M: Sets both input and output block size to 1 megabyte
    • count=256: Copies only 256 input blocks

    This effectively creates a 256MB file filled with zeros.

  2. Verify the file size:

    ls -lh virtual.img

    You should see that virtual.img is exactly 256MB.

  3. Now, let's format this virtual disk with an ext4 filesystem:

    sudo mkfs.ext4 virtual.img

    This command creates an ext4 filesystem on our virtual disk file. You'll see some output about the filesystem creation process.

  4. Next, we need to create a mount point. This is the directory where the contents of our virtual disk will appear:

    sudo mkdir /mnt/virtualdisk
  5. Now we can mount the virtual disk:

    sudo mount -o loop virtual.img /mnt/virtualdisk

    The -o loop option is necessary because we're mounting a file as if it were a block device.

  6. Let's verify that the disk is mounted:

    mount | grep virtualdisk

    You should see a line indicating that virtual.img is mounted on /mnt/virtualdisk.

  7. Now that it's mounted, we can use it like any other directory. Let's create a file:

    sudo touch /mnt/virtualdisk/testfile
    ls /mnt/virtualdisk

    You should see testfile listed.

  8. When you're done using the virtual disk, you should unmount it:

    sudo umount /mnt/virtualdisk

This process of creating, formatting, and mounting a virtual disk is very similar to what happens when you plug in a new hard drive or USB stick. The main difference is that we're doing everything with a file instead of a physical device.

Managing Disk Partitions with fdisk

In a real system, before you can create a filesystem, you often need to create partitions. While we can't modify actual disk partitions in this virtual environment, we can explore how to use fdisk to view partition information.

  1. First, let's view information about all disk partitions:

    sudo fdisk -l

    This will display information about all disk devices and their partitions. You'll see details about the disk size, number of sectors, and partition table.

  2. Now, let's look at the partition information for our virtual disk:

    sudo fdisk -l virtual.img

    This will show you the partition table of the virtual disk. Since we created the filesystem directly on the disk image without partitioning, you might see a message saying that it doesn't contain a valid partition table.

In a real system, you would use fdisk interactively to create, delete, or modify partitions. Here's a brief overview of how that would work:

  • You'd start fdisk with sudo fdisk /dev/sdX (replace X with the appropriate letter for the disk you want to partition)
  • You'd use the command 'n' to create a new partition
  • 'd' would delete a partition
  • 't' would change a partition's system id (which indicates the partition's intended use)
  • 'w' would write the changes and exit

Remember, modifying partitions can lead to data loss, so always be careful and backup important data before making changes to disk partitions.

Checking Filesystem Integrity with fsck

The fsck (file system consistency check) utility is an important tool for maintaining the health of your filesystems. It's used to check for and optionally repair filesystem inconsistencies.

  1. First, make sure the virtual disk is unmounted. If it's still mounted from the previous steps, unmount it:

    sudo umount /mnt/virtualdisk

    It's important to always run fsck on unmounted filesystems to avoid data corruption.

  2. Now, let's run fsck on our virtual disk:

    sudo fsck -f virtual.img

    The -f option forces checking even if the filesystem seems clean. You should see output indicating that fsck is checking the filesystem.

  3. If any errors are found, fsck will ask if you want to fix them. For learning purposes, you can answer 'y' to these prompts. However, in a real-world scenario with important data, you'd want to carefully consider each error before allowing automatic fixes.

  4. After fsck completes, it will give you a summary of what it found and what actions it took.

In real-world scenarios, you would use fsck on actual disk partitions (like /dev/sda1) rather than disk image files. It's an important tool for maintaining filesystem health, and it's often run automatically during the boot process if the system detects that a filesystem wasn't cleanly unmounted (for example, after a power failure).

Remember, while fsck can fix many filesystem issues, it's not a substitute for regular backups. Always keep backups of your important data!

Summary

Congratulations! In this lab, you've learned how to:

  1. View disk usage with df
  2. Examine directory sizes with du
  3. Create, format, mount, and unmount a virtual disk
  4. View partition information with fdisk
  5. Check filesystem integrity with fsck

These skills form a foundation for more advanced Linux system administration tasks. They are crucial for managing storage, troubleshooting disk space issues, and maintaining filesystem health in Linux systems.

As an extra challenge, try to write a shell script that finds the top 10 largest files or directories in your home directory and displays their sizes in human-readable format. This will combine several of the commands you've learned in this lab.

Remember, practice is key in mastering these concepts. Don't hesitate to experiment with these commands (in a safe environment) to deepen your understanding. Good luck with your continued learning journey in Linux system administration!

Other Linux Tutorials you may like