Hi,
After checking all the UNIX threads, I am able to come up with a solution so far. I am working on a shell script where it moves the files to a certain directory. The conditions to check are
1) Check if the file exists in the current directory.
2) Check if the destination directory exists.
3) If the destination directory exists, then create a Year sub directory under it. If the Year sub directory already exists, then skip to the next step.
4) If the Year sub directory exists, then check if the Date sub directory exists. If it exists, move the file else create the sub directory and move it.
I did work on the script and it is erroring out. I was wondering if this is the right way to do it or is there is a better way to do it.
Please advice...
Code:
#Check Number of Parms
if [ $# -eq 3 ]
then
echo ""
echo "Start of $0"
echo ""
else
echo "Usage: $0 <INPUT DIRECTORY> <DEST DIRECTORY> <FILE NAME> "
exit 1
fi
#|-----------------------------------------------------------------------------|
#| Initialize Variables
#|-----------------------------------------------------------------------------|
INPUTDIR=$1
DESTDIR=$2
FILENAME=$3
FILE_SUFFIX=`date '+%Y-%m-%d-%H.%M.%S.000000'`
YEAR_DIR=${DESTDIR}/`date +%Y`
DATE_DIR=${YEAR_DIR}/`date +%Y%m%d`
#|-----------------------------------------------------------------------------|
#| Check for existence of the file name in the specified directory
#|-----------------------------------------------------------------------------|
cd ${INPUTDIR}
if [ ! -s "${FILENAME}" ]
then
echo "\tError: File $FILENAME Found, Archiving!"
else
echo "\tError: File $FILENAME Not Found, Aborting!"
exit 1
fi
#|-----------------------------------------------------------------------------|
#| Check for existence of the Year directory in the specified destination
#| directory. If it doesn't exist, then create it. Example: 2007
#|-----------------------------------------------------------------------------|
cd ${DESTDIR}
if [ ! -d "${YEAR_DIR}" ] || [ ! -w "${YEAR_DIR}" ]
then
echo "\nERROR: ${YEAR_DIR} is not a directory..Making ${YEAR_DIR} one!"
mkdir ${DIR}/${YEAR_DIR}
else
echo "\nERROR: Cannot create a directory"
exit 1
fi
#|-----------------------------------------------------------------------------|
#| Check for existence of the Date directory in the Year directory under
#| destination directory. If it doesn't exist, then create it. Example: 20071210
#|-----------------------------------------------------------------------------|
cd ${YEAR_DIR}
if [ ! -d "${DATE_DIR}" ] || [ ! -w "${DATE_DIR}" ]
then
mkdir ${YEAR_DIR}/${DATE_DIR}
mv ${FILENAME} ${FILENAME}.$FILE_SUFFIX "${DATE_DIR}"
echo "\tRenamed and appended the suffix ${FILE_SUFFIX}"
else
echo "\nERROR: ${DATE_DIR} is not a directory or is not writable"
exit 1
fi
echo ""
echo "End of $0"