Hard Disk and Space Management on Linux: Reference & Examples

Last updated:
Table of Contents

Here are some commands that can help you view the state of your hard disk:

You should probably run these commands as root (or using sudo)

Disk Usage by Partition

$ sudo df -h

Example output

Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/ubuntu1264-root  28G    17G   11G  62% /
udev                         988M  4.0K  988M   1% /dev
tmpfs                        199M  276K  199M   1% /run
none                         5.0M     0  5.0M   0% /run/lock
none                         995M     0  995M   0% /run/shm
/dev/sda1                    228M   71M  145M  33% /boot

Disk Usage by Directory

To see where your disk space has gone

$ sudo du -h / | sort -h

Example output (some lines ignored)

14G  /home/johndoe/Downloads
15G  /home/johndoe
16G  /home
35G  /var/lib 
40G  /var
99G  /

Size of each directory under /path/to/directory

This might take up to a couple of minutes, depending on how large/fast your disk is

This is a slightly modified version of the first example, where you specify a directory rather than the whole disk.

$ sudo du -h /home/johndoe/ | sort -h

Example output (some lines ignored)

27M  /home/johndoe/Downloads/yii_13
35M  /home/johndoe/3bsa.git/objects/pack
44M  /home/johndoe/3bsa.git/objects
45M  /home/johndoe/3bsa.git
74M  /home/johndoe/Downloads/elasticsearch-1.2.1
204M /home/johndoe/Downloads
1.4G /home/johndoe/

Disk usage by directory, limit subdirectory depth

Set flag --max-depth=<MAX_DEPTH>.

This makes it easier to analyze output:

$ sudo du -h --max-depth=2 / | sort -h

Example output (only some lines displayed)

3,0G    /var/www
3,4G    /usr/local
4,6G    /usr/lib
6,5G    /var
13G /usr
58G /media
58G /media/felipe
400G    /home
400G    /home/felipe
479G    /

Number of files in directory

List the files in directory and pipe the output to wc -l

Example: count number of files in current directory (excluding subdirectories)

$ find . -maxdepth 1 -type f | wc -l

To include subdirectories recursively, just omit the -maxdepth modifier.

$ find . -type f | wc -l


Dialogue & Discussion