LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-15-2010, 10:59 PM   #1
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Rep: Reputation: 15
Archive Date Path


I have a script which archives the processed files to an 'archive' directory.

Code:
DATE=$(date "+%Y%m%d%H%M")
cp $LOGFOLDER\data/${HOST[$i]}.csv $LOGFOLDER\archive/${HOST[$i]}_$DATE.csv
gzip $LOGFOLDER\archive/${HOST[$i]}_$DATE.csv
md5sum $LOGFOLDER\archive/${HOST[$i]}_$DATE.csv.gz > $LOGFOLDER\archive/${HOST[$i]}_$DATE.csv.gz.md5
I would like to organize the archives by directory 'days' for example like this:

Code:
/Archives/2010/11/16/
/Archives/2010/11/17/
/Archives/2010/11/18/
Is it as simple as appending the year, month & day to the file path?

Thanks & Regards,
 
Old 11-15-2010, 11:48 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I am not real sure I understand the correlation between your current information and the desired setup.
Quote:
Is it as simple as appending the year, month & day to the file path?
yes it is that simple to use mkdir create the directory structure you require

Is there a relationship between archive and Archives?

How are you making the directory structure currently?
 
1 members found this post helpful.
Old 11-16-2010, 12:16 AM   #3
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Original Poster
Rep: Reputation: 15
Thanks for the response.

So I would do something like this? Sorry, I dont have access to a shell prompt at this moment in time.

Code:
DATE=$(date "+%Y%m%d%H%M")
YEAR=$(date "+%Y")
MONTH=$(date "+%m")
DAY=$(date "+%d")

mkdir $LOGFOLDER\data\$YEAR\$MONTH\$DAY
cp $LOGFOLDER\data/${HOST[$i]}.csv $LOGFOLDER\archive\$YEAR\$MONTH\DAY/${HOST[$i]}_$DATE.csv
gzip $LOGFOLDER\archive\$YEAR\$MONTH\DAY/${HOST[$i]}_$DATE.csv
md5sum $LOGFOLDER\archive\$YEAR\$MONTH\DAY/${HOST[$i]}_$DATE.csv.gz > $LOGFOLDER\archive\$YEAR\$MONTH\DAY/${HOST[$i]}_$DATE.csv.gz.md5
 
Old 11-16-2010, 12:23 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by hattori.hanzo View Post
So I would do something like this?
Yes but it would be shorter and easier to understand (and would avoid the DAY for $DAY typo) as
Code:
DATE=$(date "+%Y%m%d%H%M")
YEAR=$(date "+%Y")
MONTH=$(date "+%m")
DAY=$(date "+%d")
DATA_DIR=$LOGFOLDER\data\$YEAR\$MONTH\$DAY
ARCHIVE_DIR=$LOGFOLDER\archive\$YEAR\$MONTH\$DAY

mkdir $DATA_DIR $ARCHIVE_DIR
cp $LOGFOLDER\data/${HOST[$i]}.csv $DATA_DIR/${HOST[$i]}_$DATE.csv
gzip $ARCHIVE_DIR/${HOST[$i]}_$DATE.csv
md5sum $ARCHIVE_DIR/${HOST[$i]}_$DATE.csv.gz > $ARCHIVE_DIR/${HOST[$i]}_$DATE.csv.gz.md5
EDIT: I just noticed that the original scrippet makes the data directory but does not use it, using the archive directory instead. Dropping the data directory and adding BASENAME it becomes:
Code:
DATE=$(date "+%Y%m%d%H%M")
YEAR=$(date "+%Y")
MONTH=$(date "+%m")
DAY=$(date "+%d")
DIR=$LOGFOLDER\archive\$YEAR\$MONTH\$DAY
BASENAME=${HOST[$i]}.csv

mkdir $DIR
cp $LOGFOLDER\data/$BASENAME $DIR
gzip $DIR/$BASENAME
md5sum $DIR/$BASENAME.gz > $DIR/$BASENAME.gz.md5

Last edited by catkin; 11-16-2010 at 12:33 AM.
 
Old 11-16-2010, 12:23 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I am not sure why some of your slashes are in different directions, ie / or \, but as the structure may not already exist
you should use the -p option to be sure to create any parents required:
Code:
mkdir -p $LOGFOLDER\data\$YEAR\$MONTH\$DAY
 
Old 11-16-2010, 12:39 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by grail View Post
Well I am not sure why some of your slashes are in different directions, ie / or \
Hi grail

Oops! I missed that! It will cause breakage. Here's what happens testing at the command line:
Code:
c@CW8:~$ $ARCHIVE_DIR/$BASENAME^C
c@CW8:~$ DATE=$(date "+%Y%m%d%H%M")
c@CW8:~$ YEAR=$(date "+%Y")
c@CW8:~$ MONTH=$(date "+%m")
c@CW8:~$ DAY=$(date "+%d")
c@CW8:~$ DIR=$LOGFOLDER\data\$YEAR\$MONTH\$DAY
c@CW8:~$ echo $DIR
data$YEAR$MONTH$DAY
Must be DIR=$LOGFOLDER/data/$YEAR/$MONTH/$DAY
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help writing Unix Shell script to pass file name,path,creation date ! windjashi Programming 4 10-08-2009 12:59 AM
[SOLVED] How do I change ca.archive.ubuntu.com to ca2.archive.ubuntu.com at the command line kennymw Linux - Newbie 3 09-01-2009 02:52 AM
Setting system date and time affecting the clock and date on BIOS satimis Ubuntu 7 09-21-2007 08:02 AM
TAR Archive file - need relative path cmd0a0 Linux - Software 2 10-19-2005 02:57 PM
importing an MS Outlook archive archive.pst alloydog Linux - Software 2 08-29-2003 03:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration