Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I write batch file and i want to compare the modification time (timstap) to two folder and copy the latest one . i don't know if this option available directory be cause i don't know .so i want to get the date and store each part in variale to compare it

Example :-

get date date 11 5 2011 11:10:57

day=11

month=5

year=2011

hour=11

min=10

secnd=57

share|improve this question

3 Answers 3

up vote 5 down vote accepted

Bash provides the -nt comparison operator which returns true if one file is newer than another file. Here's a script which illustrates this functionality

#!/bin/bash

if [[ "$1" -nt "$2" ]]
then
    echo "$1 is newer than $2 - copying $1"
    #do something
else
    if [[ "$2" -nt "$1" ]]
        then
            echo "$2 is newer than $1 - copying $2"
            #do something
        else
            echo "$1 and $2 are the same time"
            #do nothing
        fi
fi
share|improve this answer

Maybe the option -newer of "find" can do it.

find . -newer directory -exec cp {} /new/location \;

P.D. I don't speak english

share|improve this answer

To set datetime components to shell variables:

eval $(date "+day=%d month=%m year=%Y hour=%H min=%M secnd=%S")

To compare dates, Iain's answer is the way to go.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.