Find out what takes space on the hard drive, in a Linux/Unix machine

I was struggling with a machine that was completely out of space. Here are some ways to find what is using the most storage.

Find out the system capacity and available disk space

Use the df "disk filesystem" (or "disk free") command to see how much free space there is on the disk(s):

$ df -H
Filesystem      Size  Used Avail Use% Mounted on
udev            1.1G  8.2k  1.1G   1% /dev
tmpfs           210M  418k  210M   1% /run
/dev/vda1        32G   21G  9.0G  71% /

This will show all mounted filesystems. Get the usage of an individual filesystem by referring to it by name:

$ df -H /dev/vda1

Find out the size of a file

The ls command coupled with "the human readable", "list", and "all" flags is great for gauging file sizes in a directory:

$ ls -lah

That’ll show hidden files, too.

The du (disk use) command

The "disk use" command shows the size of a file:

$ du -h archive.zip
800M    archive.zip

Find out size of a directory

Combine the du command with the "summarize" -s, --summarize flag to show the size of a directory:

$ du -sh /var/www
8.9G    /var/www
-s, --summarize
Display only a total for each argument.
-h, --human-readable
Print sizes in human readable format (e.g., 1K 234M 2G).

The next one lists the sizes of the 10 largest directories in the current working directory, in human readable form:

$ cd /var/www
$ du -hsx * | sort -rh | head -10

Source

Find packages that take a lot of space

In Debian systems you can use the aptitude command to sort by installsize, and then pipe it to the tail command to get a reasonable amount of results. This churns out the packages in ascending order:

$ aptitude search "~i" --display-format "%p %I" --sort installsize | tail -10
linux-headers-3.8.0-29                    60.1 MB
libwireshark3                             62.9 MB
linux-headers-3.13.0-51                   63.4 MB
linux-headers-3.13.0-161                  63.6 MB
libwireshark11                            83.9 MB
linux-firmware                             127 MB
linux-image-extra-3.13.0-51-generic       152 MB
linux-image-extra-3.13.0-161-generic      155 MB
linux-image-3.8.0-29-generic              176 MB
mongodb-org-tools                         238 MB

Tweak the number in tail to get more results.

Check for MySQL databases

Maybe there’s a bloated database using the lion’s share of the space. Log in to the MySQL prompt to check:

$ mysql -u root -p

See if you have too many databases, or something like that:

SHOW DATABASES;

Or get a database size in human readable form:

SELECT table_schema AS "db_name", SUM(data_length + index_length) / 1024 / 1024 / 1024 AS "Size (GB)" FROM information_schema.TABLES GROUP BY table_schema

I’ve got a post with simple MySQL operations, if you’re interested.

Check the size of logs

Just to be sure that logs haven’t bloated out of control for some reason, the /var/log can be checked:

$ du -sh /var/log
629M    /var/log