Hard Disk and Space Management on Linux: Reference & Examples

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

Get current partition

Use $ findmnt --target .

findmnt-to-get-current-partitions Current partition is /dev/sda2/

Size of current partition

Call df -h . to instruct df to run on the current directory:

Example: get size and space left in current partition

$ df -h .
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       117G   82G   29G  75% /

Size of each directory under path

This might take up some time, depending on how fast your disk is

Example Size of each directory under /home/johndoe/:

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

Output:

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/

Size by directory, limit depth

Set flag --max-depth=<MAX_DEPTH> to limit subdirectory depth.

$ 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

No Recursion

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

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

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

Recursively

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

$ find . -type f | wc -l
8381

References