-2

How (and why) do these commands with a pipe and a dash work exactly?

pacman -Qqdt | sudo pacman -Rns -
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Anonymous
  • 503
  • 1
    A dash - is usually used to designate a flag, that tells the program to set a certain option or do something a certain way (if you do pacman --help it'll probably tell you what all the flags do). Meanwhile, a pipe | makes it so that the output of the left side feeds into the standard input on the right side - so, sudo pacman -Rns will behave like taking the output of pacman -Qqdt as input. – Green Cloak Guy Jun 07 '19 at 19:25
  • @GreenCloakGuy that's good enough to be placed as an Answer, if you would! Thank you! – Jeff Schaller Jun 07 '19 at 22:30
  • @GreenCloakGuy Yes, if you could also say something about what the options do and what the purpose of the pipeline might be. – Kusalananda Jun 07 '19 at 22:33
  • Unfortunately, I don't actually know the pacman command, so I wouldn't be able to answer in enough detail for that – Green Cloak Guy Jun 07 '19 at 23:23
  • For many commands that take file arguments, a plain dash indicates "use stdin instead of a file". cat, paste are two examples. – glenn jackman Jun 08 '19 at 12:57
  • @GreenCloakGuy a lone dash isn't an option. It usually means "read from standard input". – terdon Jun 08 '19 at 13:09

1 Answers1

4

A lone dash (-), with no option, usually means "read from standard input". This is a very common convention used by many programs. The pipe, |, is a way of connecting the standard output of one program to the standard input of another. Since pacman doesn't read from standard input by default, if you want it to do so, you use the -.

So, the commands you show do (see 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.

    Combined, these options mean "query the database for packages installed as dependencies of other packages, showing only package names, and restrict the output to those packages not needed by any currently installed package". In other words, show those packages which were installed because they were needed by something else but which are no longer needed because that something else has been removed.

  • 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.

    And the - (emphasis mine):

    Invoking pacman involves specifying an operation with any potential options and targets to operate on. A target is usually a package name, file name, URL, or a search string. Targets can be provided as command line arguments. Additionally, if stdin is not from a terminal and a single hyphen (-) is passed as an argument, targets will be read from stdin.

    So, pacman -Rns - will read package names from standard input and remove any of those, and their dependencies, without keeping backups.

The entire command will therefore find no longer needed packages on your system and remove them. It's a useful way of cleaning your system of unneeded packages.

terdon
  • 242,166