# Disk Quotas, File Archiving, and Compression

> Chapter 15 of the Courseiva LPI-LPIC1 curriculum — https://courseiva.com/learn/lpi-lpic1/disk-quotas-and-file-archiving

**Official objective:** 104.3 — Set disk quotas, create archives with tar, and compress files using gzip, bzip2, and xz.

## Introduction

Without managing disk space, a shared computer can grind to a halt when one user fills the drive, and backups become a nightmare of scattered files. Understanding disk quotas, file archiving with tar, and compression with gzip, bzip2, and xz gives you the tools to control storage, preserve data, and save space — essential skills for any Linux system administrator and directly tested on the LPIC-1 exam.

## The Librarian's Desk Analogy

A head librarian runs a busy city library. They have a critical task: ensuring no single patron hogs all the books. The library has a rule — each patron can borrow at most five books at once. This is the disk quota. The librarian checks each request against this limit and refuses any borrow that would exceed it, just as a Linux system checks a user's disk usage against their quota and blocks writes that would surpass it.

The librarian also knows they need to preserve rare manuscripts. They take a bundle of loose documents, put them in a single protective box, and label it clearly with a date and contents. This is like the tar command, which bundles many files into one archive for safekeeping or transport.

Finally, the library has limited shelf space. To fit more books, they use a machine that compresses each book's pages into a smaller form, making them pack tighter on the shelf. This compression is like gzip, bzip2, or xz, which shrink files so they take up less space. The librarian's daily workflow — enforcing limits, bundling collections, and compressing — mirrors exactly how a system administrator manages disk quotas, creates tar archives, and compresses files on a Linux server.

## Core explanation

Disk quotas are a way for a system administrator to limit how much disk space a user or a group of users can consume on a Linux system. Think of it like setting a maximum capacity for each person's locker in a shared gym. Without quotas, one user could fill the entire hard drive with large files, crashing the system for everyone else. Quotas prevent this by setting two types of limits: a soft limit and a hard limit. The soft limit is a warning threshold. If a user exceeds it, the system will still allow writes but will log a warning. The hard limit is an absolute boundary. Once a user hits the hard limit, they cannot write any more data until they delete files to free up space. Quotas are set per filesystem, meaning you apply them to specific partitions like /home or /var, not to the entire system at once. To manage quotas, you use tools like edquota to edit user quotas, quotaon to enable them, and repquota to generate reports.

File archiving with tar is completely different from compression, though they are often used together. The tar command, short for 'tape archive', was originally created to bundle multiple files into one file that could be written to a tape drive. Today, tar takes a set of files and directories and packs them into a single archive file, often called a tarball. This is useful for backups, transferring groups of files, or distributing software. A tar archive preserves the directory structure, file permissions, and timestamps. The basic syntax is 'tar -cvf archive.tar /path/to/files' where -c creates the archive, -v makes the output verbose, and -f specifies the filename. To extract, you use 'tar -xvf archive.tar', where -x extracts the contents.

Compression reduces the size of a file using algorithms that find and remove redundant data. Linux offers three main compression tools, each using a different algorithm with trade-offs between speed and compression ratio:

- gzip: Uses the Lempel-Ziv coding (LZ77) algorithm. It is fast and creates files with a .gz extension. It is the most common for everyday use because it balances speed and size reduction well.
- bzip2: Uses the Burrows-Wheeler transform followed by Huffman coding. It compresses more than gzip but is slower. Its files end with .bz2.
- xz: Uses the LZMA2 algorithm, which gives the highest compression ratio among the three but is the slowest. Its files end with .xz.

You can combine archiving and compression. The most common approach is to use tar with an option that automatically pipes the archive through a compression tool. For example:
- 'tar -czvf archive.tar.gz /files' compresses with gzip
- 'tar -cjvf archive.tar.bz2 /files' compresses with bzip2
- 'tar -cJvf archive.tar.xz /files' compresses with xz

The options -z, -j, and -J tell tar to use gzip, bzip2, and xz respectively. When extracting, you use the same decompression options. You can compress a single file without tar by directly using gzip, bzip2, or xz, which will replace the original file with a compressed version. For instance, 'gzip myfile.txt' removes myfile.txt and creates myfile.txt.gz. To reverse that, 'gunzip myfile.txt.gz' restores the original.

Why do these three tools all exist? Because different situations demand different trade-offs. If you need to archive logs quickly daily, gzip is fine. If you are compressing large datasets for long-term storage and want maximum space savings, xz is better. bzip2 sits in between. The LPIC-1 exam expects you to know the basic commands, their common options, and how to combine them with tar.

## Real-world context

An IT professional at a small web hosting company manages a server that hosts websites for 50 clients. Each client has their own home directory under /home. Over time, one client started uploading large video files meant for marketing, and the server's /home partition filled up, causing all websites to slow down and some to crash. The IT pro’s first task was to investigate the space usage using 'df -h' to see that /home was 100% full, and 'du -sh /home/*' to identify the top space consumer.

To prevent this from happening again, the IT pro set up disk quotas on the /home partition. They first mounted the partition with the 'usrquota' and 'grpquota' options in /etc/fstab. Then, they created the quota files with 'quotacheck -cum /home' and enabled quotas with 'quotaon /home'. Using 'edquota -u john', they set a soft limit of 500 MB and a hard limit of 600 MB for user john. They also applied a group quota for the 'developers' group so the team could share 2 GB total. The IT pro scheduled a weekly 'repquota /home' report to email to all users warning them if they were near their limit.

The IT pro also needed to back up all client websites weekly. They wrote a cron job that runs:
- Every Sunday at 2 AM: 'tar -czf /backup/websites-$(date +%Y%m%d).tar.gz /home /var/www'
This creates a single compressed archive of all home directories and web files, with the date in the filename. The archive is then copied to an offsite storage server using rsync. The -z option compresses on the fly with gzip, which saves space and speeds up the transfer.

When a client accidentally deleted their website files, the IT pro extracted the specific directory from the backup using:
- 'tar -xzf /backup/websites-20250301.tar.gz home/client42/'
This restored just that client's home directory without unpacking the entire archive.

For long-term archival of old client data (clients who left the service), the IT pro used xz for maximum compression:
- 'tar -cJf /archive/old-clients-2024.tar.xz /home/client*/'
This produced a significantly smaller archive than gzip, saving storage costs over years.

The IT pro also used compression standalone. For very large server logs that rotated daily, they ran:
- 'gzip /var/log/apache2/access.log'
This replaced the original log file with a .gz version, cutting storage use by 70%. When needed for troubleshooting, they decompressed with 'gunzip' or used 'zcat' to view the contents without decompressing.

All these actions directly map to skills tested in LPIC-1 objective 104.3. The exam expects candidates to be able to set quotas, create and extract tar archives, and use gzip, bzip2, and xz appropriately.

## Exam focus

The LPIC-1 exam tests objective 104.3 with a mix of multiple-choice questions and fill-in-the-blank or command-line completion tasks. You will be asked to identify the correct command, the correct option, or the correct sequence of steps. The examiners love to test subtle distinctions between compression tools and options. You must know the exact syntax because they often present several very similar commands and ask which one is correct.

Key topics tested:
- Understanding disk quota concepts: soft limit vs hard limit, block vs inode quotas, grace period.
- Commands: quotaon, quotaoff, edquota, setquota, repquota, quotacheck, quota.
- Mount options for quotas: usrquota and grpquota in /etc/fstab.
- tar command: creating, listing, extracting archives. Options -c, -x, -t, -v, -f, -z, -j, -J.
- Compression tools: gzip, gunzip, zcat, bzip2, bunzip2, bzcat, xz, unxz, xzcat.
- Combining tar with compression: what -z, -j, -J do.
- Recognising file extensions: .tar, .tar.gz, .tgz, .tar.bz2, .tar.xz.

Trap patterns to watch out for:
- The exam may ask which command compresses a file but leaves the original. The correct answer is 'gzip -k' or 'bzip2 -k' or 'xz -k'. Without -k, the original file is deleted.
- They may present 'tar -cvf archive.tar.gz /files' and ask if that creates a compressed archive. The answer is no — you need -z for gzip compression. Without it, you just have a tar file with a misleading .gz extension.
- They love to test the difference between 'quota' (user view) and 'repquota' (admin report). A question might ask: 'Which command displays a summary of all quotas for all users?' The answer is repquota, not quota.
- For compression, remember that xz has the highest compression ratio but is slowest. bzip2 is middle, gzip is fastest. The exam might ask which tool to use for maximum space reduction.
- Grace period: They might ask what happens when a user exceeds the soft limit. The answer is that the user still can write but a timer starts. After the grace period expires, the soft limit becomes enforced like a hard limit.
- Mount options: They will test that quotas must be enabled in /etc/fstab with usrquota or grpquota before issuing quotaon.

Key commands to memorise verbatim:
- edquota -u username (edit user quota)
- setquota -u username soft hard 0 0 /partition (set quota from command line)
- quotaon -v /partition (enable quotas)
- repquota -a (report quotas for all filesystems)
- tar -czvf archive.tar.gz directory (create with gzip)
- tar -xjvf archive.tar.bz2 (extract bzip2 archive)
- gzip -k file (compress, keep original)
- xz -9 file (maximum compression)

