解释一下这些带有管道和破折号的命令如何工作?

解释一下这些带有管道和破折号的命令如何工作?

这些带有管道和破折号的命令如何(以及为什么)准确地工作?

pacman -Qqdt | sudo pacman -Rns -

答案1

一个单独的破折号(-),没有选项,通常意味着“从标准输入读取”。这是许多程序使用的非常常见的约定。管道|是将一个程序的标准输出连接到另一个程序的标准输入的一种方式。由于pacman默认情况下不从标准输入读取,如果您希望它这样做,您可以使用-.

因此,您显示的命令执行以下操作(请参阅man pacman):

  • pacman -Qqdt:

    -Q, --query
           Query the package database. This operation allows you to view installed
           packages and their files, as well as meta-information about individual
           packages (dependencies, conflicts, install date, build date, size). This can
           be run against the local package database or can be used on individual
           package files. In the first case, if no package names are provided in the
           command line, all installed packages will be queried. Additionally, various
           filters can be applied on the package list. See Query Options below.
    
    -q, --quiet
       Show less information for certain query operations. This is useful when
       pacman’s output is processed in a script. Search will only show package
       names and not version, group, and description information; owns will only
       show package names instead of "file is owned by pkg" messages; group will
       only show package names and omit group names; list will only show files and
       omit package names; check will only show pairs of package names and missing
       files; a bare query will only show package names rather than names and
       versions.
    
    -d, --deps
       Restrict or filter output to packages installed as dependencies. This option
       can be combined with -t for listing real orphans - packages that were
       installed as dependencies but are no longer required by any installed
       package.
    
    -t, --unrequired
       Restrict or filter output to print only packages neither required nor
       optionally required by any currently installed package. Specify this option
       twice to include packages which are optionally, but not directly, required
       by another package.
    

    结合起来,这些选项意味着“查询数据库以查找作为其他包的依赖项安装的包,仅显示包名称,并将输出限制为任何当前安装的包不需要的那些包”换句话说,显示那些由于其他东西需要它们而被安装但是由于其他东西已被删除而不再需要的软件包。

  • sudo pacman -Rns -:

    -R, --remove
       Remove package(s) from the system. Groups can also be specified to be
       removed, in which case every package in that group will be removed. Files
       belonging to the specified package will be deleted, and the database will be
       updated. Most configuration files will be saved with a .pacsave extension
       unless the --nosave option is used. See Remove Options below.
    
    -n, --nosave
       Instructs pacman to ignore file backup designations. Normally, when a file
       is removed from the system, the database is checked to see if the file
       should be renamed with a .pacsave extension.
    
    -s, --recursive
       Remove each target specified including all of their dependencies, provided
       that (A) they are not required by other packages; and (B) they were not
       explicitly installed by the user. This operation is recursive and analogous
       to a backwards --sync operation, and it helps keep a clean system without
       orphans. If you want to omit condition (B), pass this option twice.
    

    还有-(强调我的):

    调用 pacman 涉及指定具有任何潜在选项和操作目标的操作。目标通常是包名称、文件名、URL 或搜索字符串。目标可以作为命令行参数提供。 此外,如果 stdin 不是来自终端,并且将单个连字符 (-) 作为参数传递,则目标将从 stdin 读取。

    因此,pacman -Rns -将从标准输入中读取包名称并删除其中的任何包名称及其依赖项,而不保留备份。

因此,整个命令将找到系统上不再需要的软件包并将其删除。这是清理系统中不需要的软件包的有用方法。

相关内容