Copying latest file into a folder

Hello all, this is my first post while i am trying to understand unix.
I would basically like to know if i can do this:
Lets say i have a folderA and folderB
And i save something in folderA

Can i make a script that checks folderA latest file, then compares it with the date of latest file in folder B
If the file in folderA is newer that the last file in folderB then copy it on FolderB rename it to something.
Repeat this infinitely :slight_smile:

So even if i delete the contents of folderA i will have a backup of all files in folderB

I am not sure how to proceed or if that is possible.
Thanks in advance for any info!

i can suggest you to go through the man pages of commands like
test
find
using test command you can test for newer file and find will help you to locate the file in folders then you can use cp or mv according to your req.

thanks for your answer!
I am going to look into it and letu know more questions! :slight_smile:

$ ls -tr folderA | tail -1
Will give you the newest file.

So you could use:

LATEST=`ls -tr folderA | tail -1`
cp "folderA/${LATEST}" "folderB/${LATEST}".$$

Put the above in a script and call it from cron (if running once per minute is enough?) and $$ will be a different PID each time the script runs.

Or use a script something like, if the newest file in FolderA is going to change names each time:

PREVLATEST=""
while [ 1 ]; do
LATEST=`ls -tr folderA | tail -1`
if [ "${LATEST}" != "${PREVLATEST}" ]; then
  cp "folderA/${LATEST}" "folderB/${LATEST}"
  PREVLATEST="${LATEST}"
done

Call the script thus: nohup ./scriptname & and it would just keep running (you might want to put a sleep in their somewhere!

HTH

Tony

Tony thanks so much i will give it a try!!!!!

hello there, i tried it and it doesnt work :frowning:
I tried to code below:

PREVLATEST=""
while [ 1 ]; do
LATEST=`ls -tr /var/Folder1/ | tail -1`
if [ "${LATEST}" != "${PREVLATEST}" ]; then
  cp "/var/Folder1/${LATEST}" "/var/Folder2/${LATEST}"
  PREVLATEST="${LATEST}"
done

I save this on a file.sh then i run it at the startup as a daemon.

Seems like nothing is happening.
any directions?

Put a set -x at the top of the script and run it in a terminal window to see what it is doing, a sleep of some period just before the done might be advisable (say 3 seconds?)

If the set -x spits out too much on the screen then put:

echo PREVLATEST = ${PREVLATEST} LATEST = ${LATEST}

before the if line and put:

echo cp "/var/Folder1/${LATEST}" "/var/Folder2/${LATEST}"

before the actual copy line instead of using set -x.

To run it as a daemon from within a terminal window you can do:

# nohup ./script.sh &

Tony, once again thank you for the fast and detailed reply, i will be trying it in a while and let you know!

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.

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 :slight_smile:
I dont think rsync will work for me....

As of now, i am trying Tony's solution, hope it works :slight_smile: but thank you for the suggestion.

so i retried: here is my script

#!/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?

Your if block is not closed by a "fi".

Mea culpa!

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

Also the line:

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

may be simplified to:

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:"

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:

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:

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.

I think even cp has a -u option which copies only latest files from source to dest.

hey guys thanks again for your replies..

i still have a problem with the script... so i have a few versions of it that dont work, here they are:

#!/bin/bash


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

in this one it complains "sysntax error near unexpected token 'done'"

#!/bin/bash


SOURCEFOLDER=/var/mobile/Media/DCIM/100APPLE
TARGETFOLDER=/var/mobile/Media/DCIM_2
while [ 1 ]; do
  find ${SOURCEFOLDER} | cpio -pdmv ${TARGETFOLDER}
  sleep 3
done

in this one it complaints that command cpio "command not found"

So here is another code that works if i run it without complaints...

#!/bin/bash


if [ -e /var/mobile/Media/DCIM_2 ]
then
	mv /var/mobile/Media/DCIM /var/mobile/Media/DCIM_temp
	mv /var/mobile/Media/DCIM_2 /var/mobile/Media/DCIM
	mv /var/mobile/Media/DCIM_temp /var/mobile/Media/DCIM_2
else
	mv /var/mobile/Media/DCIM /var/mobile/Media/DCIM_2
	mkdir /var/mobile/Media/DCIM
	chown mobile:mobile /var/mobile/Media/DCIM
fi

The code above just check what is the current directory and sets the other one as a default....

so taking as an example the one above, i am trying to run something as simple as :

#!/bin/bash

cp /var/mobile/Media/DCIM/IMG_0001.JPG /var/mobile/Media/DCIM_2/hello.jpg

and it complains that the directory doesnt exist.... ???? makes no sense to me

Any ideas??

On my machine cpio(1) is at /bin/cpio.

If not then try running as root:

# updatedb; locate cpio | grep bin

If you do not have locate the run:

# find /usr /bin /sbin /usr/local/bin /opt -type f -name cpio

One of the above will tell you where cpio is on your system.