The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
create a archive of old file jayaramanit UNIX for Dummies Questions & Answers 2 04-22-2008 05:05 AM
want to create directory with the previous date sridhusha Shell Programming and Scripting 7 11-19-2007 05:44 AM
calculate directory size by year of file igidttam UNIX for Dummies Questions & Answers 11 09-25-2006 07:40 PM
Directory create date Bab00shka UNIX for Dummies Questions & Answers 2 06-29-2005 10:42 AM
create directory named current date flowrats UNIX for Dummies Questions & Answers 2 07-30-2002 11:26 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-12-2007
madhunk madhunk is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 91
Create Year directory, date subdirectory and archive the file

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"
  #2 (permalink)  
Old 12-12-2007
Lakris Lakris is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 243
Well, there are a few things in Your script that I find a bit strange. Do You want to call this script for each file You want to process? Why do You want nanoseconds if they are always static? Don't You just want to check for existence of a file? And there are a few other syntax errors as well (for example DIR is not defined) and some things will work with absolute paths but not relative.

Anyway, strip off some stuff (to make it shorter) and get to the essence of if, try the following. Unless it is important to know if a directory exist or not, just create it when You need it. An example:

Code:
if [ ! $# -eq 3 ]
then
  echo "Usage: $0 <INPUT DIRECTORY> <DEST DIRECTORY> <FILE NAME> "
  exit 1
fi

FILE_SUFFIX=`date '+%Y-%m-%d-%H.%M.%S.000000'`
FINAL_DEST_DIR=$2/`date +%Y`/`date +%Y%m%d`

if [ -f "$1/$3" ] 
then
    echo "File $3 Found in $1, moving it to $FINAL_DEST_DIR/$3.$FILE_SUFFIX"
    mkdir -p $FINAL_DEST_DIR
    mv $1/$3 $FINAL_DEST_DIR/$3.$FILE_SUFFIX
else
    echo "\tError: File $3 Not Found"
fi
Maybe it can help You a bit on the way.
  #3 (permalink)  
Old 12-13-2007
madhunk madhunk is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 91
Thank you very much Lakris...The code looks much simpler and better..I am always learning better ways to do it.

Thanks again...
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0