Monday, May 18, 2015

Litle Bit of Shell Script - Part 2


I Just made a change in the script. Actually, the script was working just fine but I had only one single problem with it. If I had two file on the directory to be compressed, the script would make only one compressed file. Although this situation might not happen in the environment I'd like to implement this script, I was not comfortable with it. Tried many ways to overcome this problem but, in fact, the original script with only one change was able to give me the result I wanted.
The original script was:

NOW=$(date +"%m-%d-%Y")
FILE="Prime-Backup.$NOW.tar.gz.$$"
tar  -zcf  $FILE  $files

Then a change to :

NOW=$(date +"%m-%d-%Y")
    tar -cz -f "$file".$NOW.MyFile.tar.gz  "$file"

I Keep the variable "NOW" because it is important to define the file's date but I changed the line that was screw up the results. As I was putting the final file in a variable called $FILE, I always had only on file as output. As soon as I specify the $file, the script start to compress by file. To keep things organized, I put the Date command inside the file's name. Now is complete. Everything the script supposed to do, it finally does.


#!/bin/bash

#Create a reference file
touch -d -20days "/tmp/20dayref.$$"

#Go to the directory in which my files resides
cd /home/pendrive

#Looking for file with specific characteristics
find . -type f  -name 't*'  -print | while read file

#if found, do
do
#If file is newer than the reference file, and if has the specific characteristics
    if [[ ("$file" -nt "/tmp/20dayref.$$") && ( "$file" =~ \.txt$) ]]

    then

#Compress the file using the current date and some specific information

echo "File": $file " is newer  than 20 days"
    echo "Compacting...."
    NOW=$(date +"%m-%d-%Y")
    tar -cz -f "$file".$NOW.MyFile.tar.gz  "$file"

#For clean up metter, delete original  files
    echo "Deleting original backup file...": $file
    rm $file

#If file is older than the reference file

elif [[ "$file" -ot "/tmp/20dayref.$$" ]]

then

#Delete it

    echo "File": $file "is older than 20 days"
    echo $file
    echo "Deleted  permanently...."
    rm $file

else
#If nothing is found to do, just print the success message
echo "Job successfully completed!!!!"

    fi
done

1 comment:

  1. It is a very informative and useful post thanks it is good material to read this post increases my knowledge. Gestión de proyectos

    ReplyDelete