Skip to content

Instantly share code, notes, and snippets.

@chjj
Created October 8, 2011 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chjj/1272607 to your computer and use it in GitHub Desktop.
Save chjj/1272607 to your computer and use it in GitHub Desktop.
script for people who dont like file managers
#!/bin/sh
# easy manipulation of archives for people
# who don't like archivers or file managers
# copyright (c) 2011, christopher jeffrey
LIST=
EXTRACT=
CREATE=
ENCRYPT=
case "$1" in
l | -l | list | --list)
LIST=1
;;
e | -e | extract | --extract)
EXTRACT=1
;;
c | -c | create | --create)
CREATE=1
;;
r | -r | crypt | --crypt)
ENCRYPT=1
;;
--install-deps)
if [ -n `which apt-get` ]; then
sudo apt-get install \
zip unzip p7zip-full rar unrar-free tar \
binutils openssl genisoimage p7zip-rar
elif [ -n `which pacman` ]; then
sudo pacman -S \
zip unzip p7zip unrar tar binutils openssl cdrkit
elif [ -n `which yum` ]; then
sudo yum install \
zip unzip p7zip tar binutils openssl cdrkit
else
echo "Package manager not found."
fi
exit 0
;;
* | -h | help | --help)
SCRIPT=`basename "$0"`
echo "Usage:"
echo " $SCRIPT option archive [file ...]"
echo ""
echo "Description"
echo " Unified interface for dealing with archives."
echo ""
echo "Options:"
echo " l, -l, list, --list - list contents"
echo " e, -e, extract, --extract - extract contents"
echo " c, -c, create, --create - create archive"
echo " r, -r, crypt, --crypt - encrypt or decrypt"
echo " --install-deps - install dependencies"
echo ""
echo "Dependencies"
echo " ar, tar, zip, unzip, rar, unrar, cdrkit"
echo " or p7zip*(zip, unzip, rar, unrar)"
exit 0
;;
esac
FILE="$2"
if [ -z "$FILE" ]; then
echo "Please provide an archive filename."
exit 0
fi
shift
shift
NAME=`basename "$FILE"`
if [ -n "$LIST" ]; then
if [ ! -f "$FILE" ]; then
echo "$FILE does not exist."
exit 0
fi
case "$NAME" in
*.a | *.ar | *.deb)
ar vt "$FILE"
;;
*.tar.gz | *.tgz)
tar -vtzf "$FILE"
;;
*.tar.bz2 | *.tbz2 | *.tar.bz | *.tbz)
tar -vtjf "$FILE"
;;
*.tar.xz | *.txz)
tar -vtJf "$FILE"
;;
*.tar)
tar -vtf "$FILE"
;;
*.zip)
if [ -n `which unzip` ]; then
unzip -l "$FILE"
else
7z l -tzip "$FILE"
fi
;;
*.7z | *.rpm)
7z l "$FILE"
;;
*.rar)
if [ -n `which unrar` ]; then
unrar l "$FILE"
elif [ -n `which unrar-free` ]; then
unrar-free -t "$FILE"
elif [ -f "/usr/lib/p7zip/Codecs/Rar29.so" ]; then
7z l -trar "$FILE"
else
echo "No rar support."
fi
;;
*.iso)
if [ -n `isoinfo` ]; then
isoinfo -l -J -R -i "$FILE"
else
7z l -tiso "$FILE"
fi
;;
*)
echo "Please enter a valid archive."
exit 0
;;
esac
sha1sum "$FILE"
fi
if [ -n "$EXTRACT" ]; then
if [ ! -f "$FILE" ]; then
echo "$FILE does not exist."
exit 0
fi
DIRECTORY=`echo "$NAME" \
| sed 's/\(.\+\)\..\+/\1/' \
| sed 's/\(.\+\)\..\+/\1/'`
DIRECTORY="${DIRECTORY}-"
if [ -d "$DIRECTORY" ]; then
echo "Directory already exists."
exit 0
fi
mkdir "$DIRECTORY"
case "$NAME" in
*.a | *.ar | *.deb)
cd "$DIRECTORY"
ar vx "$FILE"
;;
*.tar.gz | *.tgz)
tar -vxzf "$FILE" -C "$DIRECTORY"
;;
*.tar.bz2 | *.tbz2 | *.tar.bz | *.tbz)
tar -vxjf "$FILE" -C "$DIRECTORY"
;;
*.tar.xz | *.txz)
tar -vxJf "$FILE" -C "$DIRECTORY"
;;
*.tar)
tar -vxf "$FILE" -C "$DIRECTORY"
;;
*.zip)
if [ -n `which unzip` ]; then
unzip "$FILE" -d "$DIRECTORY"
else
7z e -tzip -o"$FIRECTORY" "$FILE"
fi
;;
*.7z | *.rpm)
7z e -o"$DIRECTORY" "$FILE"
;;
*.rar)
if [ -n `which unrar` ]; then
unrar x "$FILE" "$DIRECTORY"
elif [ -n `which unrar-free` ]; then
unrar-free -x "$FILE" "$DIRECTORY"
elif [ -f "/usr/lib/p7zip/Codecs/Rar29.so" ]; then
7z e -trar -o"$DIRECTORY" "$FILE"
else
echo "No rar support."
fi
;;
*.gz)
rm "$DIRECTORY"
gzip -d "$FILE"
;;
*.bz2 | *.bz)
rm "$DIRECTORY"
bzip2 -d "$FILE"
;;
*.xz)
rm "$DIRECTORY"
xz -d "$FILE"
;;
*.iso)
if [ -n `which 7z` ]; then
7z e -tiso -o"$DIRECTORY" "$FILE"
elif [ -n `which isoinfo` ]; then
# ridiculously convoluted way to extract
# probably doesnt work
isoinfo -l -R -J -i "$FILE" | while read LINE ; do
LINE=`echo "$LINE" | sed 's/^.*\s\(\S\+\)\s*$/\1/'`
test -z "$LINE" && continue
test "$LINE" = "/" && continue
test "$LINE" = "." && continue
test "$LINE" = ".." && continue
FLAT=`basename "$LINE"`
isoinfo -i "$FILE" -x "$LINE" > "${DIRECTORY}/$FLAT"
done
fi
;;
*)
echo "Please enter a valid archive."
exit 0
;;
esac
fi
if [ -n "$CREATE" ]; then
if [ -e "$FILE" ]; then
echo "$FILE already exists."
exit 0
fi
case "$NAME" in
*.a | *.ar | *.deb)
ar vrcs "$FILE" "$@"
;;
*.tar.gz | *.tgz)
tar -vczf "$FILE" "$@"
;;
*.tar.bz2 | *.tbz2 | *.tar.bz | *.tbz)
tar -vcjf "$FILE" "$@"
;;
*.tar.xz | *.txz)
tar -vcJf "$FILE" "$@"
;;
*.tar)
tar -vcf "$FILE" "$@"
;;
*.zip)
if [ -n `which zip` ]; then
FILE=`echo "$FILE" \
| sed 's/^\(.\+\)\(\..\+\)$/\1/'`
zip -r "$FILE" "$@"
else
7z a -tzip "$FILE" "$@"
fi
;;
*.7z)
7z a "$FILE" "$@"
;;
*.rar)
if [ -n `which rar` ]; then
rar a "$FILE" "$@"
elif [ -f "/usr/lib/p7zip/Codecs/Rar29.so" ]; then
7z a -trar "$FILE" "$@"
else
echo "No rar support."
fi
;;
*.iso)
genisoimage -r -J -o "$FILE" "$@"
;;
*)
echo "Please enter a valid archive."
exit 0
;;
esac
sha1sum "$FILE"
fi
if [ -n "$ENCRYPT" ]; then
if [ ! -f "$FILE" ]; then
echo "$FILE does not exist."
exit 0
fi
CIPHER="$1"
if [ -z "$CIPHER" ]; then
#CIPHER='AES-128-CBC'
CIPHER='aes128'
fi
case "$NAME" in
*.enc)
OUT=`echo "$FILE" | sed 's/\.enc$//'`
if [ -f "$OUT" ]; then
echo "$OUT already exists."
exit 0
fi
openssl "$CIPHER" -d -a -in "$FILE" -out "$OUT"
sha1sum "$OUT"
;;
*)
if [ -f "${FILE}.enc" ]; then
echo "${FILE}.enc already exists."
exit 0
fi
openssl "$CIPHER" -a -salt -in "$FILE" -out "${FILE}.enc"
sha1sum "${FILE}.enc"
#openssl sha1 "${FILE}.enc"
;;
esac
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment