Skip to content

Instantly share code, notes, and snippets.

@pachanka
Last active May 7, 2023 17:57
Show Gist options
  • Save pachanka/7928b484d70882cd69f3119d5585305a to your computer and use it in GitHub Desktop.
Save pachanka/7928b484d70882cd69f3119d5585305a to your computer and use it in GitHub Desktop.
A simple bash script to grep within a bunch of GPG encrypted files.
#!/bin/bash
#
# Usage search-with-gpg path/to/encrypted/files/*
#
if [ -z "$1" ]; then
echo "Usage: $0 'search string' [path/to/encrypted/files/*]";
exit 1;
else
SEARCH=$1;
fi
read -rsp "Enter passphrase: " PASSPHRASE
echo "";
TMPDIR="/tmp/${0##*/}-$$";
if [ -d "$TMPDIR" ]; then
if [ ! -w "$TMPDIR" ]; then
echo "Error: cannot delete $TMPDIR"
exit 1;
else
rm -rf $TMPDIR
fi
fi
mkdir -v "$TMPDIR";
echo "Searching for $SEARCH ...";
for F in "${@:2}"
do
TMPFILE="${TMPDIR}/${F##*/}"
echo "$PASSPHRASE" | gpg --quiet --passphrase-fd 0 --batch -d --output "${TMPFILE}.txt" "${F}" > /dev/null
done
if [ "$(ls -A $TMPDIR)" ]; then
grep --color=always -rni "$SEARCH" $TMPDIR
# Shred files
if type shred > /dev/null ; then
# shred is present in PATH
find ${TMPDIR} -type f -exec shred {} \;
fi
rm -rf ${TMPDIR}
else
echo "Error decrypting files.";
fi
exit 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment