Newest file using command line

Discussion in 'Mac Programming' started by andrewheard, Jul 8, 2007.

  1. macrumors regular

    Joined:
    May 16, 2005
    Location:
    Ontario, Canada
    #1
    I'm writing a shell script and I need to determine the newest folder somehow. There will be a folder for each day named like 2007-07-08 and within each of those will be a folder with the time like 7-00 or 13-30. Anybody have any tips?

    The purpose of all of this is to have an incremental backup sort of like time machine so that I can see how a document looked an hour ago. I'm planning to have it run hourly whenever my external hard drive is connected to my MacBook and I'd like to have it keep the hourly backups for 2 days and then just keep the newest backup for days prior to that. Hopefully that makes sense.

    The backup process is very easy by making a hard linked copy of the last backup and then running rsync, which will only update the changed files.

    Incase you are wondering, yes, I am paranoid about losing my university work.

    Thanks in advance,

    Andrew
     
  2. macrumors 68000

    Joined:
    Feb 23, 2006
    #2
    just create a cronjob for rsync. Don't reinvent the wheel.
     
  3. thread starter macrumors regular

    Joined:
    May 16, 2005
    Location:
    Ontario, Canada
    #3
    I'm planning to run this using cron. I guess what I wrote only makes sense in my mind.
     
  4. macrumors 6502

    Joined:
    Jun 1, 2006
    #4
    To determine the newest directory in a particular containing directory (in this case /Users)

    find /Users -type d -exec stat -f "%Dm %N" {} \; | sort -r | head -1
     
  5. macrumors G4

    Joined:
    May 10, 2004
    #5
    For the newest folder in the current directory:

    Code:
    ls -tF | grep "/" | head -1
     
  6. thread starter macrumors regular

    Joined:
    May 16, 2005
    Location:
    Ontario, Canada
    #6
    This works perfectly. Thanks!
     
  7. macrumors G4

    Joined:
    May 10, 2004
    #7
    You woud have to add the -A option to ls if you could have directories whose names start with a dot.
     
  8. thread starter macrumors regular

    Joined:
    May 16, 2005
    Location:
    Ontario, Canada
    #8
    I won't but thanks a lot anyways.
     
  9. macrumors 6502

    Joined:
    Jun 1, 2006
    #9
    Sorry, thought you wanted a directory recursive search for the newest file directory in the path. You can shorten the other solution to this

    Code:
    ls -tF1 | head -1
     
  10. macrumors G4

    Joined:
    May 10, 2004
    #10
    That can give you a non-directory.
     
  11. thread starter macrumors regular

    Joined:
    May 16, 2005
    Location:
    Ontario, Canada
    #11
    I have the backup solution working. Just to need to set it up to delete old backups now. Here is what I wrote so far:

    Code:
    #!/bin/sh
    
    # Destermine today's date and time
    TODAY=`date +%Y-%m-%d`
    CURRENTTIME=`date +%H-%M`
    
    # The location where backups will be stored and the backup source files
    DESTINATION='/Volumes/ExternalHD/Test'
    SOURCE='/Users/andrew/Homework'
    
    # Change to destination directory and determine newest folder
    cd $DESTINATION
    NEWESTDATE=`ls -r | head -1`
    
    # Change to newest folder
    cd $NEWESTDATE
    
    # Determine newest folder and change into it
    NEWESTTIME=`ls -r | head -1`
    cd $NEWESTTIME
    
    # Make copy of latest backup
    if [ "$NEWESTDATE" = "$TODAY" ]; then
    	find . -print | cpio -dplm ../$CURRENTTIME
    else
    	mkdir $DESTINATION/$TODAY
    	find . -print | cpio -dplm $DESTINATION/$TODAY/$CURRENTTIME
    fi
    
    # Update latest backup from source files
    rsync -aEpSx --delete $SOURCE $DESTINATION/$TODAY/$CURRENTTIME
    
     
  12. macrumors 6502

    Joined:
    Jun 1, 2006
    #12
    The way the file structure has been outlined, there will be no non-directories in the top-level date named directories. Files will only be found in the directories labeled with times.
     

Share This Page