Create Year directory, date subdirectory and archive the file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Create Year directory, date subdirectory and archive the file
# 1  
Old 12-12-2007
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  
Old 12-12-2007
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  
Old 12-13-2007
Thank you very much Lakris...The code looks much simpler and better..I am always learning better ways to do it.

Thanks again...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create file every year and log data to it

Hi there, Here is what I currently have and trying to come up with a logic to automatically append logs to a respective year file. The log files might get generated everyday or any other day, irrespective of which I want to copy all of the logs for that year into one file /tmp/<year>.log and then... (4 Replies)
Discussion started by: mbak
4 Replies

2. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

3. Shell Programming and Scripting

Searching File in Directory and all Subdirectory and Delete

Hi All, My directory structure is like Directory1 SubDirectory1 SubDirectory2 SubDirectory3 I have main directories and subdirectories underneath. I want to write a shell script where I will be passing file name as a parameter, Now I want to find all the files in Directory1... (19 Replies)
Discussion started by: John William
19 Replies

4. Shell Programming and Scripting

Move all files not in a directory into a subdirectory named for each given file

Hi Everyone! Looking for some help with a script that will take all files in any given root folder (which are not already in a folder) and put them into separate folders with the name of each given file. Any ideas? Thank you! (1 Reply)
Discussion started by: DanTheMan
1 Replies

5. Shell Programming and Scripting

How can i find a word inside a file from a directory and it's subdirectory ?

Suppose i have a word "mail". I have to search this word in all files inside a directory and it's sub-directories. It will also search in all hidden directory and sub-directories. If it finds this word in any file it will list that file. How can i do this with perl/ruby/awk/sed/bash or... (9 Replies)
Discussion started by: cola
9 Replies

6. Shell Programming and Scripting

Sum of file size in directory / subdirectory

Hi , I am trying to write something to find the size of particular type of files in a directory & it's subdirectory and sum the size .. These types of file are found at directory level or its subdirectories level .. #!/bin/ksh FNAME='.pdf' S_PATH=/abc/def/xyz find $S_PATH -exec ls -lad... (4 Replies)
Discussion started by: Vaddadi
4 Replies

7. Shell Programming and Scripting

Need script to select multiple files from archive directory based on the date range

hi all, here is the description to my problem. input parameters: $date1 & $date2 based on the range i need to select the archived files from the archived directory and moved them in to working directory. can u please help me in writing the code to select the multiple files based on the... (3 Replies)
Discussion started by: bbc17484
3 Replies

8. UNIX for Dummies Questions & Answers

create tar archive without preserving directory structure?

I am adding some individual files to a tar archive and would like them to be added to the archive without any directory hierarchy, even though the files themselves exist in levels of hierarchy. Unfortunately, tar seems to always preserve the directory hierarchy when it adds the files. Here is... (2 Replies)
Discussion started by: Special_K
2 Replies

9. UNIX for Dummies Questions & Answers

create a archive of old file

i need a script which will create a archive of the files older than 10 days........ (2 Replies)
Discussion started by: jayaramanit
2 Replies

10. Shell Programming and Scripting

How to calculate file's size in directory and subdirectory

Hi, I have written one script to calculate total space of all file in one directory, ignoring subdirectory, it works fine. Now, I've been trying to calculate all files which includes files in any subdirectories. I use recursive function to do this, but it can work only if there is only one... (4 Replies)
Discussion started by: KLL
4 Replies
Login or Register to Ask a Question