Copying latest file into a folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying latest file into a folder
# 8  
Old 03-26-2009
Tony, once again thank you for the fast and detailed reply, i will be trying it in a while and let you know!
# 9  
Old 03-26-2009
If you are on a linux system and all you care is to do a one way sync from folderA to folderB, why don't you check out existing utilities like rsync or unison? I've used both and they are quite stable utilities. If you are on Unix, then you can try to look for rsync ported for your platform.
# 10  
Old 03-27-2009
i am on iphone... i am quite confused with all the environments, i try to read of the internet but things dont make sense sometime Smilie
I dont think rsync will work for me....

As of now, i am trying Tony's solution, hope it works Smilie but thank you for the suggestion.
# 11  
Old 03-27-2009
so i retried: here is my script

Code:
#!/bin/bash


PREVLATEST=""
while [ 1 ]; do
LATEST=`ls -tr /var/mobile/Media/DCIM/100APPLE | tail -1`
if [ "${LATEST}" != "${PREVLATEST}" ]; then
  cp `ls -tr "/var/mobile/Media/DCIM/100APPLE/${LATEST}" | tail -1` "/var/mobile/Media/DCIM_2/${LATEST}"
  PREVLATEST="${LATEST}"
done

i get:


localhost:/private/var/stash/libexec.C9Fmd1/cydia root# sh TAT.sh
-sh: localhost:/private/var/stash/libexec.C9Fmd1/cydia: No such file or directory
localhost:/private/var/stash/libexec.C9Fmd1/cydia root# TAT.sh: line 10: syntax error near unexpected token `done'
> TAT.sh: line 10: `done'

any suggestions?
# 12  
Old 03-27-2009
Your if block is not closed by a "fi".
# 13  
Old 03-27-2009
Quote:
Originally Posted by rikxik
Your if block is not closed by a "fi".
Mea culpa!

Put a fi in a line before the done, sorry!

Also the line:
Code:
cp `ls -tr "/var/mobile/Media/DCIM/100APPLE/${LATEST}" | tail -1` "/var/mobile/Media/DCIM_2/${LATEST}"

may be simplified to:
Code:
cp "/var/mobile/Media/DCIM/100APPLE/${LATEST}" "/var/mobile/Media/DCIM_2/${LATEST}"

Also why are you trying to use an NFS address in a script?:

localhost:/private/var/stash/libexec.C9Fmd1/cydia

Drop the "localhost:"

Last edited by TonyFullerMalv; 03-27-2009 at 03:17 PM.. Reason: Original cp line overly complicated.
# 14  
Old 03-27-2009
Carrying on rikxik's suggestion if you simply want to copy only new or changed files from foldera to folderb then the following will do that:
Code:
find foldera | cpio -pdmv folderb

The cpio(1) command will not copy files into folderb that already exist AND are the same as the in foldera, if any files are new or changed in foldera then cpio will copy them.

So for the folders you mentioned before:
Code:
SOURCEFOLDER=/var/mobile/Media/DCIM/100APPLE
TARGETFOLDER=/var/mobile/Media/DCIM_2
while [ 1 ]; do
  find ${SOURCEFOLDER} | cpio -pdmv ${TARGETFOLDER}
  sleep 3
done

Drop the v parameter crom cpio once you are happy it is doing what you want it to.

Last edited by TonyFullerMalv; 03-27-2009 at 03:18 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Retrieve the Latest file in a folder using SFTP

HI Guys, Can anyone help me to retrieve the latest file from below example using SFTP I have below list of files in a folder v403t.lstprgms.sortin1 val027.d099.fwest.oct2711 xcelrptd.d1400sqp.dec1713.t040459.@02del xcelrptd.d1400sqp.dec1713.t073308.@02del... (3 Replies)
Discussion started by: heye18
3 Replies

2. UNIX for Dummies Questions & Answers

Copying a file based on a incremented folder name

I need to routinely cp a file out of the highest incremented folder named PROD_2_6.1_xxx where xxx (3 numerical values) is incremented only once a night starting at 100 and ending 999 over a period of time. I am not worried about copying a subset of a complete file while it is being created by some... (5 Replies)
Discussion started by: sf_cb
5 Replies

3. Shell Programming and Scripting

Perl : copying only the latest file to other directory

In linux.. In a directory there are 3 files which I want to copy only the latest file (ls -ltr myfiles*.txt|tail -1) to other directory in perl? Could anyone please help me with the code? Regards, J (1 Reply)
Discussion started by: scriptscript
1 Replies

4. UNIX and Linux Applications

Need to copy the latest file from Unix server to Shared folder

Hi All, One job in unix server will generate .csv files daily. I need to copy the latest of these .csv file from the unix server to the shared drive/folder in windows through unix script. My shared folder will look something like W:\some folder(for example). Could any one of you please help... (3 Replies)
Discussion started by: jaya@123
3 Replies

5. UNIX for Dummies Questions & Answers

Copy the latest (last file) in given folder

#!/bin/bash for i in {1..1536..1} do #find /home/test/Desktop/up111/workplace/Malware/$i/logs for a in /home/test/Desktop/up111/workplace/Malware/$i/logs/* do #max=a for b in /home/test/Desktop/up111/workplace/Malware/$i/logs/* do ... (4 Replies)
Discussion started by: upvan111
4 Replies

6. Shell Programming and Scripting

script for Finding files in a folder and copying to another folder

Hi all, I have a folder '/samplefolder' in which i have some files like data0.txt, data1.txt and data2.txt. I have to search the folder for existence of the file data0.txt first and if found have to copy it to some other file; next i have to search the folder for existence of file... (5 Replies)
Discussion started by: satish2712
5 Replies

7. Shell Programming and Scripting

copying the latest file

Hi, I have a problem. I have some text files in a folder. The names can be like: emp_20080307053015.dat emp_20080306053015.dat emp_20080305053015.dat emp_20080304053015.dat The date format appended is like yyyymmdd and timestamp. What i need is i have to copy the latest file every... (12 Replies)
Discussion started by: Aswarth
12 Replies

8. UNIX for Dummies Questions & Answers

Copy the latest file from a folder

Hi, I have a problem. I have some text files in a folder. The names can be like: emp_20080307053015.dat emp_20080306053015.dat emp_20080305053015.dat emp_20080304053015.dat The date format appended is like yyyymmdd and timestamp. What i need is i have to copy the latest file every... (3 Replies)
Discussion started by: Aswarth
3 Replies

9. UNIX for Dummies Questions & Answers

Copying files with the latest date

Hi All, I have a situation where I need to copy the files having the latest date. For example I have a file by name bas100e1_jun05. I need to copy it to bas100e1. But when a file by name bas100e1_jul05 is put in the same directory the script should copy the file having the latest month which... (34 Replies)
Discussion started by: shashi_kiran_v
34 Replies
Login or Register to Ask a Question