How to check the largest Files and free disk space in Linux

How to check the largest Files and free disk space in Linux

Managing disk space is crucial for maintaining a well-functioning Linux system. Over time, files accumulate, and without regular monitoring, a system can quickly run out of space. This article will guide you through the steps to identify the largest files on your system and check your disk space usage.

Understanding Disk Space in Linux

In Linux, everything is treated as a file, including devices and partitions. The Linux file system hierarchy is structured with a root directory (/) that branches into various subdirectories. As you use your system, files are created, modified, and deleted, leading to changes in disk space usage.

It’s important to regularly check your disk space to prevent your system from running out of space, which can cause performance issues or even system crashes. Linux provides several command-line tools to help you monitor disk usage and identify large files that might be consuming excessive space.

Checking Free Disk Space

The first step in managing disk space is to check how much space is available on your system. The df (disk free) command is the primary tool for this task.

Using the df Command

The df command displays information about the total, used, and available disk space on your system’s file systems. The basic syntax is:

df -h

 

Here, the -h option stands for “human-readable,” which formats the output in a way that’s easier to understand, showing sizes in KB, MB, or GB.

Example Output

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        50G   25G   23G  53% /
tmpfs           7.8G   50M  7.8G   1% /dev/shm

 

In this output, the Size column shows the total size of the partition, Used shows the space that’s currently used, Avail indicates the available space, and Use% represents the percentage of used space.

Checking Disk Usage with du

While df gives an overview of disk usage, the du (disk usage) command provides detailed information about the space used by individual files and directories.

Basic Usage

The basic syntax of the du command is:

du -h /path/to/directory

 

This command will display the disk usage of the specified directory and its subdirectories in a human-readable format.

Checking Disk Usage for Specific Directories

If you want to check the disk usage of your home directory, you can use:

du -sh /home/username

 

The -s option stands for “summary,” and it provides a total size for the specified directory without listing individual files.

Identifying the Largest Files on Your System

After determining your disk space usage, the next step is to identify the largest files. These files are often the culprits when your disk space is running low.

Using the find Command

The find command is a powerful tool for searching files in a directory hierarchy. You can use it to find the largest files by specifying the file size.

Example Command

To find the largest files in the /home directory, you can use:

find /home -type f -exec ls -lh {} + | sort -k 5 -h | tail -n 10

 

Let’s break down this command:

  • find /home -type f: This part searches for files (-type f) in the /home directory.
  • -exec ls -lh {}: This part executes the ls -lh command on each file found, providing a long listing with human-readable file sizes.
  • sort -k 5 -h: This part sorts the output by the file size (the 5th column in the ls -lh output).
  • tail -n 10: Finally, this part displays the last 10 lines of the sorted output, which correspond to the 10 largest files.

Using the du Command for Finding Large Files

Another method to find large files is using the du command in combination with other commands like sort and head.

Example Command

To find the largest files in the /var directory, you can use:

du -ah /var | sort -rh | head -n 10

 

This command provides the 10 largest files and directories under the /var directory.

  • du -ah /var: This part lists all files and directories in /var with their sizes in human-readable format.
  • sort -rh: This part sorts the output by size in reverse order (largest first).
  • head -n 10: Finally, this part shows the top 10 entries from the sorted list.

Combining find and du for Efficient Searches

You can combine the find and du commands to refine your search for large files. For example, to find files larger than 1GB in the /home directory, you can use:

find /home -type f -size +1G -exec du -h {} + | sort -rh | head -n 10

 

This command finds files larger than 1GB, displays their sizes, and lists the top 10 largest ones.

Automating Disk Space Monitoring

Manually checking disk space and identifying large files can be time-consuming. To automate this process, you can use cron jobs and shell scripts.

Creating a Shell Script

You can create a simple shell script to monitor disk usage and send alerts if the usage exceeds a certain threshold.

Example Script

#!/bin/bash
THRESHOLD=90
USAGE=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
if [ $USAGE -gt $THRESHOLD ]; then
    echo "Disk usage is above $THRESHOLD%. Current usage: $USAGE%" | mail -s "Disk Space Alert" user@example.com
fi

 

This script checks the disk usage of the root partition and sends an email alert if it exceeds 90%.

Setting Up a Cron Job

To run this script automatically, you can set up a cron job.

  1. Open the cron job editor:bashCopy codecrontab -e
  2. Add the following line to run the script every hour:bashCopy code0 * * * * /path/to/your/script.sh

This will ensure that your disk usage is monitored regularly without manual intervention.

Conclusion

Managing disk space is essential for maintaining a healthy Linux system. By regularly checking your disk space with the df and du commands, and identifying large files using find and du, you can prevent your system from running out of space. Automating this process with scripts and cron jobs can further streamline your system administration tasks. With these tools and techniques, you can ensure your Linux system runs smoothly and efficiently.

Fedya Serafiev

Fedya Serafiev

Fedya Serafiev owns the website linuxcodelab.eu. He finds satisfaction in helping people solve even the most complex technical problems. His current goal is to write easy-to-follow articles so that such problems do not arise at all.

Thank you for reading the article! If you found the information useful, you can donate using the buttons below: