pacman Tips

From ArchWiki

(Redirected from Colored Pacman output)
Jump to: navigation, search
Summary
This is a collection of common tips for new pacman users.
Languages
English
Related
pacman
Mirrors
powerpill

Contents

General

Basic pacman modifications and improvements

Color output

The most effective method of colorizing pacman is installing pacman-color. To install with yaourt:

$ yaourt -S pacman-color

Besides that, there are numerous scripts and hacks devised by members of the Arch Linux community:

Simple Bash script

Colorized pacman search output:

alias pacs="pacsearch"
pacsearch () {
       echo -e "$(pacman -Ss $@ | sed \
       -e 's#core/.*#\\033[1;31m&\\033[0;37m#g' \
       -e 's#extra/.*#\\033[0;32m&\\033[0;37m#g' \
       -e 's#community/.*#\\033[1;35m&\\033[0;37m#g' \
       -e 's#^.*/.* [0-9].*#\\033[0;36m&\\033[0;37m#g' )"
}

For a system-wide script, do:

# touch /usr/bin/pacs && chmod 755 /usr/bin/pacs

Then paste this into /usr/bin/pacs:

#!/bin/bash
echo -e "$(pacman -Ss $@ | sed \
     -e 's#core/.*#\\033[1;31m&\\033[0;37m#g' \
     -e 's#extra/.*#\\033[0;32m&\\033[0;37m#g' \
     -e 's#community/.*#\\033[1;35m&\\033[0;37m#g' \
     -e 's#^.*/.* [0-9].*#\\033[0;36m&\\033[0;37m#g' )"

To use it simply type pacs instead of pacman -S.

Python script

This one also searches the AUR.

Using acoc

There is another, more general possibility of colorizing arbitrary command output. Download the small Ruby tool acoc, and its requirements, term-ansicolor and tpty. Some applications like ls will not run without tpty because they need to be started from a terminal (or pseudo terminal, in this case), or else they behave differently.

Installation is relatively straightforward:

$ tar xvzf tpty-0.0.1.tar.gz
$ cd tpty-0.0.1
$ ruby extconf.rb
$ make
$ ruby ./test.rb
# make install
$ tar xvzf term-ansicolor-1.0.1.tar.gz
$ cd term-ansicolor-1.0.1
# ruby install.rb

And now acoc itself:

$ tar xvzf acoc-0.7.1.tar.gz
$ cd acoc-0.7.1
# make install

Now, just read the section Advanced Installation in acoc's INSTALL file, and configure acoc as prefered. Create a link for pacman as well, since that is primarily what this is being done for.

Once acoc runs, optionally add these lines to acoc.conf:

[pacman -Si]
/^Name\s+:\s([\w.-]+)/                                  bold
[pacman -Qi]
/^Name\s+:\s([\w.-]+)/                                  bold
[pacman -Qi$]
/^([\w.-]+)\s([\w.-]+)/                                 bold,clear
[pacman -Ss]
/^([\w.-]+)\/([\w.-]+)\s+([\w.-]+)/                     clear,bold,clear
[pacman -Qs]
/^([\w.-]+)\/([\w.-]+)\s+([\w.-]+)/                     clear,bold,clear
[pacman -Sl]
/^([\w.-]+)\s([\w.-]+)\s([\w.-]+)/                      clear,bold,clear
[pacman -Qo]
/^([\w.-\/]+)\sis\sowned\sby\s([\w.-]+)\s([\w.-]+)/     clear,bold,clear
[pacman -Qe$]
/^([\w.-]+)\s([\w.-]+)/                                 bold,clear
[pacman -Qg$]
/^([\w.-]+)\s([\w.-]+)/                                 clear,bold

The above lines make pacman print all package names in bold, which is particularly helpful when doing, e.g. pacman -Ss xfce. If desiring a more colorful output, modify the lines to suit. Read the acoc documentation contained in the source package for more information.

Aliases

The following instructions allow users to run some of the more common pacman commands without the need to type them fully.

Configure the shell

Add the following examples, which work in both Bash and Zsh:

# Pacman alias examples
alias pacupg='sudo pacman -Syu'        # Synchronize with repositories before upgrading packages that are out of date on the local system.
alias pacin='sudo pacman -S'           # Install specific package(s) from the repositories
alias pacins='sudo pacman -Up'         # Install specific package not from the repositories as a file 
alias pacre='sudo pacman -R'           # Remove the specified package(s), retaining its configuration(s) and required dependencies
alias pacrem='sudo pacman -Rns'        # Remove the specified package(s), its configuration(s) and unneeded dependencies
alias pacrep='pacman -Si'              # Display information about a given package in the repositories
alias pacreps='pacman -Ss'             # Search for package(s) in the repositories
alias pacloc='pacman -Qi'              # Display information about a given package in the local database
alias paclocs='pacman -Qs'             # Search for package(s) in the local database
# Additional pacman alias examples
alias pacupd='sudo pacman -Sy && sudo abs'     # Update and refresh the local package and ABS databases against repositories
alias pacinsd='sudo pacman -S --asdeps'        # Install given package(s) as dependencies of another package
alias pacmir='sudo pacman -Syy'                # Force refresh of all package lists after updating /etc/pacman.d/mirrorlist

