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 -kin kilobytes, as a whole number. In GNUduthis is already the default unit, but with-kthe result is the same on any system.du -min megabytes.du -hfor humans: it picksK,M, orGon its own.
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
du -k /tmp/big > /tmp/answer: the file will end up with8and the file name, separated by a tab. You need| cut -f1or| awk '{print $1}'.du -k < /tmp/big:dudoesn't read stdin and prints the size of the current directory.du -hin a script:8.0Kisn't a number. For comparisons, use-k.- The size from
ls -linstead ofdu: for small and sparse files the numbers can differ a lot. Dividingls -l's output by 1024 is not the same asdu -k. du -sh dir/*doesn't see hidden directories, andls -lh dirdoesn't show the size of a directory's contents.- macOS: BSD
duwithout a flag counts in 512-byte blocks, so an 8 KB file will show up as16. With-kand-hthe numbers match Linux. There's no--apparent-sizeflag there (there's-A), and-d 1works.
What you'll need for the tasks
- Get a file's size with
du -kand leave just one number withcut -f1orawk. - Save the number to a file with
>. - Compare directories with
du -sh .../* | sort -h, and don't trustls -lwhen it's about space used.
Practice in your browser
Linux runs right here. Solve the task and we'll save your progress automatically.
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.
0%
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.
Tell the agent: use MCP izi, give me lesson terminal-basics-02-file-size
Progress saved in this browser
Clear your history or open this on your phone, and the marks will be gone. Leave your email and they'll move to your account. No password needed.
Done. moved to your account. You can see your progress on any device. My progress