Bash unzip script

				
					#!/bin/bash
# script to decompress zips and copy used zips to dir

# the important directories
my_zip_dir="/home/USER/zip_dir"
my_pict_dir="/home/USER/pict_dir"


# the testing ----------------------------------------
# are there zip files
ls *.zip > /dev/null 2>&1 

if [ $? -eq 0 ]; then
    echo "zip files present, programm continues"
else
    echo ERROR: no zip files found programm stops
    exit 1
fi


# does pict dir exist
if [ -d "$my_pict_dir" ]; then
  	echo " ${my_pict_dir}..already there everything ok"
else
	echo ERROR: picture dir doesnot exists programms stops
    exit 1
fi


# does zip dir exists
if [ -d "$my_zip_dir" ]; then
  	echo " ${my_zip_dir}..already there"
else
	mkdir  "$my_zip_dir"
fi

# end of testing ---------------------------------------------


# the unzipping ---------------------------------------------
for i in *.zip;
	do filename=$(basename "$i")
	extension="${filename##*.}"
	filename="${filename%.*}"
	echo "$filename"

	DIR=/home/USER/target_dir/"$filename"
    
    if [ -d "$DIR" ]; then
  		echo " ${DIR}..already there"
	else
		mkdir  "$DIR"
	fi

	if [ $? -eq 0 ]; then
		unzip -nq "$i"  -d  "$DIR" 
	
		if [ $? -eq 0 ]; then
			echo "unzip ok"
			mv  "$i" "$my_zip_dir"
		else
			echo "unzip failed"
		fi
	else
		echo "could not make dir ${DIR} "
	fi
done
# ----------------------------------------------------------------
echo programm ended


				
			

This script was made to unpack a bunch of zip files with old pictures. I have found the script somewhere on the internet and adapted it to my own needs. 

What does the the script do:

  1. It test if zip files are present in the directory
  2. It tests whether your picture directory exists
  3. it tests if the directory where you want to put your zip files exists
  4. Makes a directory to store your files ins
  5.  Unzips the files in the newly made directory
  6. move the zip file to a storage directory

How to use it:

  1. Make the script executable with chmod +x
  2. Edit the lines with the important directories
  3. In terminal. change to directory where the zip files are.
  4. Copy the script into this directory and run it. 

For me it worked like a charm

 

HP

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top