Question

Locked

bash rookie needs help w/ variable

By 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!

This conversation is currently closed to new comments.

3 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

You pretty much have it

by deity_chooch In reply to bash rookie needs help w/ ...

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>

Collapse -

nice!

by Churdoo In reply to You pretty much have it

nice response, deity!

Give this guy a thumb!

Back to Software Forum
3 total posts (Page 1 of 1)  

Software Forums