PDA

View Full Version : Newest file using command line




andrewheard
Jul 8, 2007, 03:30 PM
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



SC68Cal
Jul 8, 2007, 03:36 PM
just create a cronjob for rsync. Don't reinvent the wheel.

andrewheard
Jul 8, 2007, 03:38 PM
I'm planning to run this using cron. I guess what I wrote only makes sense in my mind.

antibact1
Jul 8, 2007, 03:47 PM
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
Jul 8, 2007, 04:06 PM
For the newest folder in the current directory:

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

andrewheard
Jul 8, 2007, 04:10 PM
For the newest folder in the current directory:

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

This works perfectly. Thanks!

cube
Jul 8, 2007, 04:28 PM
You woud have to add the -A option to ls if you could have directories whose names start with a dot.

andrewheard
Jul 8, 2007, 04:33 PM
You woud have to add the -A option to ls if you could have directories whose names start with a dot.

I won't but thanks a lot anyways.

antibact1
Jul 8, 2007, 05:32 PM
For the newest folder in the current directory:

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

ls -tF1 | head -1

cube
Jul 8, 2007, 05:52 PM
Sorry, thought you wanted a directory recursive search for the newest file directory in the path. You can shorten the other solution to this

ls -tF1 | head -1

That can give you a non-directory.

andrewheard
Jul 8, 2007, 06:44 PM
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:


#!/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
Jul 9, 2007, 12:37 AM
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.