Script for compressing files with gzip within the terminal with date
This is a simple bash script to compress in tar.gz (gzip) format a file or folder appending date to file name.
Save this file as gzip.sh and give it execution permissions. From terminal i could be: chmod 755 gzip.sh
This script does the following:
- Checks if you passed exactly 2 arguments
- Compresses your source
- Adds the date on the resulting filename
- Optionally changes permissions of your filename (uncomment line chown and put the desired user:group)
Script:
#!/bin/bash # Albert Lombarte # if [ $# != 2 ] # There must be two arguments then echo "--USAGE: $0 source target" exit 1 fi FILENAME=`date "+%Y-%b-%d_$2"` WHAT=$1 #====================================== echo "-- STARTED compression of $WHAT into $FILENAME" #Use 'sudo' command before commands if needed tar -c --gzip -f $FILENAME $WHAT #chown alombarte:bckgroup $FILENAME echo "-- FINISHED $WHAT" echo "" exit 1
or the same with less stuff...
#!/bin/bash # Albert Lombarte if [ $# != 2 ] then echo "--USAGE: $0 source target" exit 1 fi tar -c --gzip -f $1 $2
This example ...will result in 03-Aug-2005_alombarte.tar.gz
./gzip.sh /home/alombarte alombarte.tar.gz