SyntaxStudy
Sign Up
Linux / Bash Links, Archives, and Disk Usage
Linux / Bash Beginner 1 min read

Links, Archives, and Disk Usage

Linux supports two types of file links. A hard link is an additional directory entry pointing to the same inode and data blocks on disk — deleting one link does not remove the data until all links are gone. A symbolic (soft) link is a special file that contains a path to another file, similar to a shortcut. Symbolic links can cross filesystem boundaries and point to directories, unlike hard links. Archiving and compression are essential for backups and file transfers. `tar` (tape archive) bundles files into a single archive. Combined with `gzip` (`-z`) or `bzip2` (`-j`) or `xz` (`-J`), it also compresses them. The `zip` and `unzip` utilities create archives compatible with Windows. Understanding the difference between archiving (bundling) and compression (reducing size) prevents confusion with `tar` flags. Monitoring disk usage prevents systems from running out of space, which causes application failures. `df` reports free space per filesystem, while `du` recursively measures directory sizes. Combining `du` with `sort -h` quickly identifies the directories consuming the most space.
Example
# Create a hard link
ln original.txt hardlink.txt
ls -li original.txt hardlink.txt    # Same inode number

# Create a symbolic link
ln -s /etc/nginx/nginx.conf nginx.conf
ls -la nginx.conf
# lrwxrwxrwx 1 alice alice 22 Apr 10 09:00 nginx.conf -> /etc/nginx/nginx.conf

# Remove a symbolic link (not the target)
rm nginx.conf

# ---- tar archives ----

# Create a .tar.gz archive
tar -czvf backup.tar.gz /etc/nginx/

# List contents of an archive
tar -tzvf backup.tar.gz

# Extract an archive to current directory
tar -xzvf backup.tar.gz

# Extract to a specific directory
tar -xzvf backup.tar.gz -C /tmp/restore/

# Create .tar.bz2 (better compression, slower)
tar -cjvf backup.tar.bz2 projects/

# Create .tar.xz (best compression)
tar -cJvf backup.tar.xz projects/

# ---- zip ----
zip -r archive.zip projects/
unzip archive.zip -d /tmp/projects_restored/

# ---- disk usage ----
df -h                               # Free space per filesystem
du -sh /var/log/                    # Size of a directory
du -sh /var/log/* | sort -h         # Sorted sizes of subdirectories
du -sh /* 2>/dev/null | sort -rh | head -10   # Top 10 biggest root dirs