Sunday 10 August 2014

Pack Up Tar Files Script

This script is used to pack up files into a "tar.gz", this can easily be change to a "tar.xz" by replacing all instances of "tar.gz".
This script just makes life a bit easier. The script uses parameters so the format would be:
./pack.sh afile.txt anotherFile.txt (and so on)
#!/bin/bash
# if no parameters then show help
# $@ is the parameter 
# -z checks is parameter is empty and if so then show help. 
if [[ -z "$@" ]]; then
#echo " ${0##*/}  <*file/dir2> <*name>.tar.gz - archive file/folders"
# ${0##*/} show name of file
  echo " ${0##*/}  <*file/dir2> - archive and compress files and dirs"
  exit
fi

# choose a file name for your new tar.gz
#read the line, -p means prompt for a responce 
while true; do
  read -p " Enter archive filename: " filename
# -n means none emtpy string (checking that you wrote something)
  if [ -n "$filename" ]; then
    break
  fi
done

# Check if selection(s) exists
# for loop looping through the two files and checking there ok
for a in "$@"; do
  if [ ! -f "$a" ]; then
    echo " Selection \""$@"\" does not exist."
    exit
  fi
done

# Create archive
tar -c --xz -f "$filename".tar.gz "$@" && \
#-c means create file.
#--xz means extract files and zip them up
#-f means file
echo " Created "$filename".tar.gz"

No comments:

Post a Comment