Disk Space Command in Linux

Learn via video courses
Topics Covered

Linux disk space commands like df, du, and ncdu are crucial for managing and monitoring disk usage. They help users optimize storage, pinpoint large files, and maintain efficient disk management. This article will delve into popular Linux disk commands and guide you on their effective usage. With a basic understanding of these commands, let's explore each one in more detail.

Disk Space Commands in Linux

Disk space commands in Linux are the most important tools for keeping track of and controlling how your system uses its disk space. They tell you important things about how much space is free, how much space is being used, and how the filesystem is being used.

Let's find out the details of the most popular disk space command in Linux.

df (Disk Free) Command

The df (Disk Free) command is a widely used disk space command in Linux that shows how much free and used disk space is on mounted filesystems. It shows the filesystem, the total size, the space that is used, the space that is accessible, and the percentage of space that is used. The df command can be used to track how much disk space is being used and find out what amount of space is available on different partitions or filesystems.

Here is the syntax of the df command:

df command

Some commonly used options with the df command include:

OptionsObjective
-h or --human-readableDisplays sizes in a human-readable format (e.g., "1.5G" for 1.5 gigabytes)
-T or --print-typePrints the filesystem type along with the disk space information
-t or --type=FILESYSTEM-TYPEDisplays disk space usage for specific filesystem types.
-x or --exclude-type=FILESYSTEM-TYPEExcludes specific filesystem types from the output

Now, let's look at some examples of using the df command:

Display disk space usage for all mounted filesystems:

Display disk space usage in human-readable format:

Display disk space usage for a specific filesystem or directory:

Display disk space usage only for specific filesystem types (e.g., ext4):

Exclude specific filesystem types (e.g., tmpfs) from the output:

df command

The df command shows disk space used on multiple filesystems and helps uncover problems like low disk space or high utilization. The df command lets you optimize the disk to use, ensure system stability, and prevent performance issues by routinely monitoring disk space.

du (Disk Usage) Command

The du (Disk Usage) command in Linux is used to estimate and show how much space files and directories are taking up on the hard drive. It lets you know how much space a file or directory is taking up on your filesystem. The du command tells you how many disk blocks are used by the files or folders you specify, as well as their total size and apparent size and that is why it is one of the most important disk space commands in Linux

Here is the syntax of the du command:

du command

Some commonly used options with the du command include:

OptionsObjective
-h or --human-readable:Displays sizes in a human-readable format (e.g., "1.5G" for 1.5 gigabytes)
-s or --summarizeDisplays only the total disk space used by the specified files or directories, excluding individual sizes
-c or --totalPrints a grand total of disk space used by all specified files or directories
-a or --allDisplays disk usage for all files and directories, including hidden ones
-x or --one-file-systemLimits the command to a single filesystem and does not cross filesystem boundaries

Now, let's look at some examples of using the du command:

Display the disk space usage of a specific directory:

Display the disk space usage in human-readable format:

Display the total disk space used by a directory and its subdirectories:

Display the disk space usage of multiple directories and get a total:

du command

Display the disk space usage of all files and directories, including hidden ones:

du command

The du command helps to find space-consuming files and directories, their sizes, and which folders occupy the most disk space. The du command lets you manage disk space, optimize storage utilization, and finds spaces in the Linux system.

ncdu (NCurses Disk Usage) Command

In Linux, the ncdu (NCurses Disk usage) command is a potent disk utilization analyzer that offers an interactive and simple method of viewing and controlling disk space usage. You can move across folders, check disk utilization information, and perform actions like deleting unnecessary files or directories due to the information's hierarchical presentation. When you wish to check the utilization of disk space on a distant server or prefer a visual representation of disk usage, the ncdu command is particularly helpful. This is also a very essential disk space command in Linux

Here is the syntax to use the ncdu command:

ncdu command

Some commonly used options with the ncdu command include:

OptionsObjective
-q or --quietRuns ncdu in quiet mode, suppressing the display of information during scanning
-r or --confirm-quitAsks for confirmation before quitting the ncdu interface
-x or --exclude PATTERNExcludes files or directories matching the specified pattern from the scan
-o or --order BYOrders the display by size (default), name, or time
-s or --siUses SI units (powers of 1000) instead of binary units (powers of 1024) for displaying sizes

Now, let's look at some examples of using the ncdu command:

Analyze disk space usage interactively in the current directory:

Analyze disk space usage in a specific directory:

Exclude specific directories from the scan:

Use SI units to display sizes (in kilobytes, megabytes, etc.):

ncdu help screen

When you run the ncdu command, it will look through the directory you tell it to and show you details about how much space is being used in a terminal-based interface. From the ncdu interface, you can move through directories, expand or collapse directory levels, see how your disk is being used, and do things like delete files or folders.

Conclusion

  • These are just a few examples of the disk space commands available in Linux

  • By utilizing these commands, you can monitor disk usage, identify space-consuming files or directories, and optimize disk space utilization.

  • Remember to refer to the command's man pages or help documentation for more options and usage details of disk space commands in Linux.

  • With proper disk space management, you can ensure the smooth operation and performance of your Linux system.