Questions

bash rookie needs help w/ variable

Tags:
+
0 Votes
Locked

bash rookie needs help w/ variable

asusag
Hi all...

I've been trying to find a way to do this, I know there is and I know it's probably simple but I cant wrap my brain around it.

I'm backing up a series of directories on a daily basis, creating one tar.gz file for each folder. This works fine. It currently saves the file name as todays date, but I'd like to add the name of each directory to the title of each tar.gz file, along with the date. So if a directory I'm archiving is named 'lobby' and it's November 30 2007, I want it named lobby113007.tar.gz

Here is my big whopping script...

filename=`date +%m%d%y`
variable to today's date.
for dir in /home/camera/*
do
tar -czvf /home/archives/$filename.tar.gz $dir
done

What kind of variable would I need to have it gather the directory name, then add it to the file?

Thanks!
  • +
    0 Votes
    deity_chooch

    All you'd need is to pipe the $dir variable through `sed` to get rid of the original directory name. One thing though... your FOR loop is just taking all files in the "/home/camera/" directory whether they are files or directories. You might want to add a `test` to make sure only the directories are tarballed.
    <br/><br/>
    <tt>#!/bin/bash<br/>
    <br/>
    DATE=`date +%m%d%y`<br/>
    for FILE in /home/camera/*; do<br/>
    if [ -d $FILE ]; then<br/>
    DIR=`echo $FILE | sed 's|/home/camera/||'`<br/>
    tar cvzf /home/archives/$DIR$DATE.tar.gz $FILE<br/>
    fi<br/>
    done</tt>

    +
    0 Votes
    Churdoo

    nice response, deity!

    Give this guy a thumb!

  • +
    0 Votes
    deity_chooch

    All you'd need is to pipe the $dir variable through `sed` to get rid of the original directory name. One thing though... your FOR loop is just taking all files in the "/home/camera/" directory whether they are files or directories. You might want to add a `test` to make sure only the directories are tarballed.
    <br/><br/>
    <tt>#!/bin/bash<br/>
    <br/>
    DATE=`date +%m%d%y`<br/>
    for FILE in /home/camera/*; do<br/>
    if [ -d $FILE ]; then<br/>
    DIR=`echo $FILE | sed 's|/home/camera/||'`<br/>
    tar cvzf /home/archives/$DIR$DATE.tar.gz $FILE<br/>
    fi<br/>
    done</tt>

    +
    0 Votes
    Churdoo

    nice response, deity!

    Give this guy a thumb!