/ Bash Scripts

Analyze and optimize disk space usage

This documentation will guide you through the process of creating a Bash script to analyze and optimize disk space usage. It’s assumed that you have intermediate knowledge of Linux and Bash scripting.

Overview

The script will perform the following actions:

  1. Analyze disk usage
  2. Identify directories consuming high disk space
  3. Clean up unnecessary files

It will incorporate the ability for the user to provide custom options and settings.

Requirements

  • A Linux-based system
  • Bash shell (typically default on most Linux systems)
  • Administrative access (for disk space optimization)

Creating the Script

We’ll call the script diskopt.sh.

#!/bin/bash

This shebang line allows the script to be executed like a standalone executable.

Analyzing Disk Usage

The du command can be used to analyze disk usage. By using the -h flag, we make the output human-readable, and the -s flag will summarize the output, giving us just the total for each directory:

# Analyzing disk usage

# $1: directory path
# $2: maximum depth for directory size listing
analyze_usage() {
    du -h -d $2 $1
}

Identifying High Disk Usage Directories

We can pipe the output of the du command into sort and awk to list directories consuming disk space above a threshold:

# Identifying high disk usage directories

# $1: directory path
# $2: maximum depth for directory size listing
# $3: threshold size (e.g. 500M, 2G)
identify_high_usage() {
    du -h -d $2 $1 | sort -hr | awk -v threshold=$3 '$1 > threshold'
}

Cleaning up Unnecessary Files

The find command can be used to identify and remove files not accessed within a given number of days:

# Cleaning up unnecessary files

# $1: directory path
# $2: number of days
cleanup_files() {
    find $1 -type f -atime +$2 -exec rm -f {} \;
}

Processing Command Line Options

The getopts function will allow the user to specify command line options:

# Process command line options
while getopts a:i:c: flag
do
    case "${flag}" in
        a) ANALYSIS_DIR=${OPTARG};;
        i) IDENTIFY_DIR=${OPTARG};;
        c) CLEANUP_DIR=${OPTARG};;
    esac
done

In this example, the -a option allows the user to specify the directory for analysis, -i for identifying high disk usage, and -c for cleanup.

The Final Script

Combining all parts, your script should look like this:

#!/bin/bash

# Analyzing disk usage
analyze_usage() {
    du -h -d $2 $1
}

# Identifying high disk usage directories
identify_high_usage() {
    du -h -d $2 $1 | sort -hr | awk -v threshold=$3 '$1 > threshold'
}

# Cleaning up unnecessary files
cleanup_files() {
    find $1 -type f -atime +$2 -exec rm -f {} \;
}

# Process command line options
while getopts a:i:c: flag
do
    case "${flag}" in
        a) ANALYSIS_DIR=${OPTARG};;
        i) IDENTIFY_DIR=${OPTARG};;
        c) CLEANUP_DIR=${OPTARG};;
    esac
done

if [ -n "$

ANALYSIS_DIR" ]; then
    echo "Analyzing disk usage for $ANALYSIS_DIR..."
    analyze_usage $ANALYSIS_DIR 1
fi

if [ -n "$IDENTIFY_DIR" ]; then
    echo "Identifying high disk usage directories in $IDENTIFY_DIR..."
    identify_high_usage $IDENTIFY_DIR 1 100M
fi

if [ -n "$CLEANUP_DIR" ]; then
    echo "Cleaning up unnecessary files in $CLEANUP_DIR..."
    cleanup_files $CLEANUP_DIR 30
fi

Running the Script

Before running the script, ensure that it has executable permissions:

chmod +x diskopt.sh

You can run the script with specific options as follows:

./diskopt.sh -a /home/user -i /var -c /tmp

This will analyze disk usage for /home/user, identify high disk usage directories in /var, and clean up unnecessary files in /tmp.

This documentation covers the basics. Depending on your specific needs, you might need to modify or extend the functionality of this script.

Was this helpful?

Thanks for your feedback!