Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Create Year directory, date subdirectory and archive the file Post 302150768 by madhunk on Wednesday 12th of December 2007 03:03:02 PM
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"

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
asadmin-stop-appserv(1AS)					   User Commands					 asadmin-stop-appserv(1AS)

NAME
asadmin-stop-appserv, stop-appserv - stops the domains inthe specified directory SYNOPSIS
stop-appserv [--domaindir install_dir/domains] [--terse=false] [--echo=false] This command has been deprecated. Please use stop-domain to stop the domains in the specified or default domains directory. stop-appserv stops the domains in the specified or default domains directory. If the domains directory is not specified, the domains in the default domains directory are stopped. The default domains directory is install_dir/domains. This command can only be run locally. OPTIONS
--domaindir directory where the domains are stopped. If specified, path must be accessible in the filesystem. If not specified, the domain is stopped in the default install_dir/domains directory. --terse indicates that any output data must be very concise, typically avoiding human-friendly sentences and favoring well- formatted data for consumption by a script. Default is false. --echo setting to true will echo the command line statement on the standard output. Default is false. EXIT STATUS
0 command executed successfully 1 error in executing the command asadmin-create-domain(1AS), asadmin-start-domain(1AS), asadmin-stop-domain(1AS), asadmin-delete-domain(1AS), asadmin-list-domains(1AS) J2EE 1.4 SDK March 2004 asadmin-stop-appserv(1AS)
All times are GMT -4. The time now is 09:48 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy