tags:

views:

361

answers:

5

Let's say I have two directory structures:

/var/www/site1.prod
/var/www/site1.test

I want to use find(1) to see the files that are newer in /var/www/site1.test than their counterparts in /var/www/site1.prod.

Can I do this with find(1), and if so, how?

+2  A: 

Hi,

I think you can't do it with find alone, but you can do something like

$ cd /var/www/site1.test
$ files=`find .`
$ for $f in $files; do
     if [ $f -nt /var/www/site1.prod/$f ]; then echo "$f changed!"; fi;
  done
antti.huima
minor modifications:for f in $files; do if [ $f -nt /var/www/site1.prod/$f ]; then echo "$f changed"; fi;done
gms8994
This will break for filenames containing blanks, and break for a large number of files on certain (ba)shes.
vladr
Warning: This won't tell you if a new file was created in /var/www/site1.prod and not in /var/www/site1.test -- which may be acceptable.
Eddie
This isn't the first shell script I've seen on SO that conveniently forgets that file names may contain blanks.
bendin
I'm sure it does.
antti.huima
A: 

If you look at the -fprintf option you should be able to create two finds that output into two files that list the files name and modification time. You should then be able to just diff the two files to see what has changed.

Jackson
+7  A: 

You also could use rsync -n

rsync -av -n /var/www/site1.test /var/www/site1.prod

should do it.

Bruce ONeel
+3  A: 

Using find,

cd /var/www/site1.test
find . -type f -print | while read file ; do
  [ "$file" -nt /var/www/site1.prod/"$file" ] && echo "File '$file' changed"
done

This will work with filenames containing blanks, as well as work for a large volume of files.

To distinguish between modified and missing files, as per Eddie's comments,

cd /var/www/site1.test
find . -type f -print | while read file ; do
  [ "$file" -nt /var/www/site1.prod/"$file" ] && reason=changed
  [ \! -e /var/www/site1.prod/"$file" ] && reason=created
  [ -n "$reason" ] && echo "$file $reason"
done

Cheers, V.

vladr
Warning: Like the solution you are correcting, this won't tell you if a new file was created in /var/www/site1.prod and not in /var/www/site1.test -- which may be acceptable.
Eddie
A: 

I understand that you specifically asked for newer, but I think it's always good to know all your options.

I would use diff -ur dir1 dir2.

Maybe the timestamp changed, maybe it didn't, but that doesn't necessarily tell you if the contents are the same. Diff will tell you if the contents changed. If you really don't want to see the contents use diff -rq to just see the files that changed.

sjbotha
That's if you are using a version of diff that supports the "-q" option.
Eddie