Skip to content

Instantly share code, notes, and snippets.

@gamesguru
Last active December 29, 2025 14:02
Show Gist options
  • Select an option

  • Save gamesguru/f069d7ac732df26a678c49d47dede0fd to your computer and use it in GitHub Desktop.

Select an option

Save gamesguru/f069d7ac732df26a678c49d47dede0fd to your computer and use it in GitHub Desktop.
Software updating script for Ubuntu/Debian (apt-based systems)
#!/bin/bash
# A script for updating apt-based Linux distros
# ... put it in ~/.local/bin, /usr/local/bin, or anywhere in your $PATH
# exit on error
set -e
# Function Definitions
function exec_red {
printf "\\n\e[1;31m%s\e[0m\\n" "$*"
$*
}
function print_red {
printf "\\n\e[1;31m%s\e[0m\\n" "$*"
}
function print_yel {
printf "\n\e[1;33m%s\e[0m\n" "$*"
}
export -f exec_red
export -f print_red
function run_update_ubu {
printf "\\n\e[1;31m%s\e[0m\\n" "apt update"
apt update
printf "\\n\e[1;31m%s\e[0m\\n" "apt dist-upgrade"
apt dist-upgrade
printf "\\n\e[1;31m%s\e[0m\\n" "apt autoremove"
apt autoremove
printf "\\n\e[1;31m%s\e[0m\\n" "dpkg -l | grep "^rc" | awk '{print \$2}' | xargs -r dpkg --purge"
dpkg -l | grep "^rc" | awk '{print $2}' | xargs -r dpkg --purge
printf "\\n\e[1;31m%s\e[0m\\n" "exit"
}
function run_update_arch {
printf "\\n\e[1;31m%s\e[0m\\n" "pacman -Syu"
pacman -Syu
if [ -n "$(pacman -Qdtq)" ]; then
printf "\\n\e[1;31m%s\e[0m\\n" 'pacman -Rns $(pacman -Qdtq)'
pacman -Rns $(pacman -Qdtq)
else
printf "\\n\e[1;31m%s\e[0m\\n" 'No cleanup needed.'
fi
printf "\\n\e[1;31m%s\e[0m\\n" "exit # from sudo loop"
}
function non_root_updates {
# Other package managers
if [ -n "$(which flatpak)" ]; then
printf "\\n\e[1;31m%s\e[0m\\n" "flatpak update"
flatpak update
fi
# Other package managers
if [ -n "$(which yay)" ]; then
printf "\\n\e[1;31m%s\e[0m\\n" "yay -Syu"
yay -Syu
fi
}
# ~~~ END FUNCTION DEFITIONS BLOCK ~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Detect OS
_OS_ID="$(grep ^ID= /etc/os-release | awk -F= '{print $2}')"
echo "Detected distro: $_OS_ID"
_call_func=""
# Execute root tools as sudo
if [ "$_OS_ID" = "arch" ]; then
echo "Proceeding with pacman updates..."
_call_func=run_update_arch
elif [ "$_OS_ID" = "ubuntu" ]; then
echo "Proceeding with apt updates..."
_call_func=run_update_ubu
fi
if [ "$_call_func" ]; then
print_red sudo su
FUNC=$(declare -f $_call_func)
sudo bash -c "$FUNC; $_call_func"
else
print_red WARNING: Skipping apt/pacman, unknown OS: ID=${_OS_ID}
fi
# Run other software managers: flakpak, yay, etc
if [ "$NON_ROOT_UPDATES" ]; then
non_root_updates
else
print_yel "Skipping non root package managers (to run them, export NON_ROOT_UPDATES=1)."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment