I am now sure how to word this, but:
I am copying all the newest .zip files each recursive sub directory inside this original folder:
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Original"
For example
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Original/test1/zip11.zip"
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Original/test1/zip12.zip"
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Original/test2/zip21.zip"
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Original/test2/test3/zip31.zip"
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Original/test2/test3/zip32.zip"
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Original/test2/test3/zip33.zip"
This would copy zip12.zip, zip21.zip, zip33.zip (these are the newest in each directory) to the backup folder.
I have this working so all of these copy to this directory:
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Backup"
So it looks like:
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Backup/zip12.zip"
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Backup/zip21.zip"
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Backup/zip33.zip"
I want it to use the same tree structure (from Original onwards) so it looks like:
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Backup/test1/zip12.zip"
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Backup/test2/zip21.zip"
"/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Backup/test2/test3/zip33.zip"
Can someone point me in the right direction?
Here is my code:
#THE DIRECTORY THE ORIGINAL FILE IS STORED IN
MYDIR="/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Original"
#THE DIRECTORY THE BACKUP FILE WILL BE STORED IN
DEST="/Users/Stu/Documents/Hoffi Work/FTP Backup Shell Script/Backup1/"
#FIND ALL DIRECTORIES AND NAME TEMP DIRECTORY AS CURRENT DIRECTORY
find "${MYDIR}" -type d | while read CURRENTDIRECTORY;
do
#FINDS THE NEWEST ZIP FILE IN T HE DIRECTORY
NEWESTFILE=`ls -dtr1 "${CURRENTDIRECTORY}"/*.zip | tail -n 1`
echo "NF = $NEWESTFILE"
#CHECK IF FILE IS PRESENT
if [ -z "${NEWESTFILE}" ] ; then
echo "No file to copy"
#CHECK IF FILE IS A DIRECTORY
elif [ -d "${NEWESTFILE}" ] ; then
echo "Newest file is a directory"
#COPY NEWEST FILE TO DEST
else
echo "Copying ${NEWESTFILE} --> ${DEST}"
cp -p "${NEWESTFILE}" "${DEST}"
fi
done