Visit Our UNIX and Linux User Community


Script to move latest zip file to another directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to move latest zip file to another directory
# 1  
Old 09-01-2013
Wrench 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.Smilie
# 2  
Old 09-01-2013
Try:
Code:
cd /opt/app/cvf/temp1
set -- *.dat
file=${1%.dat}.zip
if [ -f "$file" ]; then
  mv -- "$file" ../temp2
fi

Or:
Code:
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..

Last edited by Scrutinizer; 09-01-2013 at 04:52 AM..
# 3  
Old 09-01-2013
Quote:
Originally Posted by Scrutinizer
Try:
Code:
cd /opt/app/cvf/temp1
set -- *.dat
file=${1%.dat}.zip
[ -f "$file" ] && mv -- "$file" ../temp2

Or:
Code:
cd /opt/app/cvf/temp1
for i in *.dat; do
  file=${i%.dat}.zip
  [ -f "$file" ] && mv -- "$file" ../temp2
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..!!Smilie

Last edited by Scrutinizer; 09-01-2013 at 04:55 AM.. Reason: corrected quote
# 4  
Old 09-01-2013
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..
# 5  
Old 09-01-2013
Quote:
Originally Posted by Scrutinizer
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
# 6  
Old 09-01-2013
Yes you can do that too..

Previous Thread | Next Thread
Test Your Knowledge in Computers #976
Difficulty: Medium
In April 2014, Linus Torvalds banned Kay Sievers from submitting patches to the Linux kernel for failing to deal with bugs that caused systemd to negatively interact with the kernel.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

2. Shell Programming and Scripting

Move file in to directory- script

Hi In directory /mnt/upload I have about 100 000 files (*.png) that have been created during the last six months. Now I need to move them to right folders. eg: file created on 2014-10-10 move to directory /mnt/upload/20141010 file created on 2014-11-11 move to directory /mnt/upload/20141111... (6 Replies)
Discussion started by: primo102
6 Replies

3. Shell Programming and Scripting

Help with korn shell script to get the latest file versions in a directory

I want to write a korn shell script to get the latest three versions for a file in the directory having lot of files with various versions (files with prefix as same but time stamp as suffix) and compress it and at the same time have to remove the remaining versions of the file (other than latest... (4 Replies)
Discussion started by: maheshbabu
4 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

create t a filelist with the latest file by YYYYMMDD and move to subfolder

Hi all, I am receiving files named like: ABC_20120730.csv ABC_20120801.csv ABC_20120812.csv They are residing in a folder named Incoming. I am supposed to write a script that will create a filelist which will contain only the name of the latest file beginning with ABC_, by YYYYMMDD... (3 Replies)
Discussion started by: kedrick
3 Replies

6. UNIX for Dummies Questions & Answers

Zip all files in a directory and move to another directory

Hi, need to zip all files in a directory and move to another directory after the zip.. i am using this one but didnt help me... zip -r my_proj_`date +%Y%m%d%H%MS`.zip /path/my_proj mv in_proj_`date +%Y%m%d%H%M%S`.zip /path/source/ i am trying to zip all the files in my_proj... (0 Replies)
Discussion started by: dssyadav
0 Replies

7. Shell Programming and Scripting

Help with auto-detect new files/folders then zip and move script

Hello, I need a simple script to Auto-detect new files and folders in the directory. And then I need to zip the new files and bzip2 new folders and move them out of that folder where I am detecting changes to the other folder. Remember, I need simple one. If anyone could do it fast, I may... (1 Reply)
Discussion started by: juzt1s
1 Replies

8. Shell Programming and Scripting

Move the latest or older File from one directory to another Directory

I Need help for one requirement, I want to move the latest/Older file in the folder to another file. File have the datetimestamp in postfix. Example: Source Directory : \a destination Directory : \a\b File1 : xy_MMDDYYYYHHMM.txt (xy_032120101456.txt) File2: xy_MMDDYYYYHHMM.txt... (1 Reply)
Discussion started by: pp_ayyanar
1 Replies

9. Shell Programming and Scripting

A script that will move a file to a directory with the same name and then rename that file

Hello all. I am new to this forum (and somewhat new to UNIX / LINUX - I started using ubuntu 1 year ago).:b: I have the following problem that I have not been able to figure out how to take care of and I was wondering if anyone could help me out.:confused: I have all of my music stored in... (7 Replies)
Discussion started by: marcozd
7 Replies

10. Shell Programming and Scripting

Unzipping latest zip file by name.

I have these zip files that come in with the same name, but different versions. We'll say: SQL_version2.zip SQL_version3.zip SQL_version2432.zip I was wondering if there is a single command (or even piped command, thus still making it a single command) that will let me unzip the latest... (3 Replies)
Discussion started by: mrwatkin
3 Replies

Featured Tech Videos