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;
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;
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
;
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
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;
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;
Gives us the following output;
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!