解釋一下這些帶有管道和破折號的命令如何運作?

解釋一下這些帶有管道和破折號的命令如何運作?

這些帶有管道和破折號的命令如何(以及為什麼)準確地工作?

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 或搜尋字串。目標可以作為命令列參數提供。 此外,如果標準輸入不是來自終端並且單個連字符 (-) 作為參數傳遞,則將從標準輸入讀取目標。

    因此,pacman -Rns -將從標準輸入中讀取套件名稱並刪除其中的任何套件名稱及其依賴項,而不保留備份。

因此,整個命令將找到系統上不再需要的軟體包並將其刪除。這是清理系統中不需要的軟體包的有用方法。

相關內容