Usage

Perform the respective commands by simply typing the alias name. For example, to synchronize with repositories before upgrading packages that are out of date on the local system:

$ pacupg

Install packages from repositories:

$ pacin <package1> <package2> <package3>

Install a custom built package:

$ pacins /path/to/<package>

Completely remove a locally installed package:

$ pacrem <package>

Search for available packages in the repositories:

$ pacreps <keywords>

Display information about a package (e.g. size, dependencies) in the repositories:

$ pacrep <keywords>

Notes

The aliases used above are merely examples. By following the syntax samples above, rename the aliases as convenient. For example:

alias pacrem='sudo pacman -Rns'
alias pacout='sudo pacman -Rns'

In the case above, the commands pacrem and pacout both call Bash to execute the same command.

Operations and Bash syntax

In addition to pacman's standard set of features, there are ways to extend it's usability through rudimentary Bash commands/syntax.

  • To install a number of packages sharing similar patterns in their names -- not the entire group nor all matching packages; eg. kdemod:
 pacman -S kdemod-{applets,theme,tools}
  • Of course, that is not limited and can be expanded to however many levels needed:
 pacman -S kdemod-{ui-{kde,kdemod},kdeartwork}
  • pacman has the -q operand to hide the version column, so it is possible to query and reinstall packages with "compiz" as part of their name:
 pacman -S `pacman -Qq | grep compiz`
  • The above can be achieved without -q by issuing an awk operation:
 pacman -S `pacman -Q | awk '/compiz/ { print $1 }'`

Installation and recovery

Alternative ways of getting and restoring packages

Installing packages from a CD/DVD/iso

  • First mount the CD (replace cdrom with dvd if needed):
# mount /mnt/cdrom
If working with an .iso file instead, first create a directory under /mnt:
# mkdir /mnt/iso
Then mount the image:
# mount -t iso9660 -o ro,loop /path/to/iso /mnt/iso
  • Configure pacman:
# nano -w /etc/pacman.conf
  • Add the following before other repositories (e.g. extra, core, etc.). This ensures the files from the CD/DVD/iso take precedence over those in the standard repositories:
# Settings for using a cd-rom as a repository.
[custom]
Server = file:///mnt/cdrom/arch/pkg
Again, replace cdrom as appropiate.

Once pacman.conf has been edited, sync pacman in order to be able to use the new repository.

Custom local repository

pacman 3 introduced a new script named repo-add which makes generating a personal own repository much easier. Use repo-add --help for more details on its usage.

This script is very easy to run, and also very easy to keep the database up to date. Simply store all of the built packages to be included in the repository in one directory, and execute the following command (where repo is the name of the custom repository):

