The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
File Archive Script KeesH Shell Programming and Scripting 10 06-09-2008 08:11 AM
Script to archive file indira UNIX for Dummies Questions & Answers 0 05-22-2007 07:15 AM
How to transfer files (By using generic script) when using sftp gsri Shell Programming and Scripting 0 08-28-2006 06:39 PM
script to archive all the log files tintedwindow Shell Programming and Scripting 0 06-13-2006 07:51 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-23-2008
Registered User
 

Join Date: Apr 2008
Posts: 10
Generic Shell Script to Archive a file

Would appreciate if any one can paste a generic schell script to archive a file with date stamp by passing the file with fullpath as parameter

For Eg. /apps/scripts/Archive_File.sh /data_home/project_home/file.txt

this should place the file in the following directory

/data_home/project_home/Archive

Any help is really appreciated...I'm kinda new to KSH and please help me.

Thanks
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-23-2008
Registered User
 

Join Date: Jul 2008
Location: BlackMesh Managed Hosting
Posts: 57
Not a big ksh person, but how about:

Code:
mv $1 `echo $1 | sed 's/\/[^/][^/]*//'`/Archive/`echo $1 | sed 's/.*\///'`.`date +"%Y-%m-%d"`
N.b. not intended to backup entire directories at a time, but if you do, drop the trailing slash (or just edit the script to do it for you; I wanted to keep it simple).
Reply With Quote
  #3 (permalink)  
Old 07-23-2008
Registered User
 

Join Date: Apr 2008
Posts: 10
awesome...Thanks a lot for your help...a little bit of refinement...

For some directories it is ARCHIVE, for some archive and for some it is Archive.....How do I include this condition to check for the available archive directory
Reply With Quote
  #4 (permalink)  
Old 07-23-2008
Registered User
 

Join Date: Apr 2008
Posts: 10
Actually I could not get it to work

here is the error that I get

+ sed s/\/[^/][^/]*//
+ echo test_file.txt
+ sed s/.*\///
+ echo test_file.txt
+ date +%Y-%m-%d
+ mv test_file.txt test_file.txt/ARCHIVE/test_file.txt.2008-07-23
mv: cannot rename test_file.txt to test_file.txt/ARCHIVE/test_file.txt.2008-07-23: Not a directory

File Location /data_home/scripts/test_file.txt
Archive Location /data_home/scripts/ARCHIVE

Thanks for your help
Reply With Quote
  #5 (permalink)  
Old 07-24-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,665
Try this:

Code:
echo $1|sed 's!\(.*/\)\(.*\)!mv & \1ARCHIVE/\2!' | sh
Before you mv the file be sure you get the expected command with:

Code:
echo $1|sed 's!\(.*/\)\(.*\)!mv & \1ARCHIVE/\2!'
Reply With Quote
  #6 (permalink)  
Old 07-24-2008
Registered User
 

Join Date: Apr 2008
Posts: 10
It works perfectly...Thanks a lot for your help....
Would like to know if there is a way to see if the check if the directories ARCHIVE/archive/Archive exists and based on that move the files to the Archive directory...since I want to write a generic script that archives the files to archive directory...but for some projects the archive directory is ARCHIVE, for some it is Archive and for others it is Archive.

Once agian thanks for all the help
Reply With Quote
  #7 (permalink)  
Old 07-25-2008
Registered User
 

Join Date: Apr 2008
Posts: 10
any ideas?
Reply With Quote
Google UNIX.COM
Reply

Tags
archive, ksh

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0