Script to move file on a daily basis


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to move file on a daily basis
# 1  
Old 04-24-2014
Script to move file on a daily basis

Hi!

Please I need help on the a script that would pick one file in a directory, change its name, them change its permissions, them move to a different directory, but has to be done on a daily basis, and the file that is being moved to its final destination has to have the following format:
Code:
xxxx_yyyy20140424.csv

, this is for today, and tomorrow must be
Code:
xxxx_yyyy20140425.csv

.

What I have wrote so far was:

Code:
#!/usr/bin/ksh
echo
#change permissions of a file
/usr/bin/chmod 777 /directory/subdirectory/File.csv
echo
#change file name
/usr/bin/mv File.csv xxxx_yyyy

I am stuck in that last line of the script.
# 2  
Old 04-24-2014
Code:
cd org_dir
_Y='%Y'
_y='%Y'
_m='%m'
_d='%d'
TIMESTAMP=`date "+$_Y$_m$_d"`
chmod 777 file.csv
mv file.csv /dest_dir/xxxx_$TIMESTAMP.csv

---------- Post updated at 04:15 AM ---------- Previous update was at 04:09 AM ----------

Additionaly you can use cronjob to call the script daily
This User Gave Thanks to Makarand Dodmis For This Post:
# 3  
Old 04-24-2014
How do I add a funtionality to warn me when a file was not moved to its destination directory, or if the script has failed.
# 4  
Old 04-24-2014
Add this at the end of the code

Code:
if [ -f ../dest_dir/xxxx_$TIMESTAMP.csv ]; then
echo "Moved"
else
echo "Not Moved"
fi
 
if [ $? -ne 0 ] # exit status is not 0
then
  echo "Error while transfering file at $(date)"
  exit $?
fi

If any command fails control will come in this if block

---------- Post updated at 09:58 AM ---------- Previous update was at 09:50 AM ----------

Updated Code

Code:
cd newfolder
_Y='%Y'
_y='%Y'
_m='%m'
_d='%d'
TIMESTAMP=`date "+$_Y$_m$_d"`
chmod 777 file.csv
mv file.csv ../dest_dir/xxxx_$TIMESTAMP.csv
if [ -f ../dest_dir/xxxx_$TIMESTAMP.csv ]; then
echo "Moved"
else
echo "Not Moved"
fi
 
if [ $? -ne 0 ] # exit status is not 0
then
  echo "Error while transfering file at $(date)"
  exit $?
fi

# 5  
Old 04-24-2014
I have put the script on a crontab, so that can be run 1AM, everyday, so I beleive the error message must come in an email format
# 6  
Old 04-24-2014
No it will not. how it will automaticaly come in email format.
you have to write

Code:
if [ $? -ne 0 ] # exit status is not 0
then
  echo "Error while transfering file at $(date)"
  echo "Error Occured" | mailx -s "Error" abc@xyz.com
  exit $?
fi

This User Gave Thanks to Makarand Dodmis For This Post:
# 7  
Old 04-24-2014
will that piece of code come in the second check
Code:
if

or first?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to count files on daily basis

Hi all I need to count files on a daily basis and send the output via email, what I am currently doing its simply running the following:ls -lrt *.cdr | awk '{if(($6 == "Jul") && ($7 == "13")) print $0}' | wc -l, which in this case it will count all files with extension "cdr" from the month of... (13 Replies)
Discussion started by: fretagi
13 Replies

2. Red Hat

UNIX script for daily file transfer ???

I need to create a script which needs to transfer the "<filename>+yyyymmdd".dat file from a unix machine to a linux machine.. daily. we can use an active batch to schedule the script. however, the script needs to detect if the new file is present, if present, then transfer. any help ?? (1 Reply)
Discussion started by: ravikodi
1 Replies

3. UNIX for Dummies Questions & Answers

Tracking the script(.sh) files triggered on daily or monthly basis from source

Hi Gurus, Is there any solution for tracking the script(.sh) files triggered on daily or monthly basis from source - Datastage (Routines) Needs to find out if this scripts are running on daily just want to know that is there anything to track Thanks in Advance (2 Replies)
Discussion started by: SeenuGuddu
2 Replies

4. Shell Programming and Scripting

Shell script to get the latest file from the file list and move

Hi, Anybody help me to write a Shell Script Get the latest file from the file list based on created and then move to the target directory. Tried with the following script: got error. A=$(ls -1dt $(find "cveit/local_ftp/reflash-parts" -type f -daystart -mtime -$dateoffset) | head... (2 Replies)
Discussion started by: saravan_an
2 Replies

5. Shell Programming and Scripting

Script to load daily average I/O stats from a .ksh file into Oracle db

Hi can anyone help me with a script to load output of the .ksh file into an Oracle database. I have attached sample output of the information that i need to load to the database (2 Replies)
Discussion started by: LucyYani
2 Replies

6. Shell Programming and Scripting

Creating directory daily basis...

Need help on this script. the month is not changing to February... #!/bin/bash for X in `seq 1 100` do DATE=`date +%Y-%m-%d "--date=${X} day ago"` Y=`date +%Y` M=`date +%m` D=`date +%d "--date=${X} day ago"` DIR=/home/LogBackup for i in `seq 1 ` do if ;then # ... (1 Reply)
Discussion started by: alelee
1 Replies

7. Shell Programming and Scripting

output file of the shell script running through crontab is deleting automatical daily.

Dear Friends, I am working on IBM AIX. I have written one script and kept in the crontab as to run daily at 11:38 AM. and the output of the script to be appended to the file generated with the month name. but my file deleting daily and the new file is creating with the output of the shell... (2 Replies)
Discussion started by: innamuri.ravi
2 Replies

8. Shell Programming and Scripting

Zip all the files within a directory on a daily basis

I need to zip all the files within a directory on a daily basis and store them as a zip file within that directory with the date in its naming convention I have file extentions .log .lst and .out's within that directory How do i do this task zip -mqj mydir/allzip$YYMMDDDD.zip mydir/*.* ... (5 Replies)
Discussion started by: ramky79
5 Replies

9. HP-UX

HPUX printers & plotters disable on a daily basis

HI all, I've been having issues with printers and plotters disabling with no reason at all...It could happen to the same workstation once a day or once a week..There is nothing consistant..I stop and restart the spooler, only then can I re-enable the disabled printer.... If anyone is... (0 Replies)
Discussion started by: Shutdown
0 Replies
Login or Register to Ask a Question