repo-add /path/to/repo.db.tar.gz /path/to/*.pkg.tar.gz

The last argument will add all pkg.tar.gz files to the repository, so be careful. If having multiple versions of a package in the directory, it is unclear which one will take precedence and end up in the repository.

Note that when using repo-add, the database and the packages do not need to be in the same directory. But when using pacman with that database, they should be together.

To add a new package (and remove the old if it exists), simply run

repo-add /path/to/repo.db.tar.gz /path/to/packagetoadd-1.0-1-i686.pkg.tar.gz
Note: If there is a package that needs to be removed from the repository, read up on repo-remove.

Once the local repository has been made, add the repository to pacman.conf. The name of the db.tar.gz file is the repository name. Reference it directly using a file:// url, or access it via FTP using ftp://localhost/path/to/directory.

If willing, add the custom repository to the list of unofficial user repositories, so that the community can benefit from it.

Network shared pacman cache

Note: As of version 3.1, pacman -Sc changed behavior. By default it does no longer remove all outdated package files but all package files which are not installed on the machine the command was issued on. Because pacman cannot predict what packages are installed on all machines that share the cache, pacman will end up deleting files that shouldn't be. To clean up the cache so that only the newest packages are kept, put CleanMethod = KeepCurrent in the [Options] section of /etc/pacman.conf.

In order to share packages between multiple computers, simply share /var/cache/pacman/ using any network-based mount protocol. This section shows how to use shfs or sshfs to share a package cache plus the related library-directories between multiple computers on the same local network. Keep in mind that a network shared cache can be slow depending on the file-system choice, among other factors.

First, install any network-supporting filesystem; for example sshfs, shfs, ftpfs, smbfs or nfs

Tip: To use sshfs or shfs, consider reading Using SSH Keys.

Then, to share the actual packages, mount /var/cache/pacman/pkg from the server to /var/cache/pacman/pkg on every client machine.

To have shared package databases, mount /var/lib/pacman/{core,extra,testing,community} in the same way. Proceed to place the appropriate lines in /etc/fstab.

Backing up and retrieving a list of installed packages

It is good practice to keep periodic backups of all pacman-installed packages. In the event of a system crash which is unrecoverable by other means, pacman can then easily reinstall the very same packages onto a new installation.

  • First, backup the current list of packages (which are available in a repository):
 $ comm -13 <(pacman -Qmq | sort) <(pacman -Qqe | sort) > pkglist
  • Store the pkglist on a USB key or other convenient medium.
  • Copy the pkglist file to the new installation, and navigate to the directory containing it.
  • Issue the following command to install from the backup list:
 # pacman -S $(cat pkglist)

Redownloading all installed packages (minus AUR)

Listing currently installed packages will output everything, including packages that are foreign and/or might not be available for installation any longer. So simply running:

# pacman -S `pacman -Qq`

will output errors because some (or many) of the packages were not found in the repositories. There needs to be a way to list only packages that can be installed from the repositories present in pacman's database. In order to do so, combining a command to list all packages, and another to hide the list of foreign packages is required.

This can be done by using grep to filter out foreign packages:

# pacman -S $(pacman -Qq | grep -v "$(pacman -Qmq)")

A less contrieved way of achieving the above is achieved by using comm:

# pacman -S $(comm -3 <(pacman -Qq) <(pacman -Qqm))
Tip: When powerpill installed, replace pacman with powerpill in the commands above to increase download speed and alleviate the bandwidth load per server.

Restore pacman's local database

Signs that pacman needs a local database restoration:

  • pacman -Q gives absolutely no results, and pacman -Syu reports that the system is up to date, in spite knowing that it is not.
  • When trying to install a package using pacman -S package it outputs list of already satisfied dependencies.

Most likely, pacman's database of installed software, /var/lib/pacman/local, has been corrupted or deleted. While this is a serious problem, it can be restored by following the instructions below.

Instructions

  • Firstly, make sure pacman's log file is present:
$ ls /var/log/pacman.log

If it does not exist, it is not possible to continue with this method. The only option is to re-install the system from scratch.

  • Create the pkglist.sh file with the following content [1]:
awk '
  $3 == "installed" || $3 == "upgraded" { pkg[$4] = 1 } 
  $3 == "removed" { pkg[$4] = 0 } 
  END { for (i in pkg) if ( pkg[i] == 1 ) print i; }
' /var/log/pacman.log

Make the script executable:

$ chmod +x pkglist.sh
  • Now, run pkglist.sh and pipe the output to pkglist-orig.
$ ./pkglist.sh > pkglist-orig
  • pkglist now contains a list of all the software installed or upgraded. Edit pkglist and remove anything that shouldn't be re-installed (optional). This might be the situation with custom packages made with ABS, for example. Here is a way to automatically restrict the list to packages available in a repository:
$ (cat pkglist-orig ; pacman -Slq | sort | uniq) | sort | uniq -d > pkglist

Check if some important base package are missing, and add them to the list:

$ pacman -Sgq base | grep -v "$(cat pkglist)"
  • Once the contents of pkglist are satisfactory, use it to re-install the programs, and restore /var/lib/pacman/local.

There is no need to check for dependencies, and the install needs to be "forced" because the programs already exist:

# pacman -Sdf --needed $(cat pkglist)

pacman will now present a long list of software to be installed. Say Yes and wait for pacman to finish.

  • Finally, find all the configuration files that have changed. Do this by first updating the locate database:
# updatedb
  • Then search for all the configuration files that have changed:
# locate pacorig

This will give a list of all the configuration files that have been replaced. The original file will have .pacorig appended to it. Delete the new files, and rename the .pacorig files to restore the original configuration for each software package that may be affected. Some directory permissions may also have been changed. Check this if something refuses to start.

Maintenance

House keeping, in the interest of keeping a clean system and following The Arch Way

Miscellaneous procedures

For recursively removing orphans (be careful):

# pacman -Rs $(pacman -Qtdq)

To get a sorted list of local packages and their size:

$ LANG=C pacman -Qi | sed -n '/^Name[^:]*: \(.*\)/{s//\1 /;x};/^Installed[^:]*: \(.*\)/{s//\1/;H;x;s/\n//;p}' | sort -nk2

Getting a list of files not owned by any package

Periodic checks for files outside of pacman database are recommended. These files are often some 3rd party applications installed using the usual procedure (e.g. ./configure; make; make install). Search the file-system for these files (or symlinks) using this simple script:

#!/bin/bash
time /usr/bin/pacman -Ql | cut -d ' ' -f 2- | sort -u > db.txt
time find /arch /bin /boot /etc /lib /opt /sbin /usr 2>/dev/null | while read i; do
    if [ ! -d "$i" ]; then
        echo "$i"
    fi;
done | sort -u > fs.txt
time comm -23 fs.txt db.txt > non-db.txt
 
#test it:
#cat non-db.txt | while read i; do pacman -Qo "$i" 2>&1; done | tee test.txt

Note that one should not delete all files listed in non-db.txt without confirming each entry. There could be various configuration files, logs, etc., so use this list responsibly and only proceed after extensively searching for cross-references using grep.

Selective cache purge

Here is a Python script to clean the /var/cache/pacman/pkg directory while allowing to specify how many package versions should be retained. [2]

Personal tools