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
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.
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:
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
I dont think rsync will work for me....
As of now, i am trying Tony's solution, hope it works but thank you for the suggestion.
#!/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'
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.