Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

andrewheard

macrumors regular
Original poster
May 16, 2005
166
0
Ontario, Canada
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
 

antibact1

macrumors 6502
Jun 1, 2006
334
0
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
 

cube

Suspended
May 10, 2004
17,011
4,972
For the newest folder in the current directory:

Code:
ls -tF | grep "/" | head -1
 

cube

Suspended
May 10, 2004
17,011
4,972
You woud have to add the -A option to ls if you could have directories whose names start with a dot.
 

antibact1

macrumors 6502
Jun 1, 2006
334
0
For the newest folder in the current directory:

Code:
ls -tF | grep "/" | head -1

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
 

cube

Suspended
May 10, 2004
17,011
4,972
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

That can give you a non-directory.
 

andrewheard

macrumors regular
Original poster
May 16, 2005
166
0
Ontario, Canada
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
 

antibact1

macrumors 6502
Jun 1, 2006
334
0
That can give you a non-directory.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.