90 Snippets: What could be more fun than freeing storage? 🥸

90 Snippets: What could be more fun than freeing storage? 🥸

90 Projects towards mastery - Day 09 💀

Howdy, fellow coding enthusiasts! Astro here, excited to take you on a detailed journey through a remarkable shell script called "cleaner." In this article, we'll delve deep into the inner workings of the script, dissecting its code step by step. Join me as we explore how "cleaner" offers a comprehensive solution for displaying storage information, identifying large files, and even cleaning up unnecessary clutter. So grab your coding beverage of choice, and let's dive in!

Understanding the Script's Purpose

The "cleaner" script is a versatile tool designed to aid in storage management tasks. Its main objectives include displaying storage information (total, used, available), identifying large files, and suggesting the removal of redundant files to free up valuable disk space. But the magic lies in the code that powers these functionalities.

#!/bin/bash

# Colors
BOLD_RED="\033[1;31m"
BOLD_CYAN="\033[1;36m"
# ... (other color definitions)
RESET="\033[0m"

The script begins by setting the stage with color definitions. Each color code will be used to enhance the output's readability and user experience, turning plain text into a visually engaging interface.

Your Guide to Usage: The "man" Function

function man(){
    echo -e "${BOLD_RED}NAME${RESET}"
    # ... (other manual content)
}

The "man" function isn't just an introduction; it's your guidebook to utilizing "cleaner" effectively. Detailing the script's purpose and options, it provides a clear path for users to explore its capabilities.

Displaying Storage Information

function display_total(){
    echo -e "${BOLD_MAGENTA}Total storage:${RESET}"
    df -h --total | grep -vE "tmpfs|udev|loop|Filesystem|none" | awk '{ print $1 }' > ./titles
    df -h --total | grep -vE "tmpfs|udev|loop|Filesystem|none" | awk '{ print $2 }' > ./values
    c_display values titles
}

The script deepens its functionality with display_total. This function showcases the power of combining commands, using df to fetch storage information and awk to extract the relevant details for presentation.

Uncovering Hidden Giants: The "display_large" Function

function display_large(){
    echo -e "${BOLD_MAGENTA}Large files:${RESET}"
    find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null | awk '{ print $9 }' > ./titles
    find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null | awk '{ print $5 }' > ./values
    c_display values titles
}

Ever wanted to find those space-hogging files? The "display_large" function steps in with finesse. By employing the find command and cleverly parsing the output, it compiles a list of large files to help you decide which ones to trim.

Decluttering Your System: The "clean" Function

function clean(){
    # ... (other cleaning operations)
}

The "clean" function, a standout feature of the script, targets the clutter we often ignore. It covers bases like cache, trash, and old files, giving users the power to decide what stays and what goes, enhancing system efficiency.

The Core of Execution: The "main" Function

function main(){
    if [ $# -eq 0 ]; then
        echo -e "${BOLD_RED}No arguments provided${RESET}"
        echo -e "${BOLD_RED}Try 'cleaner --help' for more information${RESET}"
        exit 1
    fi

    while [ $# -gt 0 ]; do
        case $1 in
            -h|--help)
                man
                ;;
            -t|--total)
                display_total
                ;;
            -u|--used)
                display_used
                ;;
            -a|--available)
                display_available
                ;;
            -l|--large)
                display_large
                ;;
            -c|--clean)
                clean
                ;;
            *)
                echo -e "${BOLD_RED}Unknown option: $1${RESET}"
                ;;
        esac
        shift
    done
}

At the heart of the script lies the "main" function, where all the magic happens. It processes command-line arguments, orchestrates the script's operations, and ensures users experience a smooth and streamlined interaction.

Conclusion: Navigating Storage Management with Confidence

As we conclude our exploration of the "cleaner" script, it's evident that this script isn't just about code—it's about convenience, empowerment, and smart storage management. With its rich features, user-friendly interface, and efficient execution, "cleaner" is a remarkable addition to any tech enthusiast's toolkit.

So there you have it, fellow coders—a deep dive into the "cleaner" script. As you incorporate its functionalities into your workflows, may you find yourself embracing tidier, more efficient systems and enjoying the benefits of a clutter-free digital environment.

Happy coding!

Â