Advertisement
FocusedWolf

Arch: yip and yarp pacman+yay scripts to update like a boss

Feb 26th, 2024 (edited)
1,076
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.25 KB | None | 0 0
  1. File: /usr/local/bin/yarp
  2. USAGE: $ yarp    <-- Update the mirrors list and then update the system with yip script.
  3.  
  4.     #!/bin/bash
  5.  
  6.     # Installation:
  7.     #     $ sudo cp ./yarp /usr/local/bin/
  8.     #     $ sudo chmod +x /usr/local/bin/yarp
  9.  
  10.     # Check if reflector is installed.
  11.     if ! command -v reflector >/dev/null 2>&1
  12.     then
  13.         echo -e "\e[94m::\e[0m Reflector could not be found. Install with: $ sudo pacman -S reflector"
  14.         exit
  15.     fi
  16.  
  17.     # Retrieve the latest mirror list from the Arch Linux Mirror Status page, filter the most up-to-date mirrors, sort them by speed, and overwrite the file /etc/pacman.d/mirrorlist.
  18.     # NOTE: When installing Arch, the ISO's installation environment includes a copy of reflector to assist during install. Its just not installed to the system by default.
  19.     sudo reflector --verbose --country "United States" --latest 10 --protocol https --sort rate --save /etc/pacman.d/mirrorlist
  20.     echo
  21.  
  22.     # Call yip script.
  23.     yip
  24.  
  25. File: /usr/local/bin/yip
  26. USAGE: $ yip    <-- Update installed packages, delete downloaded packages, and purge the orphans, like a boss.
  27.  
  28.     #!/bin/bash
  29.  
  30.     # Installation:
  31.     #     $ sudo cp ./yip /usr/local/bin/
  32.     #     $ sudo chmod +x /usr/local/bin/yip
  33.  
  34.     free_space_check() {
  35.         local directory=$1
  36.         local low_space=$2
  37.  
  38.         # Check if directory exists.
  39.         if [[ ! -d "$directory" ]]; then
  40.             echo -e "\e[31m::\e[0m ERROR: Directory '$directory' does not exist."
  41.             exit 1
  42.         fi
  43.  
  44.         # Get available and total space in megabytes.
  45.         local available_space=$(df --output=avail "$directory" 2>/dev/null | tail -n1 | awk '{print int($1/1024)}')
  46.         local total_space=$(df --output=size "$directory" 2>/dev/null | tail -n1 | awk '{print int($1/1024)}')
  47.  
  48.         # Calculate percentage of free space.
  49.         local free_percent=$((available_space / total_space * 100))
  50.  
  51.         # Check if disk space is below threshold.
  52.         if [[ "$available_space" -lt "$low_space" ]]; then
  53.             while true; do
  54.                 echo -e "\e[31m:: WARNING: Low disk space detected. ${free_percent}% free (${available_space} MB) on drive '${directory}'."
  55.                 echo -e "            Running out of disk space during an update could corrupt your system.\e[0m"
  56.                 echo
  57.                 read -p "Do you want to continue? [Y/N]: " response
  58.                 case "$response" in
  59.                     y|Y) echo; break ;;
  60.                     n|N) exit 1 ;; # Exit script with status 1 to indicate failure.
  61.                     *) echo; echo "Please answer Y or N."; echo ;;
  62.                 esac
  63.             done
  64.         fi
  65.     }
  66.  
  67.     free_space_check /boot/efi 50
  68.     free_space_check /boot     500
  69.     free_space_check /         10240
  70.     free_space_check "$HOME"   10240
  71.  
  72.     # Update all packages including AUR.
  73.     yay -Syyu
  74.     # I actually use this one:
  75.     # yay -Syyu --noconfirm
  76.  
  77.     # Remove all unneeded dependencies.
  78.     # NOTE: Strangely both [$ yes | yay -Scc] and [$ yay -Scc --noconfirm] will prompt you to remove dependencies so [yay -Ycc --noconfirm] needs to be called first to prevent that prompt from displaying.
  79.     #
  80.     # NOTE: The command [yay -Ycc --noconfirm] is implemented in https://github.com/Jguer/yay/blob/next/cmd.go
  81.     #       It calls cleanDependencies(...) defined in https://github.com/Jguer/yay/blob/next/clean.go
  82.     #       Which calls hangingPackages(...) defined in https://github.com/Jguer/yay/blob/next/query.go
  83.     #
  84.     # WARNING: This version will not work [$ yes | yay -Scc --noconfirm].
  85.     #          The prompt will still be displayed to delete dependencies,
  86.     #          Yay AUR build directory $HOME/.cache/yay/ will be purged,
  87.     #          but no cleaning will take place in Pacman cache directory /var/cache/pacman/pkg/ and Pacman database directory /var/lib/pacman/
  88.     #          because the --noconfirm forces the "yes |" to be ignored, resulting in the default Pacman behavior of not cleaning anything to occur.
  89.     echo -e "\e[94m::\e[0m Remove all unneeded dependencies..."
  90.     yay -Ycc --noconfirm
  91.  
  92.     # Clean Pacman cache directory /var/cache/pacman/pkg/, Pacman database directory /var/lib/pacman/, and Yay AUR build directory $HOME/.cache/yay/.
  93.     # If you need to downgrade or install an outdated package then you will need to download it again.
  94.     #
  95.     # NOTE: To just clean the Pacman directories the command is [$ yes | sudo pacman -Scc].
  96.     # WARNING: This version doesn't do anything: [$ sudo pacman -Scc --noconfirm]
  97.     #          SEE: https://bbs.archlinux.org/viewtopic.php?id=236186
  98.     #          SEE: https://unix.stackexchange.com/questions/52277/pacman-option-to-assume-yes-to-every-question
  99.     # WARNING: This command is broken as well [$ yay -Scc --noconfirm]. It will only clean Yay AUR build directory: $HOME/.cache/yay/
  100.     yes | yay -Scc
  101.     echo
  102.  
  103.     # Purge the orphans.
  104.     # NOTE: This code might be redundant because of [yay -Ycc --noconfirm]. I've yet to see it actually find an orphan.
  105.     orphans=$(pacman -Qdtq)
  106.     if [[ -n "$orphans" ]]; then
  107.         echo -e "\e[94m::\e[0m Remove orphaned packages..."
  108.         sudo pacman -Rns $orphans
  109.     else
  110.         echo -e "\e[94m::\e[0m No orphaned packages found..."
  111.     fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement