Filesystem and Directory size

By: Luke Rawlins Jun 22, 2016 | 3 minutes read
Share this:

Tag: 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.

From the terminal enter the df command.

[email protected]:~$ 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.

[email protected]:~$ 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

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.

[email protected]:~$ 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?

[email protected]:~$ 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.

Pipe the output to sort

[email protected]:~$ 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

Related Posts


Media Server

Have you ever wanted to set up your own video streaming service on your home or work network? This simple guide will help you set up a media server using Ubuntu 14.04 and Plex. The setup for Plex on Ubuntu is incredibly easy and is a great way to back up your existing video, music and picture library in a way that will allow you to share the content with anyone on or off your network. Read more

Linux DNS and DHCP Server

There are lots of reasons to use Linux for your networking needs both at home and at work not the least of which is the unbeatable price (free). Linux has a well deserved reputation for security and high availability that is unrivaled among modern operating systems. Setting up a dhcp and dns server with Linux is not as hard as you might think especially when using a package called “dnsmasq”. Dnsmasq is a lightweight package that is available from the default Ubuntu repositories. Read more


Contact

If you’d like to get in touch, contact with me via email - or follow on Twitter.

[email protected]