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 -r
或 autoremove -ra
来进行自动移除操作了。