Home Beginner's Guide How to check disk space via command line in Linux

How to check disk space via command line in Linux

Managing disk space is crucial for any Linux user. Dive into our guide to learn essential command-line methods to easily view, analyze, and manage your storage allocations.

by John Horan
checking disk space in linux command line

Are you a Linux enthusiast who appreciates the limitless power and flexibility offered by the command line? If yes, then you might have found yourself frequently checking disk space usage. It’s a crucial task that ensures your system runs smoothly. Over the years, I have acquired various commands and techniques to manage my disk space and keep it optimized.

Today, I’m excited to share these valuable insights with you, so you can take better control of your disk space. Whether you want to know how much disk space you have left or which files and folders are hogging the most space, you’ve come to the right place. Let’s get started and explore these useful tips together!

Checking disk space in Linux using command-line

1. Starting simple with df command

df stands for disk free, and it’s a nifty little command to get an overview of your system’s disk space usage.

General syntax:

df [OPTIONS]... [FILE]...

Sample output:

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       50G   25G  25G  50% /
tmpfs            2G     0   2G   0% /dev/shm

The -h flag here is particularly useful. It stands for ‘human-readable’, and it displays sizes in a format (like MB, GB) that’s easy for us humans to understand. Without it, you’d get a long list of numbers in bytes, and trust me, no one wants to count those zeroes!

You can also use the df command to check the disk space of a specific drive. To do this, simply specify the path to the drive as an argument to the df command. For example, to check the disk space of the /home directory, you would use the following command:

df /home

2. Digging deeper with du command

du is short for disk usage. It’s another favorite of mine that helps you get detailed statistics about space consumed by directories.

General syntax:

du [OPTION]... [FILE]...

Sample output:

$ du -sh /home/fosslinux
5.5G	/home/fosslinux

The -s option stands for ‘summarize’, giving you a total for the specified directory, and again, -h is for human-readable. I often find myself navigating to a directory and just typing du -sh * to see how much space each subdirectory consumes.

3. Spotting the big fish with ncdu

Now, while du is incredibly useful, ncdu (NCurses Disk Usage) is a lifesaver when you want a more interactive experience. It provides a menu-based interface to navigate your file system and check space usage. But, you would need to install it first if it’s not already there. I admit, I highly recommend this tool – it’s like a map guiding you to your disk space treasures (or monsters!).

Debian and Ubuntu-based Distributions (including Linux Mint)

  • sudo apt update
  • sudo apt install ncdu

Red Hat-based Distributions (including CentOS and Fedora)

CentOS/RHEL:

  • sudo yum install epel-release
  • sudo yum install ncdu

Fedora:

You can install ncdu directly using dnf:

  • sudo dnf install ncdu

General syntax:

ncdu [OPTION]... [DIR]

Output: After typing ncdu, you’d be presented with an interactive screen showcasing directories and their sizes, letting you dive deep into each to explore further.

4. Getting graphical with baobab

For those days when I’m not feeling too text-oriented, there’s baobab, the disk usage analyzer for the GNOME desktop environment. You would launch it from the command line and get a graphical view of your directories and their sizes.

Command to run:

baobab

While this is not strictly a command-line tool, it’s launched from the terminal and is a handy fallback when you’re in the mood for visuals. I can’t deny, sometimes, those colorful pie charts do make the task more appealing! After entering the command, “files” app will pop up, where you have to select the drive on which you want to do the analysis.

baobab command usage in linux

Baobab command usage in Ubuntu

5. File hunting with find command

The find command isn’t exclusively a disk management tool, but it’s incredibly helpful in identifying large files that are lurking in your system.

General syntax:

find [PATH] -type f -size +[SIZE]

Sample output:

$ find /home/fosslinux -type f -size +100M
/home/fosslinux/Videos/big_video_file.mp4
/home/fosslinux/Backup/large_backup.tar.gz

This command is looking for files larger than 100MB in the /home/user directory. It’s particularly useful when I suspect there are some large files but am unsure where they’re located.

6. Monitoring real-time disk I/O with iostat

While iostat is more about disk performance, it can indirectly help in understanding disk space and activity.

General syntax:

iostat [OPTIONS]

Sample output:

$ iostat
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           1.23    0.00    0.73    0.53    0.00   97.50

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda               0.89        13.67        24.89     683455     1242884

While this tool gives you input/output statistics, it indirectly informs about excessive writing or reading, which might indicate issues related to disk space.

7. Visualizing disk usage with xdiskusage

xdiskusage is a visual tool to show you the layout of files and directories on your system. It’s not a command-line tool in the pure sense but can be started from the terminal and provides a bird’s eye view of your storage. You can install it using the following commands:

Debian/Ubuntu based distros:

  • sudo apt install xdiskusage

Fedora/RHEL-based distros

  • sudo dnf install xdiskusage

Manjaro/Arch Linux-based distros

  • sudo pacman -S xdiskusage

Command to run:

xdiskusage

It displays a window with a visual representation of file and directory sizes. I appreciate xdiskusage for those times when a visual summary feels more intuitive.

8. File system disk space usage with lsblk

The lsblk command displays information about your available block devices, giving an overview of your file systems and their mount points.

General syntax:

lsblk [OPTIONS]

Sample output:

$ lsblk
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda      8:0    0  50G  0 disk 
├─sda1   8:1    0   1G  0 part /boot
└─sda2   8:2    0  49G  0 part /

It’s a straightforward way to identify the partitions on your disk, their sizes, and where they’re mounted. Personally, I find this command great for a quick summary, especially when dealing with multiple disks or partitions.

9. Some tips and tricks

Over the years, I’ve developed some habits and tricks around these tools:

  • Aliases: I often set up aliases in my .bashrc or .zshrc files for commands I use frequently. For example, alias dus="du -sh *" lets me type dus instead of the whole command. It’s a little lazy, but it’s a timesaver!
  • Clearing the Cache: Before running disk checks, I usually clear the cache using sudo apt clean or sudo yum clean all depending on the distribution. It ensures I’m getting accurate, up-to-date results.
  • Regular Clean-ups: At least once a month, I run through these checks to clean up unwanted files and keep my system lean. It’s like a digital detox, and it feels refreshing!

Conclusion

The Linux command line provides a plethora of tools that offer an unparalleled level of insight and control over your system, especially when it comes to managing disk space. With these tools, you can easily monitor disk usage, identify large files and directories, and free up space by removing unnecessary data.

This guide provided you steps to equip you with the necessary knowledge to keep your Linux system running smoothly and efficiently. Regularly checking and decluttering your digital space is just as important as keeping your physical space tidy. So, get ready to explore the wonders of the Linux command line, and happy space hunting!

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.