Skip to content

Instantly share code, notes, and snippets.

@branquito
Created November 6, 2013 00:06
Show Gist options
  • Save branquito/7328614 to your computer and use it in GitHub Desktop.
Save branquito/7328614 to your computer and use it in GitHub Desktop.
Find-Extract files from zip archives
#!/usr/bin/bash
# author Branchito
# Usage
# This script takes filenames as arguments, as many as you wish
# and then searches through all .zip archives for those filenames
# and extracts them
# if you do not wish to specify args manually on the command line
# call script this way..
# ./find-images $(< filelist),
# where filelist is a file, containing list of files, each on
# a separate line.
# No zero args allowed.
if [ $# -eq 0 ]
then
echo "Usage: `basename $0` arg1 [ arg2, ... ]"
exit 0
fi
# No empty args allowed
if [ -z "$1" ]
then
echo "Argument cannot be empty!"
exit 89
fi
# all OK, so continue
while :
do
echo -n "Enter a directory name to store files in: "
read dirname junk
[[ ! -z $dirname ]] && break
done
# if dir exists do not make one, but continue extracting files into it.
[[ ! -d "$dirname" ]] && mkdir "$dirname"
for var in "$@"
do
for f in *.zip
do
# check if file exists, if not then extract
# so only the first instance of found file will be extracted
# you may use one of these to change behaviour of unzip
# if desired, but in second instance bellow, after grep
# because that's what actually does the work.
# -n never overwrite existing files
# -o overwrite files WITHOUT prompting
# -j junk paths (do not make directories)
# -C match filenames case-insensitively
# -q quiet mode ( -qq => quieter )
# ..see unzip --help for more.
# By DEFAULT this script uses case insensitvity.
# Remove -C option from bellow line, to change that.
[[ ! -e "$dirname/$var" ]] && unzip -l $f | grep -iq "$var" && unzip -C $f "$var" -d $dirname
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment