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.
This tutorial will teach you how to create a pacman hook to never miss any .pacnew files after an update.
Just create two files:
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
Ensure /etc/pacman.d/scripts/check-pacnew is owned by root and is executable!
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!
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.
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.
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.
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.
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.
Bash magic. Sorry for poor english in code
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
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.
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.