The exam also tests the concept of block quotas vs inode quotas. Block quotas limit actual disk space (in blocks). Inode quotas limit the number of files (inodes) a user can create. This is important because a user could otherwise create millions of tiny files and exhaust the filesystem's inode table even if the total disk space is barely used.

Finally, know the default compression levels. gzip levels 1-9 (default 6), bzip2 levels 1-9 (default 9), xz levels 0-9 (default 6), and also -e for extreme compression in xz. The exam may ask which level offers the best compression (9 for all, but xz -e or -9 is best overall).

## Step by step

1. **Enable Quota Support in the Filesystem** — Edit /etc/fstab and add the usrquota option to the mount options column for the partition you want to monitor (e.g., /home). This tells the kernel to track disk usage per user on that filesystem.
2. **Initialise the Quota Database** — Run 'quotacheck -cum /home'. The -c flag creates new quota files, -u scans for user quotas, and -m forces the scan even if the filesystem is mounted read-write. This generates aquota.user and aquota.group files in the partition root.
3. **Enable Quotas** — Execute 'quotaon /home' to activate quota monitoring. The system now checks every write operation against user limits. Without this step, quotas are configured but not enforced.
4. **Set User Quotas with edquota** — Run 'edquota -u username' to open an editor where you set soft and hard limits in blocks (typically 1 block = 1024 bytes). Also set the inode limits to control the number of files a user can create. Save and exit to apply the limits.
5. **Create a Compressed Tar Archive** — Use 'tar -czvf backup.tar.gz /important/data'. The -c creates the archive, -z pipes it through gzip, -v lists the files being added, and -f names the output file. This bundles files and compresses them in one step.
6. **Verify the Archive and Extract Selectively** — Run 'tar -tzvf backup.tar.gz' to list contents without extracting. To restore a single directory, use 'tar -xzvf backup.tar.gz path/to/dir'. This avoids unpacking the entire archive, saving time and disk space.

## Comparisons

### Soft Limit vs Hard Limit

**Soft Limit:**
- A warning threshold that can be exceeded temporarily
- Allows writes during a configurable grace period
- User receives warning messages in shell

**Hard Limit:**
- An absolute maximum that cannot be exceeded
- Writes are denied immediately when reached
- User gets 'Disk quota exceeded' error

### gzip vs xz

**gzip:**
- Faster compression and decompression speed
- Lower compression ratio (larger files)
- Uses less CPU and memory during operation

**xz:**
- Slower compression and decompression
- Higher compression ratio (smaller files)
- Uses more CPU and memory; can use -e for extreme mode

### quota command vs repquota command

**quota command:**
- Shows only the current user's quota and usage
- Used by regular users to check their own status
- Cannot display information for other users

**repquota command:**
- Shows a summary of all users' quotas on a filesystem
- Used by administrators for reporting
- Requires root privileges to run

### tar -cvf vs tar -czvf

**tar -cvf:**
- Creates a plain tar archive with no compression
- File extension typically .tar
- Faster to create and extract

**tar -czvf:**
- Creates a tar archive compressed with gzip
- File extension .tar.gz or .tgz
- Slower but produces a much smaller file

## Diagram

_Flowchart showing how disk quotas enforce write limits and how tar and compression tools combine to create compressed archives._

```mermaid
flowchart TD
    A[User writes data] --> B{Is filesystem mounted with usrquota?}
    B -- Yes --> C{Check user quota}
    C -- Under soft limit --> D[Allow write]
    C -- Over soft limit, under hard limit --> E[Allow write, start grace period]
    C -- Over hard limit --> F[Deny write, quota exceeded]
    B -- No --> D
    E --> G[Grace period expires?]
    G -- No --> D
    G -- Yes --> F
    A2[Admin archives files] --> H[tar -cvf]
    H --> I[Tarball (uncompressed)]
    A3[Admin wants compression] --> J[gzip / bzip2 / xz]
    J --> K[Compressed file with .gz / .bz2 / .xz]
    A4[Admin combines] --> L[tar -czvf / -cjvf / -cJvf]
    L --> M[Compressed archive (.tar.gz / .tar.bz2 / .tar.xz)]
```

## Common misconceptions

