Date
Dec. 22nd, 2024
 
2024年 11月 22日

Post: Autoremove for Arch Linux

Autoremove for Arch Linux

Published 16:07 Jul 03, 2020.

Created by @ezra. Categorized in #UNIX/Linux, and tagged as #Arch Linux, #UNIX/Linux.

Source format: Asciidoc

Table of Content

在 Debian 体系的 Linux 发行版所使用的包管理器 APT 中有一个 autoremove 指令可以用来自动移除不需要的依赖包,那么,是否在 Arch Linux 中也可以使用类似的指令呢?

答案是肯定的,最简单的方式是执行 sudo pacman -Rcns $(pacman -Qdtq) 指令。

当然,这个指令的缺点也很明显,那就是有时你可能需要执行数次才能清理干净。

为了解决这个问题,我们可以编写这样一个脚本:

autoremove.sh
#!/bin/sh
# This script lists and removes UNneeded DEPendencieS on Arch Linux
# Options: -l <list> | -r <remove> | -ra <remove all - recursive>

LOOPFLAG=0
PACMAN=$(which pacman 2> /dev/null)
SUDO=$(which sudo 2> /dev/null)

case "$1" in
  -l)
  echo -e "
  \r** UNNEEDED DEPENDENCIES **
  \r-> checking dependencies...
  "
  $PACMAN -Qdtq
  if [ "$?" = 1 ]; then
    echo -e "-> Your system doesn't have unneeded dependencies. \n"
  fi
  ;;
  -r)
  while [ "$LOOPFLAG" = 0 ]
  do
    echo -e "
    \r** UNNEEDED DEPENDENCIES **
    \r-> checking dependencies...
    "
    $PACMAN -Qdtq
    if [ "$?" = 0 ]; then
      echo ""
      echo -n "Remove these packages with pacman? [Y/n] "
      read OPTION
      if [ "$OPTION" = "y" ] || [ "$OPTION" = "" ]; then
        echo -n "-> "
        if [ -f $SUDO ]; then
          $SUDO $PACMAN -Rn $($PACMAN -Qdtq)
          if [ "$?" != 0 ]; then
            echo -e "-> Dependencies skipped... next dependencies... \n"
          else
            echo -e "-> Unneeded dependencie(s) sucessfully removed. \n"
          fi
        else
          $PACMAN -Rn $($PACMAN -Qdtq)
          echo -e "-> Unneeded dependencie(s) sucessfully removed. \n"
        fi
      elif [ "$OPTION" = "n" ]; then
        exit 0
      fi
    else
      LOOPFLAG=1
      echo -e "-> Your system doesn't have unneeded dependencies. \n"
    fi
  done
  ;;
  -ra)
  $PACMAN -Qdtq > /dev/null
  if [ "$?" = 1 ]; then
    echo -e "
    \r** UNNEEDED DEPENDENCIES **
    \r-> checking dependencies...
    "
    echo -e "-> Your system doesn't have unneeded dependencies. \n"
  else
    echo -e "\n** UNNEEDED DEPENDENCIES - RECURSIVE **"
    echo -n "-> "
    if [ -f $SUDO ]; then
       $SUDO $PACMAN -Rsn $($PACMAN -Qdtq)
    else
       $PACMAN -Rsn $($PACMAN -Qdtq)
    fi
  fi
  ;;
  *)
    echo "Usage: $0 {-l <list> | -r <remove> | -ra <remove all - recursive>}"
esac
exit 0

接下来,为它赋予执行权限,并置入 /bin 目录中:

$ sudo chmod +x autoremove.sh
$ sudo cp autoremove.sh /bin/autoremove

以后,便可以通过 autoremove -rautoremove -ra 来进行自动移除操作了。

Pinned Message
HOTODOGO
The Founder and CEO of Infeca Technology.
Developer, Designer, Blogger.
Big fan of Apple, Love of colour.
Feel free to contact me.
反曲点科技创始人和首席执行官。
开发、设计与写作皆为所长。
热爱苹果、钟情色彩。
随时恭候 垂询