Click to See Complete Forum and Search --> : Using "date" command in a bash script


Floog
10-12-2003, 05:02 PM
I've made a very simple bash script that I named archive.sh

What I would like to do is use some of the "date" command output to name create and name a new sub-directory where all the newly created .tar.bz2 files will be stored.

Basically, my script takes different directories and tars and bzips the data.
Example:
tar -cjpf /archive/foo1.tar.bz2 /home/foo1
tar -cjpf /archive/foo2.tar.bz2 /home/foo2

Once the script completes all the tar files, I would like to somehow run the "date" command, take some of the output and use that output to create a new sub-directory of /archive
Example:
~$date
Sun Oct 12 17:00:06 EDT 2003

I'd like to take the "Oct 12 . . . 2003" and create a subdirectory called /archive/Oct_12_2003

Then, I'd do a simple move command, moving all the recently created *.tar.bz2 files into the /archive/Oct_12_2003 directory

So, every time I run my archive.sh script it would just create a new archive subdirectory with an easily identifiable date name.

I just don't know how to parse the "date" output and use it to create a new sub-directory.

Can anyone point me in the right direction to get started. Thanks for your time and patience.

Floog

bwkaz
10-12-2003, 08:10 PM
#!/bin/bash

DATESTRING=$(date)

echo $DATESTRING

echo $DATESTRING | sed -e 's/ /_/g'

DATESTRING2=$(date +%d%m%Y)

echo $DATESTRING2 Ought to get you started...

Floog
10-13-2003, 09:22 PM
Thanks for the guidance Bwkaz. I think I see where you're heading. I need to look into the "sed -e 's/ /_/g'" -- I don't really understand that stuff. Doing "man sed" right now.

I appreciate you taking the time.

Floog

Originally posted by bwkaz
#!/bin/bash

echo $DATESTRING | sed -e 's/ /_/g'

DATESTRING2=$(date +%d%m%Y)

echo $DATESTRING2

bsh152s
10-16-2003, 02:42 PM
Actually, you don't have to parse the date output. The date command does enough for you.

dirname=`date +"%b_%d_%Y"`
mkdir $dirname

Floog
10-19-2003, 10:13 PM
Hi BSH,

I've only just started to learn and apply a little bash, python, and php. What does the modulus "%" command do exactly.

Can you state the quoted command below into a written sentence. I'm not exactly sure what the command says and how it is applying the "date" output.

Thanks for your response. I apologize for taking a few days to read your post. I can't stand it when my personal life interferes with technological relationships. :-)

Floog



Originally posted by bsh152s

dirname=`date +"%b_%d_%Y"`

bsh152s
10-20-2003, 08:45 AM
dirname=`date +"%b_%d_%Y"`

The %b, %d, and %Y are just format specifiiers for the date command (b=Abbreviated Month name, d=day of month, Y=four digit year). I found these option in my pride an joy--"Unix in a Nutshell". I highly recommend O'Reilly's nutshell series for desktop references. If your thing is man pages, "man date" refers to strftime in relation to the format specifiers. So, just do a "man strftime" for a complete list and description of the formats.

bwkaz
10-20-2003, 06:30 PM
Originally posted by Floog
Can you state the quoted command below into a written sentence. It goes like this:

(1) Run the command date +"%b_%d_%Y"

(2) Take the output of that command and substitute it in this line in place of the backtick-delimited expression

(3) Assign that value to the variable named dirname.

As bsh152s said, the %'s are just to introduce format specifiers, they don't cause any modulus arithmetic or anything -- not in this command anyway. :)

Floog
10-21-2003, 10:34 AM
ahh, i see. the fog is beginning to lift.

thanks for all your help, fellas. i'll be giving it a try this weekend while making a few alternate back-ups.

and i think i may be hitting amazon.com for an O'Reilly nutshell book, or three. i really need to get some more bash, sed, and awk under my belt.


floog