tech katta

Disks, packages and archives: df, du, apt and tar

Lesson 11 of 23 · 5 min readFree

In this lesson

Check how full a server's disk is and what filled it, install and inspect software with apt, and pack or unpack backups and releases with tar.

On this page
  1. Why this matters
  2. df: how full is the disk?
  3. du: what is using the space?
  4. apt: installing software
  5. tar: archives
  6. Try it

Three small jobs come up on every server sooner or later: "the disk is full", "install this tool" and "pack up that folder". This lesson gives you the commands for all three, using Ubuntu, the most common choice for servers in this course.

Why this matters#

A full disk is one of the nastiest production problems. The database cannot write, logs stop, the app crashes with confusing errors and sometimes you cannot even log in. Knowing how to find what is using the space takes five minutes with the right commands and hours without them. Package management and archives are the other half of day-to-day server work: installing the software your app needs and moving releases and backups around as single files.

df: how full is the disk?#

df ("disk free") reports space for each mounted file system. Add -h for human-readable sizes and -T to show the type.

Shell
df -hT /
Text
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/vda       ext4  252G  9.2G   60G  14% /

The numbers are from the machine used for this lesson. On a small cloud server you might see 25G with 80% used. The line you care about is the Use% of the file system that holds your data. Watch for anything over about 85%. Pass a path to ask about the disk that holds it: df -h /var/log.

You can also run out of inodes, the records that describe files, even with free space. That happens when a folder fills with millions of tiny files, such as session files or cache entries. df -i shows inode use:

Shell
df -i /
Text
Filesystem       Inodes  IUsed    IFree IUse% Mounted on
/dev/vda       16777216 220194 16557022    2% /

If IUse% is near 100% while Use% is low, look for a folder with a huge number of small files.

du: what is using the space?#

df says the disk is full. du ("disk usage") says which folders are to blame.

Shell
du -sh /srv/l11/app
du -h --max-depth=1 /srv/l11/app
Text
8.7M	/srv/l11/app
8.1M	/srv/l11/app/shared
624K	/srv/l11/app/releases
8.7M	/srv/l11/app

-s prints one total, -h makes sizes readable and --max-depth=1 shows each immediate child with its total. Sort the result to rank the biggest first:

Shell
du -sh /srv/l11/app/* | sort -h
du -ah /srv/l11/app | sort -rh | head -4
Text
624K	/srv/l11/app/releases
8.1M	/srv/l11/app/shared
8.7M	/srv/l11/app
8.1M	/srv/l11/app/shared
6.1M	/srv/l11/app/shared/uploads
6.0M	/srv/l11/app/shared/uploads/video.bin

sort -h understands sizes like 624K and 8.1M, while plain sort -n would not. du -a includes files, not only folders, so the largest single file appears too. The next step is the usual hunt: start at / with sudo du -h --max-depth=1 / 2>/dev/null | sort -rh | head, and repeat inside the biggest folder until you find the cause. The usual suspects are /var/log, the journal (journalctl --disk-usage), old releases, Docker images and uploaded files.

One trap: if you delete a big log file that a running process still has open, the space is not freed until that process closes it or restarts. df still shows the disk full and du finds nothing. Restarting the service releases the space, and lsof +L1 lists deleted-but-open files. For logs the better habit is to truncate in place with : > file.log, or let logrotate handle it.

apt: installing software#

On Debian and Ubuntu, apt installs programs from repositories, which are signed collections of packages that your system trusts. Package installs need root, so on a normal account you prefix sudo. We were root in the test container, so the commands below were run without it.

Shell
sudo apt update
sudo apt install rsync

apt update downloads the latest package lists. It does not install anything. Without it, apt install may fetch an old or missing version. The refresh looks like this:

Text
Hit:1 URL noble InRelease
Hit:2 URL noble-security InRelease
Hit:3 URL noble-updates InRelease

(We replaced the repository addresses with URL.) Hit means the list is unchanged since your last update.

Useful commands around it:

  • apt search word and apt show package find and describe packages.
  • apt-cache policy rsync shows which version is installed and which is the candidate.
  • sudo apt upgrade installs newer versions of what you have, and sudo apt remove package removes a package but keeps its config. purge removes config too.
  • sudo apt autoremove removes dependencies nothing needs any more.
  • dpkg -L package lists the files a package installed, and dpkg -S /usr/bin/rsync tells you which package owns a file.
Shell
apt-cache policy rsync | head -3
dpkg -S /usr/bin/rsync
Text
rsync:
  Installed: 3.2.8-0ubuntu0.24.04.1
  Candidate: 3.2.8-0ubuntu0.24.04.1
rsync: /usr/bin/rsync

To preview an install without doing it, use a simulation:

Shell
apt-get install -s tree
Text
The following NEW packages will be installed:
  tree
0 upgraded, 1 newly installed, 0 to remove and 87 not upgraded.

If you misspell a package, you get E: Unable to locate package. That usually means a typo, or that you forgot apt update, or that the package is in a repository you have not added.

In scripts, use apt-get install -y package with -y to answer yes automatically, because the apt command is meant for interactive use and warns about it. Only install packages from the official repositories or from vendors you trust. A repository you add can replace any package on the system.

tar: archives#

tar packs many files and folders into one file, which makes it easy to back up, copy or deploy them together. Adding -z compresses with gzip, and the result is usually named .tar.gz or .tgz.

The options are easy to remember by what you want to do:

  • -c create an archive.
  • -x extract.
  • -t list the contents without extracting.
  • -z gzip compression.
  • -f name the archive file name. It must come right after the options.
  • -C dir change to this folder first.
  • -v print each file as it is processed.

Create an archive of one release and the shared .env, list it and extract it somewhere else:

Shell
tar -czf app-20261202.tar.gz app/releases/20261202 app/shared/.env
tar -tzf app-20261202.tar.gz
mkdir restore && tar -xzf app-20261202.tar.gz -C restore
Text
app/releases/20261202/
app/releases/20261202/bundle.js
app/shared/.env

Always list (-t) an archive you did not make before you extract it. Archives from others can contain absolute paths or fill your current folder with files. Extract into a fresh folder with -C.

To extract just one file, name it. The name must match what -t printed:

Shell
tar -xzf app-20261202.tar.gz -C restore app/shared/.env

If the name is wrong you get tar: ./nonexistent: Not found in archive. To leave files out, use --exclude: tar --exclude='*.log' -czf nolog.tar.gz app. To archive a folder's contents without its parent path, use -C: tar -czf logs.tar.gz -C app/shared logs stores paths starting with logs/.

Compression helps text a lot and random or already compressed data not at all. Our test archive stayed at 311K because the sample files were random bytes. Real logs, JavaScript and JSON shrink far more.

gzip file compresses one file (and replaces it, unless you pass -k), gunzip reverses it and zcat file.gz reads it without unpacking. For .zip files you need zip and unzip, which may have to be installed with apt.

Try it#

  1. Run df -h and find the line that holds /. Is it above 85%? Run df -i as well.
  2. Pick a folder you own with a few files, such as ~. Find its three biggest subfolders using du -h --max-depth=1 ~ | sort -rh | head.
  3. Create demo/ with a few text files. Make demo.tar.gz with tar -czf, list it with tar -tzf, and extract it into a new folder with -C. Compare the files with diff -r.
  4. On a Debian or Ubuntu machine, run apt-cache policy tree and apt-get install -s tree. Read the output. Then use dpkg -S /usr/bin/ls to see which package owns ls.
  5. Create a tar archive that leaves out all .log files. Check the list to prove none are inside.