See what disk space is left on your Linux web server

How to find out how much disk space is left on your Linux web server

Just like any other computer your web server can get cluttered up and eventually run out of space.

Common reasons this happens on web servers include;

  • Large assets going unchecked (uploading video files etc. onto the web server)
  • Backup files not getting rotated correctly
  • Log files not getting rotated correctly

In this short tutorial I am going to show you some quick ways to see how much disk space is left and how to hone in on the biggest offenders.

Finding out how much space you have left

To find out how much space you have left we can run the df command.

From any directory just type df and hit enter, it will quickly return some results and exit;

Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/xvda1     607919632 182906704 412660724  31% /
udev             7919944         4   7919940   1% /dev
tmpfs            3171152       196   3170956   1% /run
none                5120         0      5120   0% /run/lock
none             7927872         0   7927872   0% /run/shm

This can look daunting at first, but we can soon tidy that up.

The first thing is the amount of rows, we normally only have one disk that we care about, by default the df command returns all disks that it can find.

In our case it is the top disk (/dev/xvda1) – we can guess this because it is mounted to / and has the most space allocated to it and in use.

This means next time we can run df /dev/xvda1 and just get back the row we care about;

Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/xvda1     607919632 182909320 412658108  31% /

Much easier to work with.

Now we need to do something about those numbers, they are fairly meaningless. df has an option that can help us with that. -h will turn those numbers into something that is human readible.

So we can type df -h /dev/xvda1;

Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1      580G  175G  394G  31% /

Great, that is exactly what we want, we can see at a glance the size of the drive, how much we have used and how much we have left.

Narrowing down on large files

In the above example we have plenty of space, there is no need for us to go looking for things to remove, but lets assume we were running low on space and wanted to free some up.

There are a few ways you can do this, nearly every result in Google shows a slightly different method, this is what I like to do.

You will normally have a fairly good idea of where files are likely to build up. On your local computer it could be the Downloads directory, on your web server it could be your main website directory, lets start there.

Once you have cd /location/to/my/website you can run the following command;

du -h --max-depth=1

du is the Disk Usage command and we have passed two options into it, the first is -h which we saw earlier, it will give us human readable numbers as output. The --max-depth=1 tells it to only go one level deep and sum up the size of things any lower than that.

When I run this on a rails application I see the following;

68K	./lib
1.2M	./db
8.0K	./doc
96K	./spec
8.0K	./script
216K	./config
119M	./.git
4.0K	./backups
16M	./public
8.0K	./.bundle
5.5M	./app
44K	./vendor
1.4G	./tmp
1.6G	.

The bottom row is the culmination of all the other rows and is telling us the there is 1.6G of data stored in the directory I am in.

Each other row tells us the amount of data in each subdirectory. It looks like ./tmp has some explaining to do;

du -h --max-depth=1 tmp/

Gives us the following output;

2.5M	tmp/assets
1.4G	tmp/cache
1.4G	tmp/

That isn’t so bad, we use caching quite a bit in this particular application, had we not then we would have found a perfect place to begin a cull of data!


Recent posts View all

Ruby

Forcing a Rails database column to be not null

How you can force a table column to always have something in it with Rails

Writing Marketing

We've deleted an article's worth of unhelpful words

We've improved several pages across our site by removing words that add no value, and often detract from the article.