What's Filling My Disk? du and df
du shows how much disk space each file and folder takes, sort -h puts those sizes in order with the biggest first, and df shows how much room is left on the drive.
Apprentice · Unix · Linux · Updated
After this lesson you can
- Find what takes the most space in a folder with du -sh, sorted biggest first with sort -h
- Check how much room is left on a drive with df -h
Key takeaways
- du -sh and a name shows how much space a file or folder takes. du -sh * does it for everything in the folder you're in.
- The | (pipe) hands one command's output to the next one: du -sh * | sort -hr lists the biggest first.
- sort -h understands sizes such as 28K and 3.9M. Plain sort reads them as text and gets the order wrong.
- df -h shows a drive's size, how much of it is used and how much is left. df -h . shows only the drive you're working on.
- du and df only read sizes, so they're safe to run on any folder.
It's the night before a trip, you want to download a few movies for the flight, and your laptop says the disk is almost full. Something is taking up the space, but what? A file manager makes you open folder after folder and check each one's size by hand. The terminal answers in one line: du says what takes the space, and df says how much is left. IT help-desk techs ask exactly this on a full laptop, and web developers ask it when a server stops saving files because its disk filled up.
Set up a packing folder
The lessons in this chapter use a made-up folder of things for a summer trip: photos, a video, some music and the travel papers. Copy these lines into your terminal. They make the folder ~/terminal-practice/packing, fill it with stand-in files, and list what's there. They're safe to run again.
mkdir -p ~/terminal-practice/packing && cd ~/terminal-practice/packing
mkdir -p photos/beach photos/city videos music documents/2026/summer-trip/tickets
printf 'passport\nphone charger\nsunscreen\nheadphones\n' > packing-list.txt
head -c 2500000 /dev/urandom > photos/beach/sunset.jpg
head -c 2100000 /dev/urandom > photos/beach/waves.jpg
head -c 1800000 /dev/urandom > photos/city/museum.jpg
head -c 1600000 /dev/urandom > photos/city/street-food.jpg
head -c 15000000 /dev/urandom > videos/drone-flight.mp4
head -c 4000000 /dev/urandom > music/road-trip-mix.mp3
printf 'Flight to Lisbon, June 12, seat 14C\n' > documents/2026/summer-trip/tickets/flight.txt
printf 'Train to Porto, June 15, coach 4\n' > documents/2026/summer-trip/tickets/train.txt
printf 'Hotel Sol, June 12 to 15, booking 4471\n' > documents/2026/summer-trip/hotel.txt
ls
documents music packing-list.txt photos videos
The head -c lines give each stand-in photo and video a real size. /dev/urandom is an endless supply of random bytes that Linux and macOS both have; head -c 2500000 takes the first 2,500,000 of them (-c counts bytes, where plain head counts lines), and > saves them as the file.
du: how much space does it take?
du stands for disk usage. Give it -s (for summary: one total, not every folder inside), -h (for human-readable, the same -h as in ls -lh) and a folder's name:
du -sh photos
7.7M photos
The four photos take 7.7 megabytes. To see everything in the folder at once, use * (read "star"), which stands for every name here, as in Wildcards: *, ? and [ ]:
du -sh *
28K documents
3.9M music
4.0K packing-list.txt
7.7M photos
15M videos
One line per name, size first. Two things look odd at first. packing-list.txt holds only 44 bytes, yet du says 4.0K: a disk hands out space in blocks, usually 4 kilobytes each, and even a tiny file takes a whole block. And the list is in alphabetical order, so in a folder of 200 names the big one could be anywhere.
Biggest first: sort -h and a first pipe
The fix is to hand du's list to sort, which puts lines in order. The | between them (read "pipe") works like the pass in a restaurant kitchen: one cook sets a finished plate on it, and the next cook picks it up. du's lines go to sort instead of to the screen:
du -sh * | sort -h
4.0K packing-list.txt
28K documents
3.9M music
7.7M photos
15M videos
-h tells sort to read sizes the human way, so K comes before M, and M before G. Add -r (for reverse) to put the biggest first:
du -sh * | sort -hr
15M videos
7.7M photos
3.9M music
28K documents
4.0K packing-list.txt
There's the answer: one video takes more space than all four photos together. Leave out the -h and sort compares the sizes as text, character by character, so 15M lands above 28K because 1 comes before 2. Pipes can do a lot more than this; Pipes: |, in Module 3, takes them apart.
To look one level deeper, leave out -s. du then lists every folder inside, with the total last:
du -h photos
3.3M photos/city
4.4M photos/beach
7.7M photos
df: how much room is left?
du measures what's in your folders; df (disk free) measures the drive itself. df -h . reports on the drive that holds the folder you're in (. is "this folder"):
df -h .
Filesystem Size Used Avail Use% Mounted on
/dev/sdd 1007G 30G 926G 4% /
Your numbers and names will be different. Read the line left to right: the drive's name, its size, how much is used, how much is available, the used part as a percentage, and the folder where it appears ("mounted on"). Linux calls each of these a file system: a drive, or part of one, set up to hold files. A "disk almost full" warning means Use% is close to 100%.
On its own, df -h lists every file system, including small ones the system keeps for itself, so expect a long list. In WSL it also shows your Windows drives, such as C:\ mounted on /mnt/c. On a Mac, the volume that holds your files is usually the one mounted on /System/Volumes/Data; du -sh and sort -h work the same there.
du and df only read sizes; they never move or delete anything. So try them on your real files: du -sh ~/* | sort -hr lists everything in your home folder, biggest first. On a big home folder it can take a moment.
Check yourself
2 questions to try on the lesson page.
So what
"Disk almost full" stops being a mystery: du -sh * | sort -hr points straight at what's taking the space, and df -h tells you how much room you've won back.