Just some guy on the internet.


authors: [“Luke Rawlins”]
date: 2016-06-22
draft: false
title: Filesystem and Directory size
url: /filesystem-and-directory-size/
tags:
– Linux


Just a quick look at df and du. This comes up a lot when we have filesystems that are filling up and need to find out which directories or logs are using the space.

How to find the size of mounted filesystems

From the terminal enter the df command.

luke@testserver:~$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 492M 12K 492M 1% /dev
tmpfs 100M 780K 99M 1% /run
/dev/xvda1 15G 3.1G 11G 22% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
none 5.0M 0 5.0M 0% /run/lock
none 497M 0 497M 0% /run/shm
none 100M 0 100M 0% /run/user

According to its man page df “displays the amount of disk space available on the file system” adding the -h argument tells df to display in human readable format.

Adding a “-T” notice caps will tell df to also display the filesystem type.

luke@testserver:~$ df -hT
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 492M 12K 492M 1% /dev
tmpfs tmpfs 100M 780K 99M 1% /run
/dev/xvda1 ext4 15G 3.1G 11G 22% /
none tmpfs 4.0K 0 4.0K 0% /sys/fs/cgroup
none tmpfs 5.0M 0 5.0M 0% /run/lock
none tmpfs 497M 0 497M 0% /run/shm
none tmpfs 100M 0 100M 0% /run/user

What if you are running out of space and you are not sure which directories or files are using up your hard disk?

The “du” command will give you the size of files and directories. Here's a few quick and useful examples of how to use du to determine file size.

Finding directory sizes

luke@testserver:~$ du -h
4.0K ./.local/share/applications
4.0K ./.local/share/sounds
16K ./.local/share
20K ./.local
8.0K ./.gconf/apps/gnome-terminal/profiles/Default
12K ./.gconf/apps/gnome-terminal/profiles
16K ./.gconf/apps/gnome-terminal
20K ./.gconf/apps
24K ./.gconf
8.0K ./.ssh
8.0K ./.dbus/session-bus
12K ./.dbus
24K ./.vnc
4.0K ./.config/ibus/bus
8.0K ./.config/ibus
32K ./.config/pulse
12K ./.config/dconf
56K ./.config
108K ./.cache/fontconfig
128K ./.cache
4.0K ./blog/dir1
4.0K ./blog/dir2
12K ./blog
316K .

du -h shows the size of files and directories in a human readable format. Running du with no parameters will cause it to display the current directory only. By default du will show the size of subdirectories. What if we just wanted to see one level deep in the / directory?

Use the —max-depth argument to control subdirectory depth

luke@testserver:~$ sudo du -h —max-depth=1 /
25M /boot
56K /root
780K /run
62M /lib
12K /tmp
12M /sbin
9.6M /bin
4.0K /lib64
4.0K /srv
12K /dev
4.0K /media
0 /proc
16K /lost+found
8.9M /etc
4.0K /mnt
368K /home
0 /sys
1.7G /usr
128M /opt
1.2G /var
3.0G /

Notice you might get some errors about /proc access. That is because files in /proc represent the live system and in some cases they will be available when du starts but gone by the time it finishes.

What if I want to sort by the largest directories first?

Pipe the output to sort

luke@testserver:~$ sudo du -h —max-depth=1 / | sort -hr
3.0G /
1.7G /usr
1.2G /var
128M /opt
62M /lib
25M /boot
12M /sbin
9.6M /bin
8.9M /etc
780K /run
368K /home
56K /root
16K /lost+found
12K /tmp
12K /dev
4.0K /srv
4.0K /mnt
4.0K /media
4.0K /lib64
0 /sys
0 /proc