<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Lucas Saladini</title>
    <description>The latest articles on Forem by Lucas Saladini (@lucassaladini243).</description>
    <link>https://forem.com/lucassaladini243</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1942067%2Fdbecde94-7b23-48a6-910f-79c9592b405c.jpg</url>
      <title>Forem: Lucas Saladini</title>
      <link>https://forem.com/lucassaladini243</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lucassaladini243"/>
    <language>en</language>
    <item>
      <title>Automate your Linux apt update and clean trash</title>
      <dc:creator>Lucas Saladini</dc:creator>
      <pubDate>Sat, 17 Aug 2024 14:49:10 +0000</pubDate>
      <link>https://forem.com/lucassaladini243/automate-your-linux-apt-update-and-clean-trash-46a9</link>
      <guid>https://forem.com/lucassaladini243/automate-your-linux-apt-update-and-clean-trash-46a9</guid>
      <description>&lt;p&gt;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:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Maybe I cant write a bash script so it runs all the updates in a single run&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  APT Update
&lt;/h2&gt;

&lt;p&gt;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.&lt;br&gt;
Knowing that I need to run three APT commands (&lt;strong&gt;update&lt;/strong&gt;, &lt;strong&gt;upgrade&lt;/strong&gt; and &lt;strong&gt;dist-upgrade&lt;/strong&gt;), I started from there, inserting the three together and, also, need to run Snap &lt;strong&gt;refresh&lt;/strong&gt; to update the Snap packages.&lt;/p&gt;

&lt;p&gt;Perfect, done that, maybe I should also check the drivers to see if there are any updates available also, so lets add &lt;strong&gt;ubuntu-drivers autoinstall&lt;/strong&gt; also.&lt;/p&gt;

&lt;p&gt;I finished the bash script as shown below&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#! /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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Always using &lt;strong&gt;-y&lt;/strong&gt; to accept the dependencies (if there are any) and continue the process without needing to interact with the terminal.&lt;/p&gt;

&lt;h2&gt;
  
  
  Clean the System
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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 &lt;strong&gt;Flatpak&lt;/strong&gt; 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.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#! /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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  ✨ Bonus Script for Floorp Browser
&lt;/h2&gt;

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

&lt;p&gt;Taking into consideration that is also a bit boring, I created a script to do all the process.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/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"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;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 &lt;strong&gt;/lib/floorp&lt;/strong&gt; folder (if the directory does not exists, it creates it, just one more step to keep it safe and robust), deletes the &lt;strong&gt;.tar.bz2&lt;/strong&gt; file the the &lt;strong&gt;extracted folder&lt;/strong&gt; of it.&lt;/p&gt;

&lt;p&gt;Hope it helps you out to keep your Linux always up to date.&lt;/p&gt;

&lt;p&gt;If you have any questions or improvements on the scripts, please, let me know on the comment section.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>bash</category>
      <category>apt</category>
      <category>floorp</category>
    </item>
  </channel>
</rss>