- **Misconception:** A tar archive file automatically saves disk space because it bundles files together. **Reality:** A tar archive does not compress; it only bundles files. The resulting file is typically larger than the sum of the files due to padding. You must use a compression tool like gzip (via -z) to reduce size. (The word 'archive' sounds like it should save space, but tar without compression actually adds overhead.)
- **Misconception:** Setting a soft limit of 500 MB and a hard limit of 600 MB means the user can never exceed 500 MB without a warning. **Reality:** The user can exceed the soft limit up to the hard limit (600 MB) during a grace period. Once the grace period expires, the soft limit becomes a hard bar. (The term 'soft limit' sounds like a strict boundary, but it is actually a configurable warning threshold.)
- **Misconception:** Running 'gzip file.txt' creates a new file called file.txt.gz and leaves the original file.txt intact. **Reality:** By default, gzip replaces the original file with the compressed version. The original file.txt is deleted. To keep the original, you must use the -k option. (Many beginners expect a 'copy and compress' behaviour, but command-line tradition favours replacing files unless told otherwise.)
- **Misconception:** The 'xz' compression tool is always the best choice for all files because it compresses the most. **Reality:** xz usually gives the best compression ratio, but it is significantly slower and uses more memory than gzip or bzip2. It is best for archives where you care more about final size than speed. (Beginners think 'best' is absolute, but in IT, 'best' depends on context: speed vs size, memory constraints, and frequency of access.)
- **Misconception:** Disk quotas can be set on any directory or folder on the system. **Reality:** Disk quotas are applied at the filesystem level, meaning to a partition (e.g., /home, /var). You cannot set a quota on an arbitrary subdirectory unless that subdirectory is a separate mount point. (Casual users think of 'folders' as the limit boundary, but the filesystem structure is more rigid: quotas tie to mount points and superblocks.)

## Key takeaways

- Disk quotas use soft limits (warn at threshold) and hard limits (absolute maximum), and are enabled per filesystem via the usrquota and grpquota mount options.
- The tar command bundles files and directories into a single archive without compression; to compress, use -z (gzip), -j (bzip2), or -J (xz).
- By default, gzip, bzip2, and xz replace the original file with the compressed version; use -k to keep the original.
- The xz tool offers the highest compression ratio among the three common Linux compressors, but it is the slowest and most memory-intensive.
- The repquota command generates a report of quotas for all users on a filesystem, while the quota command shows a single user's own limits.
- You must run quotacheck after enabling quotas in /etc/fstab to set up the quota database files (aquota.user and aquota.group).

## FAQ

**What is the difference between soft limit and hard limit in disk quotas?**

The soft limit is a warning threshold that can be exceeded during a grace period; the hard limit is an absolute ceiling that cannot be exceeded at all.

**How do I compress a single file using gzip without deleting the original?**

Use 'gzip -k filename' to create filename.gz while keeping the original. Alternatively, use 'gzip -c filename > filename.gz'.

**What does the -z option do in the tar command?**

The -z option tells tar to filter the archive through gzip, compressing (or decompressing) the archive on the fly. It is used with -c for creation or -x for extraction.

**Why doesn't 'tar -cvf archive.tar.gz /files' create a compressed archive?**

Because the -z option is missing. Without -z, tar simply creates a tar archive with a .tar.gz filename extension that is misleading. The file is not compressed.

**Which compression tool gives the smallest file size: gzip, bzip2, or xz?**

xz generally gives the best (smallest) compression ratio, followed by bzip2, then gzip. However, xz is the slowest and uses the most memory.

**How do I check if a user has exceeded their disk quota?**

The user can run 'quota' to see their own usage and limits, or an administrator can run 'repquota /home' to see all users on that filesystem.

**Do I need to unmount a partition to enable quotas?**

No, you can run 'quotaon /partition' while the filesystem is mounted. However, you need to have the usrquota option set in /etc/fstab and you must have run quotacheck first.

## Check your understanding

1. **Which command enables quotas on the /home partition?**

   Answer: The command is 'quotaon /home'. This activates quota monitoring for that filesystem.

2. **What is the effect of the -k option when used with gzip?**

   Answer: The -k option tells gzip to keep the original file unchanged, so both the uncompressed and compressed versions exist after compression.

3. **A user wants to extract only the /etc directory from a bzip2-compressed archive called backup.tar.bz2. What command do they use?**

   Answer: The command is 'tar -xjvf backup.tar.bz2 etc/' or 'tar -xjvf backup.tar.bz2 --directory / target/etc/' (but simplest: 'tar -xjvf backup.tar.bz2 etc/').

4. **Which compression tool would you choose for maximum space saving on a large archive that you rarely access?**

   Answer: You would choose xz, as it offers the highest compression ratio among the three tools (gzip, bzip2, xz), making it ideal for long-term archival storage.

5. **What must you add to the mount options in /etc/fstab to enable user disk quotas on a partition?**

   Answer: You must add the 'usrquota' option to the mount options column for that partition. For group quotas, you add 'grpquota' as well.

---

Interactive version with quiz and diagrams: https://courseiva.com/learn/lpi-lpic1/disk-quotas-and-file-archiving
