Skip to content

Instantly share code, notes, and snippets.

@MichaelSasser
Last active March 28, 2022 14:24
Show Gist options
  • Save MichaelSasser/848a9ba60fbdd99a94eda4e60cec3a02 to your computer and use it in GitHub Desktop.
Save MichaelSasser/848a9ba60fbdd99a94eda4e60cec3a02 to your computer and use it in GitHub Desktop.
Find and optionally delete pacnew and pacsave files on Arch Linux
#!/usr/bin/env bash
# find-pacnew - find and optionally delete pacnew and pacsave files
# Copyright (c) 2020 Michael Sasser <Michael@MichaelSasser.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
function show_help() {
# Show the help function without exiting.
echo -e "usage: find-pacnew [-h] [-d]\n"
echo "Recursively find and optionally delete pacnew/pacsave files in /etc."
echo -e "Make sure to check and diff all files before deleting!\n"
echo "optional arguments:"
echo -e " -h, --help\t\tShow this help"
echo -e " -d, --delete\t\tDelete the found files"
}
delete=0 # set to 1, if [-d|--del|-delete]
ARGS=$(getopt -o 'h::d::' --long 'help::,delete::,del' -- "$@") || exit
eval "set -- $ARGS"
while true; do
case $1 in
-h|--help)
show_help; exit 0;;
-d|--del|--delete)
delete=1; shift; shift;;
--)
shift; break;;
*)
show_help; exit 1;;
esac
done
if [ "${#@}" -gt 0 ]; then # If leftovers show help and exit 1
show_help
exit 1
fi
# find pacnew/pacsave
files="$(find /etc -regextype posix-extended -regex ".+\.pac(new|save)" 2> /dev/null)"
if [ "$delete" -eq 1 ]; then # delete
while IFS= read -r file; do
if [ "${#file}" -gt 0 ]; then
rm "$file"
echo "$file ... deleted"
else
echo "Nothing to do."
fi
done <<< "$files"
else # just show files
echo "$files"
fi
# vim: set ft=sh :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment