> ## Knowledge Base Index
> Fetch the complete knowledge base index at: https://wiki.volthosting.co.uk/sitemap.xml
> Use this file to discover available pages before exploring further.
> Pure-Markdown content can be obtained by appending a '.md' suffix to the content URLs listed in the sitemap (without the trailing slash).

# Figure out Disk Space Usage on Linux

|| Looking for a [Virtual Private Server](https://volthosting.co.uk/vpshosting.html) or [Dedicated Server](https://billing.volthosting.co.uk/index.php?rp=/store/dedicated-servers)? Take a look at VoltHosting's [VPS](https://volthosting.co.uk/vpshosting.html) or [Dedicated Server](https://billing.volthosting.co.uk/index.php?rp=/store/dedicated-servers) plans.

Ran out of disk space on your Linux based machine? Thankfully you can figure out what is using up all the space! Follow this guide below to see what is using up all the space on your Linux based machine (such as Ubuntu). 

# The Disk Filesystem Command

Firstly, lets take a look at the `df` command to check disk space usage on a Linux system. You can use this command to show disk usage and free disk space for a specific file system.

Here’s an example of the output generated when running the `df` command on an Ubuntu server. I’ve added the `-h` argument, so the space usage is showing in a human-readable format.

```
root@volthosting-server:~# df -h
```
| Filesystem | Size | Used | Avail | Use% | Mounted on |
| ---- |
| udev | 71G | 0 | 71G | 0% /dev |   |
| tmpfs | 15G | 7.2M | 15G | 1% | /run |
| /dev/mapper/pve-root | 94G | 85G | 4.4G | 96% / |   |
| tmpfs | 71G | 49M | 71G | 1% | /dev/shm |
| tmpfs | 5.0M | 0 | 5.0M | 0% | /run/lock |
| /dev/fuse | 128M | 36K | 128M | 1% | /etc/pve |
| tmpfs | 15G | 0 | 15G | 0% | /run/user/0 |
*Note: it won't show in a table like this, this was done in the documentation to provide you with a mobile friendly viewing experience. But the outputted data will be similar to this.*

The `df` command is excellent for listing all available hard drive mount points. However, you will generally only need to examine a specific mount (such as `/`) when searching for disk space usage. So, usually the `du` command is much more helpful.

# The Disk Usage Command

All Ubuntu distributions have a disk usage ( `du`) command. This command-line tool estimates the disk usage in a specified directory. It does this by first summarizing any directory tree (the hierarchy of directories) in the given directory, followed by a usage summary of all the files in that directory.

## Using the Disk Usage Command

Running the `du` command on a specific directory without any arguments will display the summary for that directory.

To ensure that the disk usage command has the required root user permissions to read the directories you are pointing it to, you can prefix any `du` command with `sudo`. (We've added the `-h`  flag to keep the output in human readable format).
```bash
sudo du -h /home/volthosting/
```

Below is an example of the output you might get running the command to check the disk space:

```
sudo du -h /home/volthosting/

4.0K    /home/volthosting/.ssh
88K     /home/volthosting/webserver-stuff
8.7G    /home/volthosting/backups
1.2G    /home/volthosting/backups/rowans-things
4.0K    /home/volthosting/logs
4.0K    /home/volthosting/important-files
2.0K    /home/volthosting/important-files/really-cool-stuff
```

Here we can see all the directories in the `/home/volthosting/` directory. 

This will traverse through the directory tree for the home directory, showing the path to each directory it finds and summarizes the total disk usage of that directory.
Its important to note that this also shows hidden folders (beginning with a `.`). These usually aren't disk space hoggers, but worth keeping in mind. 

Another way we can do this, is to include the `-s` argument, which will limit the results to the estimates for the top level directories only. 

```bash
sudo du -sh /home/volthosting/
```

Below is an example of the output you might get running the command with the `-sh` flag:

```
sudo du -sh /home/volthosting/

4.0K    /home/volthosting/.ssh
88K     /home/volthosting/webserver-stuff
8.7G    /home/volthosting/backups
4.0K    /home/volthosting/logs
4.0K    /home/volthosting/important-files
```

### Filtering the Results

Being able to filter the results returned is very helpful in order to see what actually is using up all the space! To do this, we can add a filter to the `du` output using the `sort` command.

We use the pipe `|` character to send the `du` output to the `sort` command, and then add the `-h` and `-r` arguments to it to sort the data by human-readable numbers, and reverse the results, showing largest to smallest.

We've also added the wildcard symbol `*` to the end of the `du` command to refine the results a little more, as it excludes the dot (hidden) files and folders, it does not return the total summary for the entire home directory.

```bash
sudo du -sh /home/volthosting/* | sort -hr
```

Below is an example of the output you might get running the command with the `-sh` flag:

```
sudo du -sh /home/volthosting/* | sort -hr

8.7G    /home/volthosting/backups
88K     /home/volthosting/webserver-stuff
4.0K    /home/volthosting/logs
4.0K    /home/volthosting/important-files
```

### Hunting for the disk space users!

Now that we've learnt what the `du` and `sort` commands do, its time to put them together to find the space hog on your system.

Firstly, lets take a look at the root `/` directory on your server.

```
sudo du -sh /* | sort -hr
```

Depending on your server’s hard disk size and the amount of disk space in use, this could take a little while. Below is a simplified example of what the output might look like:
```
19.8G   /volthosting
1.9G    /var
1.7G    /usr
503M    /snap
145M    /boot
12M     /opt
7.8M    /etc
4.0M    /cache
3.0M    /home
1.6M    /root
992K    /run
64K     /tmp
16K     /lost+found
4.0K    /srv
4.0K    /mnt
4.0K    /media
```

As you can see, the `/volthosting` directory is taking up 19.8GB of space and is probably where the problem lies. So your next logical step would be to update the command to report on the `/volthosting` directory:

```
sudo du -sh /volthosting/* | sort -hr
```

This is an example of what this could return: 

```
16.2G   /volthosting/volthosting-main-website
1.8G    /volthosting/development-files
1.0G    /volthosting/discord-bot
164M    /volthosting/misc
```

In this example, `/volthosting/volthosting-main-website` is taking up a whopping 16.2G of space. Next would be to see what’s happening inside `/volthosting/volthosting-main-website`:

```
sudo du -sh /volthosting/volthosting-main-website/* | sort -hr
```

You can continue running `du` in this way on subsequent folders until you find the culprit.

| It will (probably not) be the `/volthosting` directory on your server taking up all the space. You should check for yourself which folder it actually is. For example, it could be `/var` or `/opt`.

## Cleaning up the Files

Once you've located files or directories which you want to relate, firstly **make sure that they are unneeded, or backed up somewhere else BEFORE deleting**. Once a file is deleted, there is no coming back.

There is one command which does all the deleting, and that is the `rm` command. 

For deleting a specific file:
```
sudo rm /volthosting/volthosting-main-website/a-big-file.txt
```
This command will delete the `a-big-file.txt` document.

For deleting a whole directory:
```
sudo rm -rf /volthosting/volthosting-main-website/logs
```
This will delete the `logs` directory.

# The ncdu command

This command is the simplest of them all, but requires you to install additional software to your device. 

## Installing ncdu

You can install `ncdu` easily as its usually included in the default package repositories on most Linux distributions. 

| Command | Supported Linux distribution |
| ---- |
| `sudo apt install ncdu` | On Debian, Ubuntu and Mint |
| `sudo yum install ncdu` | On RHEL/CentOS/Fedora and Rocky/AlmaLinux |
| `sudo emerge -a sys-apps/ncdu` | On Gentoo Linux |
| `sudo apk add ncdu` | On Alpine Linux |
| `sudo pacman -S ncdu` | On Arch Linux |
| `sudo zypper install ncdu` | On OpenSUSE |
| `sudo pkg install ncdu` | On FreeBSD |

## Using ncdu

Once installed, just type `ncdu` into the terminal to launch the program. It will firstly scan the number of files and directories, and provide the information on disk usage for the current directory. 

Feel free to run this command in the `/` directory if you're unsure on where the disk hogs are.

![It may show a screen like this for a little while, let it load and figure out the directory sizes.](https://storage.crisp.chat/users/helpdesk/website/a7f74f9f83d47000/hqew1bkzeampjsrin985khgnhlblqg_we7src.png)

Once, scanning completes, it will present the tree structure of files and folders along with their disk usage in human-readable format with a graphical bar presentation.

![](https://storage.crisp.chat/users/helpdesk/website/a7f74f9f83d47000/0kmcu2cazcndqts9lkch3tm0ieep3y_q3aoer.png)

Use the arrow keys to navigate through directories and press `i` to see the selected directory information like full path, disk usage, apparent size. Again, hit `i` to hide the window.

![This is the result if the i key is pressed while over the /var directory.](https://storage.crisp.chat/users/helpdesk/website/a7f74f9f83d47000/vld4qe9x1sbvy0mlv5h7253q3jtvop_14asj7c.png)

Now, find where your disk hog is, and delete the files! You can simply click `d` on the directory or file you want to delete, and press `Yes` to delete it. 

||| Please make sure to check that the file is unneeded, or backed up. Once a file is deleted, there's no coming back!

![](https://storage.crisp.chat/users/helpdesk/website/a7f74f9f83d47000/gy0ac7fk4qolwebgv0eidmktx7idi2_1hhb0ir.png)

To get more information about ncdu's options, press `Shift+?` and use the arrow keys to move up and down for more options.

![](https://storage.crisp.chat/users/helpdesk/website/a7f74f9f83d47000/40j0u6zguy9vctr94jb1tzzxbcyc2z_1tmu2dc.png)

To leave the interface, simply press `q`.