Sign in
← Terminal: getting oriented
3 min · easy · Lesson 2 of 5

How much space a file takes up: du

Check a file's size in kilobytes and save a single number.

Before you delete or archive something on a server, you need to know its size. How much the backup weighs, which directory ate the disk, whether the archive will fit on a flash drive: du (disk usage) answers these questions. The command shows how much space a file or directory takes up on disk.

Where to practice

The examples below work in an ubuntu:24.04 container. You can bring up the same one yourself:

docker run --rm -it ubuntu:24.04 bash

On Linux and in WSL2 the commands work right in your own terminal too. On macOS du is different (it comes from BSD); see "Common mistakes" for the details.

Create a file exactly 8 kilobytes long:

dd if=/dev/zero of=/tmp/big bs=1024 count=8 2>/dev/null

dd copies 8 blocks of 1024 bytes from /dev/zero (an infinite stream of zero bytes) into /tmp/big. 2>/dev/null hides dd's status report.

What du prints

du /tmp/big
8	/tmp/big

Two fields: size and file name, separated by a tab. Units are set with a flag:

du -h /tmp/big
8.0K	/tmp/big

-h is easy on the eyes but awkward in a script: 8.0K can't be compared with a number. For a script, use -k or -m.

How to leave just one number

With wc from the last lesson, the trick with < helped. It doesn't work with du: du doesn't read stdin, it needs a file name. Without a name, it counts the current directory:

du -k < /tmp/big
12	.

So you strip the name afterward, from the output. The fields are separated by a tab, and cut -f1 cuts out the first field (by default cut splits a line on tabs):

du -k /tmp/big | cut -f1
8

awk '{print $1}' does the same thing: it prints the first field, splitting on spaces and tabs. You can save the number to a file with >, as in the last lesson.

Size on disk and file length

ls -l also shows a size, but a different one. It's the file's length in bytes, how much data is actually written to it. du shows how much space the file system allocated for the file. Space is allocated in blocks, usually 4 KB each, so even a tiny file takes up a whole block:

printf 'hello\n' > /tmp/small
ls -l /tmp/small
-rw-r--r-- 1 root root 6 Oct  9 18:27 /tmp/small
du -k /tmp/small
4	/tmp/small

6 bytes of data, 4 KB on disk. A file with 10,000 bytes would take up three blocks, and du -k would print 12. du can also show the file's length in kilobytes, with the --apparent-size flag (length, not space):

du -k --apparent-size /tmp/small
1	/tmp/small

It also happens the other way around: huge length, almost no space. That's a sparse file. The file system doesn't store blocks that were never written to. These files are often used for virtual machine disk images:

truncate -s 1G /tmp/sparse
ls -lh /tmp/sparse
-rw-r--r-- 1 root root 1.0G Oct  9 18:27 /tmp/sparse
du -h /tmp/sparse
0	/tmp/sparse

When disk space runs out, trust du: it shows what's actually in use.

Directory size

For a directory, du by default prints a line for every subdirectory and a grand total at the end. Usually you only need the total, which you get with -s (summary):

du -sh /var/log

To find which subdirectory is the heaviest, measure each one and sort:

du -sh /var/log/* | sort -h

sort -h understands the suffixes K, M, G, so 900K ends up above 2.0M. The biggest will be the last line. The asterisk * doesn't catch hidden directories (names starting with a dot). If they matter, use du -h -d 1 /var/log | sort -h: -d 1 means "one level deep", and hidden directories will be included too.

A directory's size in ls -l (4.0K for each) is the size of the directory entry itself, not of its contents. For directories, ls is useless: you need du.

Common mistakes

What you'll need for the tasks

Practice in your browser

Linux runs right here. Solve the task and we'll save your progress automatically.

01

Size of /tmp/big in kilobytes

We created /tmp/big with a known size. Write its size (a whole number of kilobytes) to /tmp/answer.

{ i }

Go through it with an AI agent

The agent explains anything unclear in plain words, gives you 2 labs for this lesson and drills you on interview questions. $49 one-time, lifetime access.

Get access for $49

Tell the agent: use MCP izi, give me lesson terminal-basics-02-file-size