https://wiki.archlinux.org/api.php?action=feedcontributions&user=Echo&feedformat=atomArchWiki - User contributions [en]2024-03-28T11:15:15ZUser contributionsMediaWiki 1.41.0https://wiki.archlinux.org/index.php?title=Pacman/Tips_and_tricks&diff=241360Pacman/Tips and tricks2012-12-23T08:03:13Z<p>Echo: /* Listing all packages that nothing else depends on */</p>
<hr />
<div>[[Category:Package management]]<br />
[[es:Pacman Tips]]<br />
[[it:Pacman Tips]]<br />
[[ru:Pacman Tips]]<br />
[[tr:Pacman_ipuçları]]<br />
[[zh-CN:Pacman Tips]]<br />
{{Article summary start|Summary}}<br />
{{Article summary text|This is a collection of common tips for new pacman users.}}<br />
{{Article summary heading|Related}}<br />
{{Article summary wiki|pacman}}<br />
{{Article summary wiki|Mirrors}}<br />
{{Article summary wiki|Creating Packages}}<br />
{{Article summary wiki|Custom local repository}}<br />
{{Article summary end}}<br />
<br />
== Cosmetic and Convienence ==<br />
<br />
=== Color output ===<br />
<br />
The most effective method of colorizing pacman is installing {{AUR|pacman-color}} from the [[AUR]].<br />
<br />
{{Note|The package installs a separate pacman binary patched for colored output ({{ic|pacman-color}}), so you may want to use an [[alias]].}}<br />
<br />
=== Shortcuts ===<br />
<br />
The following instructions allow users to run some of the more common pacman commands without the need to type them fully via a script alias.<br />
<br />
==== Configure the shell ====<br />
<br />
Add the following examples, which work in both [[Bash]] and [[Zsh]]:<br />
{{bc|<nowiki> # Pacman alias examples<br />
alias pacupg='sudo pacman -Syu' # Synchronize with repositories before upgrading packages that are out of date on the local system.<br />
alias pacin='sudo pacman -S' # Install specific package(s) from the repositories<br />
alias pacins='sudo pacman -U' # Install specific package not from the repositories but from a file <br />
alias pacre='sudo pacman -R' # Remove the specified package(s), retaining its configuration(s) and required dependencies<br />
alias pacrem='sudo pacman -Rns' # Remove the specified package(s), its configuration(s) and unneeded dependencies<br />
alias pacrep='pacman -Si' # Display information about a given package in the repositories<br />
alias pacreps='pacman -Ss' # Search for package(s) in the repositories<br />
alias pacloc='pacman -Qi' # Display information about a given package in the local database<br />
alias paclocs='pacman -Qs' # Search for package(s) in the local database<br />
<br />
# Additional pacman alias examples<br />
alias pacupd='sudo pacman -Sy && sudo abs' # Update and refresh the local package and ABS databases against repositories<br />
alias pacinsd='sudo pacman -S --asdeps' # Install given package(s) as dependencies of another package<br />
alias pacmir='sudo pacman -Syy' # Force refresh of all package lists after updating /etc/pacman.d/mirrorlist<br />
</nowiki>}}<br />
<br />
==== Usage ====<br />
<br />
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:<br />
$ pacupg<br />
Install packages from repositories:<br />
$ pacin <package1> <package2> <package3><br />
Install a custom built package:<br />
$ pacins /path/to/<package><br />
Completely remove a locally installed package:<br />
$ pacrem <package><br />
Search for available packages in the repositories:<br />
$ pacreps <keywords><br />
Display information about a package (e.g. size, dependencies) in the repositories:<br />
$ pacrep <keywords><br />
<br />
==== Notes ====<br />
<br />
The aliases used above are merely examples. By following the syntax samples above, rename the aliases as convenient. For example:<br />
<br />
alias pacrem='sudo pacman -Rns'<br />
alias pacout='sudo pacman -Rns'<br />
<br />
In the case above, the commands {{ic|pacrem}} and {{ic|pacout}} both call your shell to execute the same command.<br />
<br />
=== Operations and Bash syntax ===<br />
<br />
In addition to pacman's standard set of features, there are ways to extend its usability through rudimentary [[Bash]] commands/syntax.<br />
<br />
* To install a number of packages sharing similar patterns in their names -- not the entire group nor all matching packages; eg. {{Pkg|kde}}:<br />
<br />
# pacman -S kde-{applets,theme,tools}<br />
<br />
* Of course, that is not limited and can be expanded to however many levels needed:<br />
<br />
# pacman -S kde-{ui-{kde,kdemod},kdeartwork}<br />
<br />
* Sometimes, {{Ic|-s}}'s builtin ERE can cause a lot of unwanted results, so it has to be limited to match the package name only; not the description nor any other field:<br />
<br />
# pacman -Ss '^vim-'<br />
<br />
* pacman has the {{ic|-q}} operand to hide the version column, so it is possible to query and reinstall packages with "compiz" as part of their name:<br />
<br />
# pacman -S $(pacman -Qq | grep compiz)<br />
<br />
== Maintenance ==<br />
<br />
''House keeping, in the interest of keeping a clean system and following [[The Arch Way]]''<br />
<br />
=== Selectively clean cache ===<br />
<br />
* Use {{ic|paccache}} from {{pkg|pacman-contrib}} to clean the {{ic|/var/cache/pacman/pkg}} directory while allowing to specify how many package versions should be retained (default is 3):<br />
<br />
paccache -rv<br />
<br />
To list all the options:<br />
<br />
{{ic|paccache -h}}<br />
<br />
* Because {{ic|paccache}} does not remove the cache of uninstalled packages, you can display them with {{ic|diff}} from {{Pkg|diffutils}} and then optionally delete each package in the {{ic|/var/cache/pacman/pkg}} directory:<br />
<br />
pacman -S diff<br />
diff -y <(pacman -Qq) <(ls /var/cache/pacman/pkg/ | sed 's/^\(.*\)-[^-]*-[^-]*-[^-]*$/\1/' | sort -u)<br />
<br />
If you want to display the output with colors install {{Pkg|colordiff}}:<br />
<br />
pacman -S colordiff<br />
colordiff -y <(pacman -Qq) <(ls /var/cache/pacman/pkg/ | sed 's/^\(.*\)-[^-]*-[^-]*-[^-]*$/\1/' | sort -u)<br />
<br />
=== Listing all installed packages with size ===<br />
<br />
* You may want to get the list of installed packages sorted by size, which may be useful when freeing space on your hard drive.<br />
pacman -Qi | awk '/^Name/ {pkg=$3} /Size/ {print $4$5,pkg}' | sort -n<br />
* Use {{ic|pacsysclean}} from {{Pkg|pacman-contrib}} package.<br />
* Install {{Pkg|expac}} and run {{ic| expac -s "%-30n %m" | sort -rhk 2}}<br />
* Invoke pacgraph with the -c option to produce a list of all installed packages with their respective sizes on the system. Pacgraph is available from [community].<br />
<br />
=== Identify files not owned by any package ===<br />
<br />
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. {{ic|./configure && make && make install}}). Search the file-system for these files (or symlinks) using this simple script:<br />
<br />
{{hc|pacman-disowned|<nowiki><br />
#!/bin/sh<br />
<br />
tmp=${TMPDIR-/tmp}/pacman-disowned-$UID-$$<br />
db=$tmp/db<br />
fs=$tmp/fs<br />
<br />
mkdir "$tmp"<br />
trap 'rm -rf "$tmp"' EXIT<br />
<br />
pacman -Qlq | sort -u > "$db"<br />
<br />
find /bin /etc /lib /sbin /usr \<br />
! -name lost+found \<br />
\( -type d -printf '%p/\n' -o -print \) | sort > "$fs"<br />
<br />
comm -23 "$fs" "$db"</nowiki>}}<br />
<br />
To generate the list:<br />
<br />
$ pacman-disowned > non-db.txt<br />
<br />
Note that one should '''not''' delete all files listed in {{ic|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 {{Ic|grep}}.<br />
<br />
=== Removing orphaned packages ===<br />
<br />
For ''recursively'' removing orphans:<br />
<br />
{{bc|# pacman -Rs $(pacman -Qtdq)}}<br />
<br />
The following '''alias''' is easily inserted into {{ic|~/.bashrc}} and removes orphans if found:<br />
<br />
{{hc|~/.bashrc|<nowiki># '[r]emove [o]rphans' - recursively remove ALL orphaned packages<br />
alias pacro="/usr/bin/pacman -Qtdq &gt; /dev/null &amp;&amp; sudo /usr/bin/pacman -Rs \$(/usr/bin/pacman -Qtdq | sed -e ':a;N;$!ba;s/\n/ /g')"</nowiki>}}<br />
<br />
The following '''function''' is easily inserted into {{ic|~/.bashrc}} and removes orphans if found:<br />
<br />
{{hc|~/.bashrc|<nowiki><br />
orphans() {<br />
if [[ ! -n $(pacman -Qdt) ]]; then<br />
echo "No orphans to remove."<br />
else<br />
sudo pacman -Rs $(pacman -Qdtq)<br />
fi<br />
}</nowiki>}}<br />
<br />
=== Removing everything but base group ===<br />
<br />
If it is ever necessary to remove all packages except the base group, try this one liner:<br />
<br />
# pacman -Rs $(comm -23 <(pacman -Qeq|sort) <((for i in $(pacman -Qqg base); do pactree -ul $i; done)|sort -u|cut -d ' ' -f 1))<br />
<br />
Source: [https://bbs.archlinux.org/viewtopic.php?id=130176 Look at discussion here]<br />
<br />
Notes:<br />
<br />
# {{ic|comm}} requires sorted input otherwise you get e.g. {{ic|comm: file 1 is not in sorted order}}.<br />
# {{ic|pactree}} prints the package name followed by what it provides. For example:<br />
<br />
{{hc|$ pactree -lu logrotate|<br />
logrotate<br />
popt<br />
glibc<br />
linux-api-headers<br />
tzdata<br />
dcron cron<br />
bash<br />
readline<br />
ncurses<br />
gzip}}<br />
<br />
The {{ic|dcron cron}} line seems to cause problems, that is why {{ic|cut -d ' ' -f 1}} is needed - to keep just the package name.<br />
<br />
=== Listing official installed packages only ===<br />
<br />
pacman -Qq |grep -Fv -f <(pacman -Qqm)<br />
<br />
=== Getting the dependencies list of several packages ===<br />
<br />
Dependencies are alphabetically sorted and doubles are removed.<br />
Note that you can use {{ic|pacman -Qi}} to improve response time a little. But<br />
you won't be able to query as many packages. Unfound packages are simply skipped<br />
(hence the {{ic|2>/dev/null}}).<br />
You can get dependencies of AUR packages as well if you use {{ic|yaourt -Si}},<br />
but it will slow down the queries.<br />
<br />
$ pacman -Si $@ 2>/dev/null | awk -F ": " -v filter="^Depends" \ '$0 ~ filter {gsub(/[>=<][^ ]*/,"",$2) ; gsub(/ +/,"\n",$2) ; print $2}' | sort -u<br />
<br />
Alternatively, you can use {{ic|expac}}: {{ic|expac -l '\n' %E -S $@ &#124; sort -u}}.<br />
<br />
=== Getting the size of several packages ===<br />
<br />
You can use (and tweak) this little shell function:<br />
<br />
{{hc|~/.bashrc|<nowiki><br />
pacman-size()<br />
{<br />
CMD="pacman -Si"<br />
SEP=": "<br />
TOTAL_SIZE=0<br />
<br />
RESULT=$(eval "${CMD} $@ 2>/dev/null" | awk -F "$SEP" -v filter="^Size" -v pkg="^Name" \<br />
'$0 ~ pkg {pkgname=$2} $0 ~ filter {gsub(/\..*/,"") ; printf("%6s KiB %s\n", $2, pkgname)}' | sort -u -k3)<br />
<br />
echo "$RESULT"<br />
<br />
## Print total size.<br />
echo "$RESULT" | awk '{TOTAL=$1+TOTAL} END {printf("Total : %d KiB\n",TOTAL)}'<br />
}</nowiki>}}<br />
<br />
As told for the dependencies list, you can use {{ic|pacman -Qi}} instead, but<br />
not [[yaourt]] since AUR's PKGBUILD do not have size information.<br />
<br />
A nice one-liner:<br />
<br />
$ pacman -Si "$@" 2>/dev/null" | awk -F ": " -v filter="Size" -v pkg="Name" \ '$0 ~ pkg {pkgname=$2} $0 ~ filter {gsub(/\..*/,"") ; printf("%6s KiB %s\n", $2, pkgname)}' | sort -u -k3 \ | tee >(awk '{TOTAL=$1+TOTAL} END {printf("Total : %d KiB\n",TOTAL)}')<br />
<br />
You should replace "$@" with packages, or put this line in a shell function.<br />
<br />
=== Listing changed configuration files ===<br />
If you want to backup your system configuration files you could copy all files in {{ic|/etc/}}, but usually you're only interested in the files that you have changed. In this case you want to list those changed configuration files, we can do this with the following script:<br />
<br />
{{hc|changed-files.sh|<nowiki><br />
#!/bin/bash<br />
for package in /var/lib/pacman/local/*; do<br />
sed '/^%BACKUP%$/,/^%/!d' $package/files | tail -n+2 | grep -v '^$' | while read file hash; do<br />
[ "$(md5sum /$file | (read hash file; echo $hash))" != "$hash" ] && echo $(basename $package) /$file<br />
done<br />
done<br />
</nowiki>}}<br />
You need to run the script as root or with sudo.<br />
# pacman -Qii | awk '/^MODIFIED/ {print $2}'<br />
does the same.<br />
<br />
=== Listing all packages that nothing else depends on ===<br />
If you want to generate a list of all installed packages that nothing else depends on, you can use the following script. This is very helpful if you are trying to free hard drive space and have installed a lot of packages that you may not remember. You can browse through the output to find packages which you no longer need.<br />
<br />
{{hc|clean|<nowiki><br />
#!/bin/bash<br />
<br />
# This script is designed to help you clean your computer from unneeded<br />
# packages. The script will find all packages that no other installed package<br />
# depends on. It will output this list of packages excluding any you have<br />
# placed in the ignore list. You may browse through the script's output and<br />
# remove any packages you do not need.<br />
<br />
# Enter groups and packages here which you know you wish to keep. They will<br />
# not be included in the list of unrequired packages later.<br />
ignoregrp="base base-devel"<br />
ignorepkg=""<br />
<br />
# Temporary file locations<br />
tmpdir=/tmp<br />
ignored=$tmpdir/ignored<br />
installed=$tmpdir/installed<br />
<br />
# Generate list of installed packages and packages you wish to keep.<br />
echo $(pacman -Sg $ignoregrp | awk '{print $2}') $ignorepkg | tr ' ' '\n' | sort | uniq > $ignored<br />
pacman -Qq | sort > $installed<br />
<br />
# Do not loop packages you are keeping<br />
loop=$(comm -13 $ignored $installed)<br />
<br />
# Check each remaining package. If package is not required by anything and<br />
# is not on your ignore list, print the package name to the screen.<br />
for line in $loop; do<br />
check=$(pacman -Qi $line | awk '/Required By/ {print $4}')<br />
if [ "$check" == 'None' ]; then echo $line; fi<br />
done<br />
<br />
# Clean up $tmpdir<br />
rm $ignored $installed<br />
</nowiki>}}<br />
<br />
If you install {{pkg|expac}} you can run {{ic|<nowiki>expac "%n %N" -Q $(expac "%n %G" | grep -v ' base') | awk '$2 == "" {print $1}'</nowiki>}} which should give the same results but much faster.<br />
<br />
== Installation and recovery ==<br />
<br />
''Alternative ways of getting and restoring packages.''<br />
<br />
=== Installing packages from a CD/DVD or USB stick ===<br />
<br />
To download packages, or groups of packages:<br />
<br />
# cd ~/Packages<br />
# pacman -Syw base base-devel grub-bios xorg gimp --cachedir .<br />
# repo-add ./custom.db.tar.gz ./*<br />
<br />
Then you can burn the "Packages" folder to a CD/DVD or transfer it to a USB stick, external HDD, etc.<br />
<br />
To install:<br />
<br />
'''1.''' Mount the media:<br />
<br />
# mkdir /mnt/repo<br />
# mount /dev/sr0 /mnt/repo #For a CD/DVD.<br />
# mount /dev/sdxY /mnt/repo #For a USB stick.<br />
<br />
'''2.''' Edit {{ic|pacman.conf}} and add this repository ''before'' the other ones (e.g. extra, core, etc.). This is important. Don't just uncomment the one on the bottom. This way it ensures that the files from the CD/DVD/USB take precedence over those in the standard repositories:<br />
<br />
{{hc|# nano /etc/pacman.conf|2=<br />
[custom]<br />
Server = file:///mnt/repo/Packages}}<br />
<br />
'''3.''' Finally, synchronize the pacman database to be able to use the new repository:<br />
<br />
# pacman -Sy<br />
<br />
=== Custom local repository ===<br />
<br />
pacman 3 introduced a new script named {{ic|repo-add}} which makes generating a database for a personal repository much easier. Use {{ic|repo-add --help}} for more details on its usage.<br />
<br />
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):<br />
<br />
$ repo-add /path/to/repo.db.tar.gz /path/to/*.pkg.tar.xz<br />
<br />
Note that when using {{ic|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.<br />
<br />
To add a new package (and remove the old if it exists), run:<br />
<br />
$ repo-add /path/to/repo.db.tar.gz /path/to/packagetoadd-1.0-1-i686.pkg.tar.xz<br />
<br />
{{Note|If there is a package that needs to be removed from the repository, read up on {{Ic|repo-remove}}.}}<br />
<br />
Once the local repository has been made, add the repository to {{ic|pacman.conf}}. The name of the {{ic|db.tar.gz}} file is the repository name. Reference it directly using a {{ic|file://}} url, or access it via FTP using ftp://localhost/path/to/directory.<br />
<br />
If willing, add the custom repository to the [[Unofficial user repositories|list of unofficial user repositories]], so that the community can benefit from it.<br />
<br />
=== Network shared pacman cache ===<br />
<br />
{{Tip|See [http://xyne.archlinux.ca/projects/pacserve/ pacserve] for an alternative (and probably simpler) solution.}}<br />
<br />
In order to share packages between multiple computers, simply share {{ic|/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.<br />
<br />
First, install any network-supporting filesystem; for example [[sshfs]], [[shfs]], [[ftpfs]], [[smbfs]] or [[nfs]].<br />
<br />
{{Tip|To use sshfs or shfs, consider reading [[Using SSH Keys]].}}<br />
<br />
Then, to share the actual packages, mount {{ic|/var/cache/pacman/pkg}} from the server to {{ic|/var/cache/pacman/pkg}} on every client machine.<br />
<br />
To have shared package databases, mount {{ic|/var/lib/pacman/sync/{core,extra,testing,community} }} in the same way. Proceed to place the appropriate lines in {{ic|/etc/fstab}}.<br />
<br />
==== Preventing unwanted cache purges ====<br />
<br />
By default, {{Ic|pacman -Sc}} removes package tarballs from the cache that correspond to packages that 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, it will end up deleting files that should not be.<br />
<br />
To clean up the cache so that only ''outdated'' tarballs are deleted, add this entry in the {{ic|[options]}} section of {{ic|/etc/pacman.conf}}:<br />
<br />
CleanMethod = KeepCurrent<br />
<br />
=== Backing up and retrieving a list of installed packages ===<br />
<br />
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.<br />
<br />
* First, backup the current list of non-local packages:<br />
<br />
: $ comm -23 <(pacman -Qeq|sort) <(pacman -Qmq|sort) > pkglist.txt<br />
<br />
* Store the {{ic|pkglist.txt}} on a USB key or other convenient medium or gist.github.com or Evernote, Dropbox, etc.<br />
<br />
* Copy the {{ic|pkglist.txt}} file to the new installation, and navigate to the directory containing it.<br />
<br />
* Issue the following command to install from the backup list:<br />
<br />
: # pacman -S $(< pkglist.txt)<br />
<br />
In the case you have a list which was not generated like mentioned above, there may be foreign packages in it (i.e. packages not belonging to any repos you have configured, or packages from the AUR).<br />
<br />
In such a case, you may still want to install all available packages from that list:<br />
<br />
# pacman -S --needed $(comm -12 <(pacman -Slq|sort) <(sort badpkdlist) )<br />
<br />
Explanation:<br />
<br />
* {{ic|pacman -Slq}} lists all available softwares, but the list is sorted by repository first, hence the {{ic|sort}} command.<br />
* Sorted files are required in order to make the {{ic|comm}} command work.<br />
* The {{ic|-12}} parameter display lines common to both entries.<br />
* The {{ic|--needed}} switch is used to skip already installed packages.<br />
<br />
You may also try to install all unavailable packages (those not in the repos) from the AUR using [[yaourt]] (not recommended unless you know exactly what you are doing):<br />
<br />
$ yaourt -S --needed $(comm -13 <(pacman -Slq|sort) <(sort badpkdlist) )<br />
<br />
Finally, you may want to remove all the packages on your system that are not mentioned in the list.<br />
<br />
{{Warning|Use this command wisely, and always check the result prompted by pacman.}}<br />
<br />
# pacman -Rsu $(comm -23 <(pacman -Qq|sort) <(sort pkglist))<br />
<br />
=== List downloaded packages that are not in base or base-devel ===<br />
<br />
The following command will list any installed packages that are not in base/base-devel, and as such were likely installed manually by the user:<br />
<br />
$ comm -23 <(pacman -Qeq|sort) <(pacman -Qgq base base-devel|sort)<br />
<br />
=== Reinstalling all installed packages ===<br />
<br />
If you mess up your system ({{ic|rm -rf}}) you can repair by having pacman reinstall all of your packages. <br />
<br />
If your system does not contain any foreign (AUR) packages you can run:<br />
<br />
# pacman -Qeq | pacman -S -<br />
<br />
Pacman preserves the installation reason by default.<br />
<br />
If you have foreign packages this will error as packages will not be found in the repositories. The following will make a list of all packages and remove the foreign packages seen with {{ic|pacman -Qmq}}. Combining a command to list all packages, and another to hide the list of foreign packages is required.<br />
<br />
The following will reinstall every package found in the repositories:<br />
<br />
# comm -23 <(pacman -Qeq|sort) <(pacman -Qmq|sort) | pacman -S -<br />
<br />
=== Restore pacman's local database ===<br />
<br />
Signs that pacman needs a local database restoration:<br />
<br />
* {{ic|pacman -Q}} gives absolutely no output, and {{Ic|pacman -Syu}} erroneously reports that the system is up to date.<br />
* When trying to install a package using {{ic|pacman -S package}}, and it outputs a list of already satisfied dependencies.<br />
* When {{ic|testdb}} (part of {{Pkg|pacman}}) reports database inconsistency.<br />
<br />
Most likely, pacman's database of installed software, {{ic|/var/lib/pacman/local}}, has been corrupted or deleted. While this is a serious problem, it can be restored by following the instructions below.<br />
<br />
Firstly, make sure pacman's log file is present:<br />
<br />
$ ls /var/log/pacman.log<br />
<br />
If it does not exist, it is ''not'' possible to continue with this method. You may be able to use [https://bbs.archlinux.org/viewtopic.php?pid=670876 Xyne's package detection script] to recreate the database. If not, then the likely solution is to re-install the entire system.<br />
<br />
==== Log filter script ====<br />
<br />
You need to install '''paclog-pkglist''' from {{Pkg|pacman-contrib}} and create following script:<br />
<br />
{{hc|pacrecover|<nowiki><br />
#!/bin/bash -e<br />
<br />
. /etc/makepkg.conf<br />
<br />
PKGCACHE=$((grep -m 1 '^CacheDir' /etc/pacman.conf || echo 'CacheDir = /var/cache/pacman/pkg') | sed 's/CacheDi$<br />
<br />
pkgdirs=("$@" "$PKGDEST" "$PKGCACHE")<br />
<br />
while read -r -a parampart; do<br />
pkgname="${parampart[0]}-${parampart[1]}-*.pkg.tar.xz"<br />
for pkgdir in ${pkgdirs[@]}; do<br />
pkgpath="$pkgdir"/$pkgname<br />
[ -f $pkgpath ] && { echo $pkgpath; break; };<br />
done || echo ${parampart[0]} 1>&2<br />
done<br />
</nowiki>}}<br />
<br />
Make the script executable:<br />
<br />
$ chmod +x pacrecover<br />
<br />
==== Generating the package recovery list ====<br />
<br />
{{Warning|If for some reason your [[pacman]] cache or [[makepkg]] package destination contain packages for other architectures, remove them before continuation.}}<br />
<br />
Run the script (optionally passing additional directories with packages as parameters):<br />
<br />
$ paclog-pkglist /var/log/pacman.log | ./pacrecover >files.list 2>pkglist.orig<br />
<br />
This way two files will be created: {{Ic|files.list}} with package files, still present on machine and {{Ic|pkglist.orig}}, packages from which should be downloaded. Later operation may result in mismatch between files of older versions of package, still present on machine, and files, found in new version. Such mismatches will have to be fixed manually.<br />
<br />
Here is a way to automatically restrict second list to packages available in a repository:<br />
<br />
$ { cat pkglist.orig; pacman -Slq; } | sort | uniq -d > pkglist<br />
<br />
Check if some important ''base'' package are missing, and add them to the list:<br />
<br />
$ comm -23 <(pacman -Sgq base) pkglist.orig >> pkglist<br />
<br />
Proceed once the contents of both lists are satisfactory, since they will be used to restore pacman's installed package database; {{ic|/var/lib/pacman/local/}}.<br />
<br />
==== Performing the recovery ====<br />
<br />
Define bash alias for recovery purposes:<br />
<br />
# recovery-pacman() {<br />
pacman "$@" \<br />
--log /dev/null \<br />
--noscriptlet \<br />
--dbonly \<br />
--force \<br />
--nodeps \<br />
--needed \<br />
#<br />
}<br />
<br />
{{ic|--log /dev/null}} allows to avoid needless pollution of pacman log, {{Ic|--needed}} will save some time by skipping packages, already present in database, {{Ic|--nodeps}} will allow installation of cached packages, even if packages being installed depend on newer versions. Rest of options will allow '''pacman''' to operate without reading/writing filesystem.<br />
<br />
Populate the sync database:<br />
<br />
# pacman -Sy<br />
<br />
Start database generation by installing locally available package files from {{ic|files.list}}:<br />
<br />
# recovery-pacman -U $(< files.list)<br />
<br />
Install the rest from {{ic|pkglist}}:<br />
<br />
# recovery-pacman -S $(< pkglist)<br />
<br />
Update the local database so that packages that are not required by any other package are marked as explicitly installed and the other as dependences. You will need be extra careful in the future when removing packages, but with the original database lost is the best we can do.<br />
<br />
# pacman -D --asdeps $(pacman -Qq)<br />
# pacman -D --asexplicit $(pacman -Qtq)<br />
<br />
Optionally check all installed packages for corruption:<br />
<br />
# pacman -Qk<br />
<br />
Optionally [[#Identify files not owned by any package]].<br />
<br />
Update all packages:<br />
<br />
# pacman -Su<br />
<br />
=== Recovering a USB key from existing install ===<br />
<br />
If you have Arch installed on a USB key and manage to mess it up (e.g. removing it while it is still being written to), then it is possible to re-install all the packages and hopefully get it back up and working again (assuming USB key is mounted in /newarch)<br />
<br />
# pacman -S $(pacman -Qq --dbpath /newarch/var/lib/pacman) --root /newarch --dbpath /newarch/var/lib/pacman<br />
<br />
=== Extracting contents of a .pkg file ===<br />
<br />
The {{ic|.pkg}} files ending in {{ic|.xz}} are simply tar'ed archives that can be decompressed with:<br />
<br />
$ tar xvf package.tar.xz<br />
<br />
If you want to extract a couple of files out of a {{ic|.pkg}} file, this would be a way to do it.<br />
<br />
=== Viewing a single file inside a .pkg file ===<br />
<br />
For example, if you want to see the contents of {{ic|/etc/conf.d/ntpd.conf}} supplied within the {{Pkg|ntp}} package:<br />
<br />
$ tar -xOf /var/cache/pacman/pkg/ntp-4.2.6.p5-6-i686.pkg.tar.xz etc/conf.d/ntpd.conf<br />
Or you can use vim, then browse the archive:<br />
$ vim /var/cache/pacman/pkg/ntp-4.2.6.p5-6-i686.pkg.tar.xz</div>Echo