Dmitry Ginzburg
Dmitry Ginzburg

Reputation: 7461

Synchronize timestamps on directories

Let's say I have two directories with the same structure and I want to set the timestamps of files, contained in the second to those of the first if and only if the content of the files is the same.

I give an answer here but if you guys have less clumsy and more efficient ways of achieving the goal, that would be perfect.

Upvotes: 0

Views: 62

Answers (2)

David C. Rankin
David C. Rankin

Reputation: 84579

An even easier way is simply:

rsync -uav /path/to/dir1/ /path/to/dir2

(removing v suppresses --verbose output)

note: the trailing '/' following dir1. It tells rsync to take the contents of dir1 instead of dir1 itself.

Upvotes: 2

Dmitry Ginzburg
Dmitry Ginzburg

Reputation: 7461

One possible solution is this script:

#!/bin/bash

OLDDIR=$(readlink -f $1)
NEWDIR=$(readlink -f $2)

cd $NEWDIR
for file in $(find .); do
    file2=$OLDDIR/$file
    if test -e "$file2" && diff >/dev/null -q "$file" "$file2" ; then
        touch -r "$file2" "$file"
    fi
done

Upvotes: 1

Related Questions