Disks, packages and archives: df, du, apt and tar
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
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.
df -hT /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:
df -i /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.
du -sh /srv/l11/app
du -h --max-depth=1 /srv/l11/app8.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:
du -sh /srv/l11/app/* | sort -h
du -ah /srv/l11/app | sort -rh | head -4624K /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.binsort -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.
sudo apt update
sudo apt install rsyncapt 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:
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 wordandapt show packagefind and describe packages.apt-cache policy rsyncshows which version is installed and which is the candidate.sudo apt upgradeinstalls newer versions of what you have, andsudo apt remove packageremoves a package but keeps its config.purgeremoves config too.sudo apt autoremoveremoves dependencies nothing needs any more.dpkg -L packagelists the files a package installed, anddpkg -S /usr/bin/rsynctells you which package owns a file.
apt-cache policy rsync | head -3
dpkg -S /usr/bin/rsyncrsync:
Installed: 3.2.8-0ubuntu0.24.04.1
Candidate: 3.2.8-0ubuntu0.24.04.1
rsync: /usr/bin/rsyncTo preview an install without doing it, use a simulation:
apt-get install -s treeThe 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:
-ccreate an archive.-xextract.-tlist the contents without extracting.-zgzip compression.-f namethe archive file name. It must come right after the options.-C dirchange to this folder first.-vprint each file as it is processed.
Create an archive of one release and the shared .env, list it and extract it somewhere else:
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 restoreapp/releases/20261202/
app/releases/20261202/bundle.js
app/shared/.envAlways 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:
tar -xzf app-20261202.tar.gz -C restore app/shared/.envIf 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#
- Run
df -hand find the line that holds/. Is it above 85%? Rundf -ias well. - Pick a folder you own with a few files, such as
~. Find its three biggest subfolders usingdu -h --max-depth=1 ~ | sort -rh | head. - Create
demo/with a few text files. Makedemo.tar.gzwithtar -czf, list it withtar -tzf, and extract it into a new folder with-C. Compare the files withdiff -r. - On a Debian or Ubuntu machine, run
apt-cache policy treeandapt-get install -s tree. Read the output. Then usedpkg -S /usr/bin/lsto see which package ownsls. - Create a tar archive that leaves out all
.logfiles. Check the list to prove none are inside.
Sign in to keep track of the lessons you finish.