- 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 toNlevels. 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.
- The
ddCommand 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.
- 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.
- 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.,rofor 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.
- 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.
- 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