-
-
Save bogn83/63ffac769f1a039e9a9218c4bd759951 to your computer and use it in GitHub Desktop.
A simple bash script to grep within a bunch of GPG encrypted files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Usage grep-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 | |
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 ..."; | |
function decrypt { | |
declare dir=${0} | |
declare relative=${1/#$dir\//} | |
declare full=${1} | |
mkdir -p $(dirname $full) | |
gpg --quiet --batch -d --output "${full}.txt" --yes --compress-algo=none --no-encrypt-to --use-agent "${relative}" > /dev/null | |
} | |
export -f decrypt | |
find ${@:2} -type f -name '*.gpg' -exec bash -c 'decrypt "$0/$1"' $TMPDIR {} \; | |
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 -n 25 -u -z {} \; | |
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