Linux system disk space

Classic SysAdmin: How to Check Disk Space on Linux from the Command Line

This is a classic article written by Jack Wallen from the Linux.com archives. For more great SysAdmin tips and techniques check out our free intro to Linux course.

Quick question: How much space do you have left on your drives? A little or a lot? Follow up question: Do you know how to find out? If you happen to use a GUI desktop (e.g., GNOME, KDE, Mate, Pantheon, etc.), the task is probably pretty simple. But what if you’re looking at a headless server, with no GUI? Do you need to install tools for the task? The answer is a resounding no. All the necessary bits are already in place to help you find out exactly how much space remains on your drives. In fact, you have two very easy-to-use options at the ready.

In this article, I’ll demonstrate these tools. I’ll be using Elementary OS, which also includes a GUI option, but we’re going to limit ourselves to the command line. The good news is these command-line tools are readily available for every Linux distribution. On my testing system, there are a number of attached drives (both internal and external). The commands used are agnostic to where a drive is plugged in; they only care that the drive is mounted and visible to the operating system.

With that said, let’s take a look at the tools.

df

The df command is the tool I first used to discover drive space on Linux, way back in the 1990s. It’s very simple in both usage and reporting. To this day, df is my go-to command for this task. This command has a few switches but, for basic reporting, you really only need one. That command is df -H. The -H switch is for human-readable format. The output of df -H will report how much space is used, available, percentage used, and the mount point of every disk attached to your system (Figure 1).

What if your list of drives is exceedingly long and you just want to view the space used on a single drive? With df, that is possible. Let’s take a look at how much space has been used up on our primary drive, located at /dev/sda1. To do that, issue the command:

The output will be limited to that one drive (Figure 2).

You can also limit the reported fields shown in the df output. Available fields are:

  • source — the file system source
  • size — total number of blocks
  • used — spaced used on a drive
  • avail — space available on a drive
  • pcent — percent of used space, divided by total size
  • target — mount point of a drive
Читайте также:  Civilization 5 linux вылетает

Let’s display the output of all our drives, showing only the size, used, and avail (or availability) fields. The command for this would be:

The output of this command is quite easy to read (Figure 3).

The only caveat here is that we don’t know the source of the output, so we’d want to include source like so:

df -H --output=source,size,used,avail

Now the output makes more sense (Figure 4).

du

Our next command is du. As you might expect, that stands for disk usage. The du command is quite different to the df command, in that it reports on directories and not drives. Because of this, you’ll want to know the names of directories to be checked. Let’s say I have a directory containing virtual machine files on my machine. That directory is /media/jack/HALEY/VIRTUALBOX. If I want to find out how much space is used by that particular directory, I’d issue the command:

du -h /media/jack/HALEY/VIRTUALBOX

The output of the above command will display the size of every file in the directory (Figure 5).

So far, this command isn’t all that helpful. What if we want to know the total usage of a particular directory? Fortunately, du can handle that task. On the same directory, the command would be:

du -sh /media/jack/HALEY/VIRTUALBOX/

Now we know how much total space the files are using up in that directory (Figure 6).

You can also use this command to see how much space is being used on all child directories of a parent, like so:

The output of this command (Figure 7) is a good way to find out what subdirectories are hogging up space on a drive.

The du command is also a great tool to use in order to see a list of directories that are using the most disk space on your system. The way to do this is by piping the output of du to two other commands: sort and head. The command to find out the top 10 directories eating space on a drive would look something like this:

du -a /media/jack | sort -n -r | head -n 10

The output would list out those directories, from largest to least offender (Figure 8).

Not as hard as you thought

Finding out how much space is being used on your Linux-attached drives is quite simple. As long as your drives are mounted to the Linux system, both df and du will do an outstanding job of reporting the necessary information. With df you can quickly see an overview of how much space is used on a disk and with du you can discover how much space is being used by specific directories. These two tools in combination should be considered must-know for every Linux administrator.

Читайте также:  Read only file system kali linux

And, in case you missed it, I recently showed how to determine your memory usage on Linux. Together, these tips will go a long way toward helping you successfully manage your Linux servers.

Источник

Check and Clean a Linux System’s Disk Space

Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.

Linux provides several built-in commands for analyzing and cleaning up your system’s disk space. This guide shows you how to use those commands to get a closer look at your disk usage and start freeing up space.

How Do I Check Disk Space on Linux?

Linux systems have two commands readily available for checking your disk space. These commands provide a high-level view of your whole system’s available disk space and the disk usage within particular directories.

How to Check Linux Disk Space with the df Command

Use the df command to view your system’s available disk space for each drive.

Filesystem 1K-blocks Used Available Use% Mounted on udev 4031204 0 4031204 0% /dev tmpfs 815276 952 814324 1% /run /dev/sda 164619468 3091188 153149572 2% / tmpfs 4076368 0 4076368 0% /dev/shm tmpfs 5120 0 5120 0% /run/lock tmpfs 4076368 0 4076368 0% /sys/fs/cgroup tmpfs 815272 0 815272 0% /run/user/1000

The df command (short for “disk free”) shows each drive’s disk size, space used, and free space. Each “block” in the above output represents one kilobyte.

To make the output from df easier to read, you can add the -h option. This option displays disk space in kilobytes (K), megabytes (M), and gigabytes (G).

Filesystem Size Used Avail Use% Mounted on udev 3.9G 0 3.9G 0% /dev tmpfs 797M 952K 796M 1% /run /dev/sda 157G 3.0G 147G 2% / tmpfs 3.9G 0 3.9G 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup tmpfs 797M 0 797M 0% /run/user/1000

You can also use the df command to target a specific drive, using either its “Filesystem” or “Mounted on” description from the columns above.

Filesystem Size Used Avail Use% Mounted on /dev/sda 157G 3.0G 147G 2% /

The above command is equivalent to sudo df -h / .

How to Check Linux Disk Space with the du Command

Use the du command to analyze disk space at a more granular level. This command summarizes the space usage for a specified directory or the current directory if none is specified.

4 /etc/systemd/system/sockets.target.wants 4 /etc/systemd/system/sysinit.target.wants 4 /etc/systemd/system/timers.target.wants 4 /etc/systemd/system/multi-user.target.wants 100 /etc/systemd/system 16 /etc/systemd/network 4 /etc/systemd/user/sockets.target.wants 8 /etc/systemd/user 164 /etc/systemd

The du command lists all of the files and directories in the target directory and displays their disk usage in kilobytes.

The last entry in the list is always the target directory itself, giving you a summary of the directory’s disk usage. You can isolate disk space information for the target directory by using the -s option. This is a useful option for directories with many files and subdirectories.

Читайте также:  What is x client in linux

As with the df command, you can make the output easier to read with the -h option. This causes the disk space to be displayed in kilobytes (K), megabytes (M), and gigabytes (G). In the example below, the -h option is used in combination with the -s option.

How Do I Clean Disk Space on Linux?

You may need to clean your disk space on Linux because you need space to install additional software. Another reason may be that you received a warning that your system’s disk space is critically low. It is likely that at some point you may need to free up disk space on your Linux system.

The best place to start is usually with your Linux package manager. Each package manager offers options to quickly and easily clear out space from unused or unnecessary packages and related data.

How to Remove Unnecessary Packages

Most major package managers include an autoremove command. This command automatically removes packages that are no longer in use. These packages are typically ones that were originally installed as dependencies for other packages.

With Debian and Ubuntu distributions, you can use APT’s version of the command:

Likewise, on AlmaLinux and CentOS, you can use the command with yum :

And the same applies to Fedora’s DNF package manager:

How to Clear the Package Cache

Linux package managers generally also include a clean command. This command clears the cache used by the package manager. It can also be a helpful command if you are having package errors due to corrupted metadata.

For Debian and Ubuntu, use the command below.

APT also has an autoclean command. This command clears the cache for outdated packages that can no longer be downloaded from APT’s repositories.

Both YUM and DNF require you to specify what you want to be cleared from the cache. The most helpful options are metadata , packages , and all . As an example, here is the YUM command for clearing all of the cached data.

How to List and Remove Unwanted Packages

If you still need space, you may want to look at your installed packages and start deciding which ones you no longer need.

    List the packages you have installed.
    For Debian and Ubuntu, use the apt command below:

Before removing packages, ensure that they are not required by the system. Usually, you can safely remove packages that you installed, but be cautious of removing packages that you do not recognize.

Next Steps

Still looking for more disk space? You may want to think about getting additional space for your Linux system. You can follow our Resizing a Linode guide to learn how to increase your Linode’s plan size.

This page was originally published on Friday, August 13, 2021.

Источник

Оцените статью
Adblock
detector