Wednesday, March 20, 2024

Print the content of all zipped files with names containing a regex

#!/bin/bash
#
# The below will print the content of all zipped files with names containing the keyword "application"
# ./print_zip_files_content.sh app.jar '.*application.*'
#
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <zip file path> <regex containing filename>"
exit 1
fi
ZIP_FILE="$1"
REGEX="$2"
# Using unzip -l to list files, then grep to filter by regex. awk to print the 4th column to the end.
# This handles spaces in filenames more gracefully.
unzip -l "$ZIP_FILE" | grep -P "$REGEX" | awk '{print $4}' | while read -r file; do
if [[ ! -z "$file" ]]; then
echo "File $file:"
unzip -p "$ZIP_FILE" "$file"
echo -e "\n"
fi
done

Followers