[HowTo] Create a pacman hook to detect .pacnew files

Difficulty: ★★☆☆☆

Pacnew and Pacsave files

A .pacnew file may be created during a package upgrade to avoid overwriting a file which already exists. A .pacsave file may be created during a package removal, or by a package installation of a package that was removed. These files require manual intervention from the user and it is good practice to handle them regularly.

source: System Maintenance - Manjaro

This tutorial will teach you how to create a pacman hook to never miss any .pacnew files after an update.

Just create two files:

  1. Create a script that we’ll call later with the pacman hook, we’ll name it check-pacnew and create it in /etc/pacman.d/scripts/ with your favorite editor:

    #!/bin/bash
    #
    # List .pacnew files when found
    
    pacnews=($(/usr/bin/pacdiff --output|grep -v pacsave))
    nb="${#pacnews[@]}"
    if [[ $nb > 0 ]]; then
      echo -e "\e[1;31m$nb .pacnew found in system \e[0m"
      printf "%s\n" "${pacnews[@]}"
    fi
    
  2. Ensure /etc/pacman.d/scripts/check-pacnew is owned by root and is executable!

  3. The actual pacman hook, will be named check-pacnew.hook and should be created in /etc/pacman.d/hooks/:

    [Trigger]
    Operation = Upgrade
    Type = Package
    Target = *
    
    [Action]
    Description = Looking for .pacnew files...
    Exec = /etc/pacman.d/scripts/check-pacnew
    When = PostTransaction
    NeedsTargets
    

That’s it! :+1:

With these two files in place, every time pacman will do an update, it will trigger the hook, which will call the script, and output something like this if it finds a .pacnew file:

[…]
(19/22) Looking for .pacnew files…
1 .pacnew found in system

/etc/security/limits.conf.pacnew

[…]

So you will never miss it when there are .pacnew files, so you can then take care of them immediately.

12 Likes

What about /home/$USER/.local/bin ?

For that, it’ll only work if the user currently logged in has the file in that path. While the path used by @omano is for every and all users. In other words, it’s not user-dependent, while anything in a /home directory is.

1 Like

Thanks for the explanation.

1 Like

I already thought about both cases, and I know that placing manually executable in /usr/bin/ is bad practice, but I also figured that using $USER or similar will not be system wide viable. Anyone has another suggestion? I though about /usr/local/bin/ but I’m not familiar really with these folders and what is best. I’m a piggy and just manually place executables in /usr/bin/ knowing it can create issues if existing programs have same name in Manjaro repos.

1 Like

Well I’m also not sure, then. Perhaps /usr/local/bin is a better idea. Either way, It shouldn’t be in a /home directory if it should be accessible to more than 1 user.

pacman already outputs when it makes a pacnew, e.g.

[2020-12-18T19:35:28-0500] [ALPM] upgraded grub (2.04-12 → 2.04-13)
[2020-12-18T19:35:28-0500] [ALPM] warning: /etc/default/grub installed as /etc/default/grub.pacnew

you just want the information at the bottom instead of in the body of the upgrades?

Yes, but when you have 600 packages updated, it is nice to have the red text warning telling you with a summary the number and the list of .pacnew files detected. This way no need to scroll the terminal (and if you don’t have a proper terminal configuration in this case anyway the output is maybe not all in the terminal if it is too long), you can’t miss the message as it is in the last pacman steps at the very bottom of terminal.

This. Because it scrolls to fast for me to catch…

@omano

Bash magic. Sorry for poor english in code :alien:
Install first:

sudo pacman -Syu mlocate

create a file clean-pacnew with CODE:

#!/usr/bin/env bash

if [ "$(id -u)" = "0" ]; then
   echo "This script must be run as normal user" 1>&2
   exit 1
fi

BD=$(tput bold)
NM=$(tput sgr0)
ls=$(ls -h --group-directories-first --time-style=+"%d.%m.%Y %H:%M" --color=auto -F)
FINDBKP=$(sudo updatedb && locate -e --regex "\.pac(new|orig|save)$")

pacbklist(){
for i in $FINDBKP
do
  ls $i
done
}

pacbkpdel(){
for i in $FINDBKP
do
  sudo rm -f $i
done
}

main(){
if [[ -z "$FINDBKP" ]]; then
    printf "${BD}ERROR: Pacman backup files not found!${NM}\n"
    exit 0
else
    printf "${BD}SUCCESS: Found pacman backup files:${NM}\n"
    pacbklist
    printf "${BD}Type ${NM}Y${BD} to continue and ${NM}DELETE${BD} them all.${NM}\n"
    printf "${BD}Type any other key to exit.${NM}\n"
    printf "${BD}Answer:${NM}"
    read SEL
    case "$SEL" in
        "Y") pacbkpdel;;
        *) exit 0;;
    esac
fi
}   

main

Launch (or add exec bit and put in bin dir)

bash clean-pacnew

Thanks for sharing your script but what are you replying this to, and what should I do with it?

I believe it is another approach to the script. Not the hook, the script.

You can’t have interactive scripts with pacman hooks.

1 Like

Didn’t realise. I’m guessing then it’s for finding and sorting it out manually. Or something. :man_shrugging:

Also it seems this script looks for .pacnew .pacsave and .pacorig files, and backup/delete them.
I have made my own script to handle .pacnew files and view difference with meld, and to optionaly delete them after closing meld.

Manually, it seems.

Well, whatever works for him/her/it. And at least now it’s out there…

using instead of creating NEW hooks with same functionality as pacman self. :alien:

1 Like

Yes I know pacman will output when it will write a pacnew file. But it doesn’t do what my hook does.

So thank you but I don’t need another script, I actually don’t need anything, I’m just sharing for other who don’t want to search in pacman log, and want instant listing of .pacnew files at the very bottom of pacman update.

And thank you @omano! I really, really like the script and what it does, so thank you!

1 Like

So, install pacman-contrib and use pacdiff.

sudo pacman -Syu && pacdiff

So, install pacutils, and use paclog in a shell function.

cat .bash_functions
#
# ~/.bash_functions
#
...

pwarn () { paclog --after="$1" | paclog --warnings ; }

...

Sample:

pwarn 2020-10-01
[2020-10-31T09:51:48-0400] [ALPM] warning: /etc/pacman.d/mirrorlist installed as /etc/pacman.d/mirrorlist.pacnew
[2020-12-02T17:22:35-0500] [ALPM] warning: /etc/systemd/homed.conf installed as /etc/systemd/homed.conf.pacnew
[2020-12-05T11:48:28-0500] [ALPM] warning: /etc/pacman.d/mirrorlist installed as /etc/pacman.d/mirrorlist.pacnew
[2021-01-10T08:24:01-0500] [ALPM] warning: /etc/pacman.d/mirrorlist installed as /etc/pacman.d/mirrorlist.pacnew
[2021-01-19T20:55:54-0500] [ALPM] warning: directory permissions differ on /mnt/
filesystem: 775  package: 755

LOL, we don’t need no schtinking hooks. :slight_smile: :slight_smile:

1 Like