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

Not a big ksh person, but how about:

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).

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

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

Try this:

echo $1|sed 's!\(.*/\)\(.*\)!mv & \1ARCHIVE/\2!' | sh

Before you mv the file be sure you get the expected command with:

echo $1|sed 's!\(.*/\)\(.*\)!mv & \1ARCHIVE/\2!'

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

any ideas?

One way to achieve this:

#!/bin/sh

file=$1

if [ -d ${file%/*}"/ARCHIVE" ]; then
  archive="ARCHIVE"
else
  archive="Archive"
fi

echo $file|sed "s_\(.*/\)\(.*\)_mv & \1$archive/\2_" | sh

Regards

You can use basename and dirname

mv "$1" "$(dirname $1)/Archive/$(basename $1).$(date +"%Y-%m-%d")"

or using parameter expansion

mv "$1" "${1%/*}/Archive/${1##*/}.$(date +"%Y-%m-%d")"