0

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

1 Answer 1

1

You could use rsync instead of cp:

rsync -R "${CURRENTDIRECTORY}"/"${NEWESTFILE}" "${DEST}"

To limit the output path to be relative to MYDIR (for example test1/zip12.zip) you will have to enter the directory before find loop: cd $MYDIR and later find ..

If you keep old files in backup directory you could even replace the whole script using rsync --ignore-existing. It would only copy new files.

Read more in man rsync.

1
  • Cheers, I will take a look!
    – smj2393
    Apr 10, 2015 at 14:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .