I'd like to keep track of all the programs I'm installing, so every time a command contains pacman -S (important space after -S) I'd like bash to append the command string to a file. Currently I'm using a function but it's annoying because I have to go to history and brace the line with the function and quotes. I could grep history but the general purpose solution would be handy.

Is there a way to do that automatically in a single step install&log ?

  • 1
    Doesn't /var/log/pacman.log contain what you want? Commented Jul 23, 2016 at 23:51
  • @StephenHarris no I want to append the command string into a file so I can run it if I reinstall linux, also I'll use this method not only for pacman, but also for aliases (automatic update of bashrc) etc Commented Jul 24, 2016 at 0:02
  • What do you means that you have a function, but you have to "go to the history and brace the line"? What does your function actually do?
    – user14755
    Commented Jul 24, 2016 at 1:10
  • @DarkHeart aa () { echo $1 >> /home/logfile; } so when I have a command I want to log I just go aa "command". Still annoying, I can define a keyboard macro to brace it quickly but sometimes when the command contains quotes it can conflict with the bracing quotes. Commented Jul 24, 2016 at 1:13
  • 1
    Yet better, Pacman tracks which packages were installed explicitly. Although I don't remember the exact command, possibly pacman --query --explicit may be used to retrieve the list.
    – Andrew
    Commented Jul 24, 2016 at 3:05

3 Answers 3


I think what you are trying to accomplish is very hard if not impossible, even when you can create a wrapper function to do what you want and an alias to call the wrapper instead of the actual command it all goes wrong because you usually run sudo pacman -S and sudo breaks all functions and aliases by switching contexts.

But not all is lost, you can list all packages explicitly installed with pacman -Qe, so if at any time you wish to reinstall the system or copy the environment to another machine you can get the list to a file, transfer the file to the target machine and run pacman to install all packages.

To make the file:

pacman -Qe | cut -d ' ' -f 1 | tr "\n" " " >the-file.txt
  • pacman -Qe: Lists all explicitly installed packages.
  • cut -d ' ' -f 1: is to remove the package versions and keep the names only.
  • tr "\n" " ": is to change newlines to spaces.
  • >the-file.txt: Is to redirect the output to a file, feel free to change the file name and path to your needs.

This will output a space separated list to the-file.txt, you then transfer the file to the target machine and run:

sudo pacman -Sy - <the-file.txt

Voila, pacman is installing all packages from the list and you will get the same result.

You can also add the --needed argument to pacman so it wont reinstall up-to-date packages pacman -S --needed ....

And if you are doing the new machine remotely you can skip the file transfer and just pipe the results to ssh and it will carry on to the remote machine:

pacman -Qe | cut -d ' ' -f 1 | tr "\n" " " | ssh -t user@host "sudo pacman -S --needed -"
  • With -t ssh allocates a pseudo terminal, otherwise sudo will fail.

Good Luck.


If you are using bash:

trap `echo $BASH_COMMAND | fgrep "pacman - S" >> /home/logfile

You'll also want it in your .bash_profile or similar so it runs for every interactive shell.

Of course, this only captures the things you typed into bash, not anything that was executed in some other way, such as a front-end running pacman in the background.

Keep in mind that this could really hamper performance (running an fgrep for every other command you run), so while this answers your question, it's not necessarily the best way to meet your objective of tracking packages.

See also: https://superuser.com/q/175799

If you are using zsh instead, look up the preexec function in the zsh manual.


Have a look at sudosh2 for logging of your entire command history with replay. This might do what you want.

  • Although this may solve the issue, it would be nice if you could expand on how to actually use sudosh2 to do that.
    – Kusalananda
    Commented Jul 24, 2016 at 8:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .