Script to move latest zip file to another directory

Hi folks,

In my application there is a job running which create a .dat file along with it zip file also at unix box location /opt/app/cvf/temp1
so in temp1 directory I have one .dat file and its zip file also.

Now since this job runs every day so if a job runs today there will be two files one is .dat file and other is it's .zip file and let say next day job also runs so what it does is that it keep the previous day .zip file but it delete the .dat file of previous day instead and it creates the new .dat file of present day, so total will be there 3 files for that day , one previous day .zip file and one present day .zip file and one present day.dat file. and so on cycle contionus , so finally I have one .dat file only and remaning previos zip files at temp1 directory

Now my objective is to move the .zip file of the latest one to the other location at /opt/app/cvf/temp2 , please advise the script that will move the latest .zip file to the temp2 directory.
I am using bash shell in unix.:confused:

Try:

cd /opt/app/cvf/temp1
set -- *.dat
file=${1%.dat}.zip
if [ -f "$file" ]; then
  mv -- "$file" ../temp2
fi

Or:

cd /opt/app/cvf/temp1
for i in *.dat; do
  file=${i%.dat}.zip
  if [ -f "$file" ]; then
    mv -- "$file" ../temp2
  fi
done

In the latter construct, the loop will only be used once, since there can only be one .dat file..

Thanks a lot also please advise shall I write this complete script in a notepad and save it with a .sh extension and then execute it..!!:confused:

Note there are two distinct script. The first one uses positional parameters to expand *.dat , the second uses a for loop. You can chose either one.. (there was a small typo in the second one, I corrected it in the mean time)

You can try these commands on a command prompt... You can also test them by pasting them in a file and then make that file executable ( chmod +x file ) . You can execute a script by using ./ , followed by the name of the file ./file . A .sh extension is not required..

Thanks a lot , actually I have a habbit of storing the scripts in an independent file with .sh extension and then calling them like sh aa.sh (assuming the script name is aa.sh ) so please advise can I do like that also and when I invoke i will pass the source folder name and destination filder name, like suppose the script name is aa.sh so the command will to execute the script will be like..
sh aa.sh temp1 temp2

Yes you can do that too..