From 287e6579b5ee1180fd60cafc8429e0a613f25fc9 Mon Sep 17 00:00:00 2001 From: Sean Hickey Date: Sat, 5 Jul 2014 23:49:04 -0400 Subject: [PATCH] Initial commit --- README.md | 19 ++++++++++++ zipDirs.sh | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100755 zipDirs.sh diff --git a/README.md b/README.md index c295a66..6df8ab9 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,22 @@ ZipDirs ======= A bash script to zip archive the directories within a directory. + +This script is designed to zip archive all the directories within a +given directory (or the current one if unspecified) and output the +.zips to another directory (or the current one). Since this uses the +'zip' program, it will also update the zips if they already exist. + +An example use case (what I needed it for in the first place) is to +zip up all the individual artists in a 'music' folder + +For example: +$ ./zipDirs.sh ~/Music ~/Music-zipped + +The output of that command will be all the dirs within ~/Music are +zipped and placed into ~/Music-zipped +(e.g. ~/Music/Something -> ~/Music-zipped/Something.zip) + +Created by Sean Hickey (Wisellama), 2014 +Released to the Public Domain under the terms of the Unlicense +http://unlicense.org/ diff --git a/zipDirs.sh b/zipDirs.sh new file mode 100755 index 0000000..3648bda --- /dev/null +++ b/zipDirs.sh @@ -0,0 +1,89 @@ +#!/bin/bash + +# This script is designed to zip archive all the directories within a +# given directory (or the current one if unspecified) and output the +# .zips to another directory (or the current one). Since this uses +# the 'zip' program, it will also update the zips if they already +# exist. +# +# An example use case (what I needed it for in the first place) is to +# zip up all the individual artists in a 'music' folder +# +# For example: +# $ ./zipDirs.sh ~/Music ~/Music-zipped +# +# The output of that command will be all the dirs within ~/Music are +# zipped and placed into ~/Music-zipped +# (e.g. ~/Music/Something -> ~/Music-zipped/Something.zip) +# +# Created by Sean Hickey (Wisellama), 2014 +# Released to the Public Domain under the terms of the Unlicense +# http://unlicense.org/ +# +# Now lets begin! + +startDir=`pwd` + +# the directory that contains all the dirs to be zipped up +dirToZip=$1 +if [ -z "$dirToZip" ] +then + dirToZip=. +fi + +# the directory where the .zips will be placed +outputDir=$2 +if [ -z "$outputDir" ] +then + outputDir=. +fi + +# convert to global paths +if [ ${dirToZip:0:1} != "/" ] +then + dirToZip=$startDir/$dirToZip +fi +if [ ${outputDir:0:1} != "/" ] +then + outputDir=$startDir/$outputDir +fi + +# Check if they are indeed directories and +# create the output dir if it doesn't exist. +# Side note: I didn't see a good way to negate the '-d' tag, so +# that's why the first if part is a nop (the ':'). +# (it didn't like using '!' before it, as in [ ! -d "$dir" ]) +if [ -d "$dirToZip" ] +then + : +else + echo "$dirToZip is not a directory, quitting." + exit 2 +fi +if [ -d "$outputDir" ] +then + : +else + if [ -e "$outputDir" ] + then + echo "$outputDir is not a directory, quitting." + exit 2 + else + mkdir "$outputDir" + fi +fi + +# go to the directory to be zipped up +cd "$dirToZip" + +if [ $? -ne 0 ] +then + exit $? +fi + +# zip 'em up +# ignore any hidden folders +find . -maxdepth 1 -type d \( ! -iname ".*" \) -exec zip -r "$outputDir/{}.zip" {} \; + + +# May the Llamas spit upon ye.