DEV Community

Cover image for Automate your Linux apt update and clean trash
Lucas Saladini
Lucas Saladini

Posted on

Automate your Linux apt update and clean trash

The other day I was updating my Linux using APT package manager and I saw how long it take to do the process, not in the way that writing on the terminal was something that is too verbose but in a way that it takes several steps so I can update all the system, including APT and Snap and I thought:

Maybe I cant write a bash script so it runs all the updates in a single run

APT Update

And from that point on, I started working on a basic script that could do all the update process with a single command on terminal.
Knowing that I need to run three APT commands (update, upgrade and dist-upgrade), I started from there, inserting the three together and, also, need to run Snap refresh to update the Snap packages.

Perfect, done that, maybe I should also check the drivers to see if there are any updates available also, so lets add ubuntu-drivers autoinstall also.

I finished the bash script as shown below

#! /bin/bash

set -e
sudo apt update
sudo apt upgrade -y
sudo apt dist-upgrade -y

set -e
sudo snap refresh

set -e
sudo ubuntu-drivers autoinstall
Enter fullscreen mode Exit fullscreen mode

Always using -y to accept the dependencies (if there are any) and continue the process without needing to interact with the terminal.

Clean the System

After updating, I saw that a bunch o junk is kept on the system, consuming disk space and I don't need that kind of junk slowing down my system and using space that I could use for important stuff.

So I started to remove versions of Snap and APT that I don't need, also, remove the APT cache, thumbnails, logs and temp files. I added the Flatpak command so, in the future, if I need something of Flatpak, I have the script already prepared to clean it, but, for now, I keep it commented.

#! /bin/bash
# Removes old versions of snaps
# CLOSE ALL SNAPS BERFORE RUNING THIS

    set -eu
    snap list --all | awk '/disabled/{print $1, $3}' |
        while read snapname revision; do
            snap remove "$snapname" --revision="$revision"
        done

    set -e
    sudo apt autoremove -y

    set -e
    sudo du -sh /var/cache/apt

    set -e
    sudo apt autoclean

    set -e
    sudo journalctl --vacuum-time=3d

    #set -e
    sudo du -sh ~/.cache/thumbnails

    set -e
    sudo du -sh /var/log

    set -e
    sudo apt autoremove --purge

    set -e
    sudo apt clean

    set -e
    sudo rm -rf /tmp/*

    #set -e
    #flatpak uninstall --unused

    set -e
    sudo journalctl -q --disk-usage

    set -e
    sudo journalctl --vacuum-size=1M

    set -e
    sudo rm -rf ~/.local/share/Trash/*

    set -e
    sudo du -sh /var/lib/snapd
Enter fullscreen mode Exit fullscreen mode

✨ Bonus Script for Floorp Browser

I use Floorp browser and I'm facing a problem with the updates, it does not update automatically and I need to download the .tar.bz2 file, extract it and copy its content to the Floorp folder on /lib/floorp.

Taking into consideration that is also a bit boring, I created a script to do all the process.

#!/bin/bash

search_dir="/home/[username]/Downloads"
target_dir="/lib/floorp"

# Check if search directory exists
if [ ! -d "$search_dir" ]; then
    echo "Directory does not exist: $search_dir"
    exit 1
fi

echo "Searching in directory: $search_dir"

# Find the tar.bz2 file
floorp_file=$(find "$search_dir" -type f -name "floorp*.tar.bz2")

if [ -n "$floorp_file" ]; then
    echo "Found file: $floorp_file"
else
    echo "No file found."
    exit 1
fi

# Extract the file
if [ -f "$floorp_file" ]; then
    echo "Extracting $floorp_file..."
    sudo tar -xjf "$floorp_file" -C "$search_dir"
else
    echo "No Floorp .tar.bz2 file found."
    exit 1
fi

# List contents of the directory to diagnose extraction
echo "Contents of $search_dir after extraction:"
ls -l "$search_dir"

# Find the extracted directory
extracted_dir=$(find "$search_dir" -mindepth 1 -maxdepth 1 -type d -name "floorp*" -print -quit)

if [ -d "$extracted_dir" ]; then
    echo "Found extracted directory: $extracted_dir"
    echo "Copying contents from $extracted_dir to $target_dir..."

    # Ensure the target directory exists
    if [ ! -d "$target_dir" ]; then
        echo "Target directory $target_dir does not exist. Creating it..."
        sudo mkdir -p "$target_dir"
    fi

    sudo cp -r "$extracted_dir/"* "$target_dir"

    echo "Contents copied successfully."
else
    echo "No directory found in $search_dir matching 'floorp*'."
    exit 1
fi

sudo rm -rf "$search_dir/floorp"
sudo rm -rf "$floorp_file"
Enter fullscreen mode Exit fullscreen mode

This scripts searches on the Downloads folder for the Floorp .tar.bz2 file, extracts it (if exists), copies the content of the extracted folder to the /lib/floorp folder (if the directory does not exists, it creates it, just one more step to keep it safe and robust), deletes the .tar.bz2 file the the extracted folder of it.

Hope it helps you out to keep your Linux always up to date.

If you have any questions or improvements on the scripts, please, let me know on the comment section.

Top comments (0)