Linux File Operations and Disk Management: A Practical Guide

  1. File System and Disk Management Basics

To inspect disk usage on your system, use df -h:

df -h

To check directory sizes, use du with various options:

  • du -h – display sizes in human-readable format (K, M, G).
  • du -a – show sizes for all files in the directory tree.
  • du -d N – limit depth to N levels. For example, du -h -d 0 ~ shows only the home directory itself; du -h -d 1 ~ adds one more level.

Remember: du measures file sizes, while df measures disk partition usage.

  1. The dd Command for Data Conversion

dd is a powerful tool for copying and converting data. Example:

dd of=test bs=10 count=1

After runing this, the terminal waits for input. Type some characters and press Ctrl+D. A file named test is created containing exactly 10 bytes (one block of size 10).

  • bs – block size (default bytes; can use K, M, G).
  • count – number of blocks to copy.

For full details, see man dd.

  1. Formatting Disks with mkfs

Use mkfs to create a file system on a device. The command supports many file system types (e.g., ext4, btrfs).

mkfs.ext4 /dev/sdb1   # example

Refer to man mkfs for type-specific options.

  1. Mounting File Systems

The mount command attaches a device to a directory:

mount -t type [-o options] device dir
  • device – the block device (e.g., /dev/sda1).
  • dir – the mount point (e.g., /mnt).
  • type – file system type (often auto-detected).
  • options – e.g., ro for read-only.

Example: mount a CD-ROM:

mount /dev/cdrom /mnt

To remount the root partition as read-write (use with caution):

mount / -o rw,remount

To mount a Windows share (CIFS):

mount -o username=test,password=test123 //192.168.1.100/shared /mnt

Often the -t flag can be omitted because mount auto-detects the type.

  1. Cerating a Virtual Hard Disk with Loop Device

You can simulate a disk using a file and the loop device. This is useful for experimenting with file systems like btrfs without repartitioning.

Step 1: Create a 512 MB file with dd:

dd if=/dev/zero bs=1M count=512 of=./vdisk.img

Step 2: Install btrfs tools and create the file system:

sudo apt install btrfs-progs
mkfs.btrfs vdisk.img

Step 3: Mount the virtual disk:

sudo mount vdisk.img /mnt

To list physical disks, use fdisk -l.

  1. Fun with cowsay

The cowsay command prints a message in a speech bubble from an ASCII animal.

# Install
sudo apt update
sudo apt install -y cowsay

# Default cow
cowsay hello world

# List all available animals
cowsay -l

# Use a specific animal (e.g., elephant)
cowsay -f elephant hello world

# Combine with fortune (install fortune-zh)
sudo apt-get install fortune-zh
/usr/games/fortune | cowsay -f daemon

Tags: Linux df du dd mkfs

Posted on Thu, 21 May 2026 18:04:04 +0000 by Daen