1

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

3 Answers 3

5

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
1

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

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

P.D. I don't speak english

0

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.

You must log in to answer this question.

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