Date
May. 19th, 2024
 
2024年 4月 12日

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
I'm looking for a SOFTWARE PROJECT DIRECTOR / SOFTWARE R&D DIRECTOR position in a fresh and dynamic company. I would like to gain the right experience and extend my skills while working in great teams and big projects.
Feel free to contact me.
For more information, please view online résumé or download PDF
本人正在寻求任职 软件项目经理 / 软件技术经理 岗位的机会, 希望加⼊某个新鲜⽽充满活⼒的公司。
如有意向请随时 与我联系
更多信息请 查阅在线简历下载 PDF