1

I have two directories containing alot of subdirectories. I want to compare the directories and find the the subdirectories which are missing in the second one and those who are newer in the first directory. In order to find a way I created to test-directories. The first one looks like this:

name@pc:~/test> ls -l dir1/
insgesamt 28
drwxrws--- 2 name 1111111 4096 12. Jan 10:53 testdir1
drwxrws--- 2 name 1111111 4096 12. Jan 10:53 testdir2
drwxrws--- 2 name 1111111 4096 12. Jan 10:53 testdir3
drwxrws--- 2 name 1111111 4096 12. Jan 10:53 testdir4
drwxrws--- 2 name 1111111 4096 12. Jan 10:53 testdir5
drwxrws--- 2 name 1111111 4096 12. Jan 10:53 testdir6
drwxrws--- 2 name 1111111 4096 12. Jan 11:35 testdir7

The second one looks like this:

name@pc:~/test> ls -l dir2/
insgesamt 20
drwxrws--- 2 name 1111111 4096 12. Jan 11:19 testdir1
drwxrws--- 2 name 1111111 4096 12. Jan 11:19 testdir2
drwxrws--- 2 name 1111111 4096 12. Jan 11:19 testdir3
drwxrws--- 2 name 1111111 4096 12. Jan 11:19 testdir4
drwxrws--- 2 name 1111111 4096 12. Jan 10:53 testdir7

As you can see testdir5 and testdir6 are missing in the second directory and the testdir7 is not up to date (its older than the one in the first directory). These 3 directories (testdir5 testdir6 and testdir7) are the desired output. The subdirectories also do not contain the same files/content in both directories as they are pre and after a conversion-process. So using the -r option in rsync is not applicable I think. The following command should give me the directory differences (missing or outdated):

name@pc:~/test> rsync --update -dvn dir1/ dir2/
building file list ... done
testdir5/
testdir6/

sent 164 bytes  received 18 bytes  364.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)

The -d option for the directories. -v For the list output. -n because I am only interessted in the differences. The --update option is not doing any difference anyway. Why am I not getting the testdir7/ here? Obviously the directorys modifaction date shows it is older. I think it has something todo with the way rsync is deciding whether something is different or not and the fact that these are only directories but there is no "use modifaction time only" option as far as I can tell. What should I do differently here?

EDIT: the results after testing some of the answers

name@pc:~/test> rsync -dnvt dir1/ dir2/
building file list ... done
./
testdir1/
testdir2/
testdir3/
testdir4/
testdir5/
testdir6/
testdir7/
testdir8/

sent 206 bytes  received 43 bytes  498.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)

name@pc:~/test> rsync -dnvt --ignore-existing dir1/ dir2/
building file list ... done
./
testdir1/
testdir2/
testdir3/
testdir4/
testdir5/
testdir6/
testdir7/
testdir8/

sent 206 bytes  received 43 bytes  498.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)

name@pc:~/test> rsync -dnvt --update dir1/ dir2/
building file list ... done
./
testdir1/
testdir2/
testdir3/
testdir4/
testdir5/
testdir6/
testdir7/
testdir8/

sent 206 bytes  received 43 bytes  498.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)

name@pc:~/test> rsync -anv --ignore-existing dir1/ dir2
sending incremental file list
./
testdir1/
testdir2/
testdir3/
testdir4/
testdir5/
testdir6/
testdir7/
testdir7/testfile1.txt
testdir8/

sent 300 bytes  received 54 bytes  708.00 bytes/sec
total size is 9  speedup is 0.03 (DRY RUN)
1
  • May not be a problem here, but beware doing tests with directories instead of files. Directory timestamps are often not updated or used for comparison. I would expect the contents of the directory to be examined for updating, regardless of the directory timestamp.
    – BowlOfRed
    Jan 12 at 19:01
1

You haven't asked rsync to traverse the directories, so it's just synchronizing the directories themselves, not the files inside the directories. If you want rsync to consider the files inside the directories., you need to pass the option -r or -a, or to list the files on the command line.

You haven't asked rsync to copy any metadata. By default, rsync only copies contents. For directories, there is no content, only the existence of the directory itself: the directory's content (the files inside it) are treated as separate objects that can be copied independently (and subject to filters). Since testdir7 already exists in the target tree, there's nothing to do about it. If you want to copy the timestamp on the directory, you need to pass the option -t or -a.

Note that copying the timestamp on a directory without copying files inside it is rarely useful. If you create (or remove, etc.) a file in a directory, that updates the modification time of the directory. On the other hand, modifying an existing file, or making changes to a directory, does not update the modification time of the directory. If you update the modification time of dir2/testdir7 and then copy files from dir1/testdir7 to dir2/testdir7, the modification time of dir2/testdir7 will be the time of the file copy. Rsync would not base its decision to copy the content of a directory on the directory's timestamp, since that does not provide any information about the state of the directory's contents.

3
  • The files inside the directories are completly different because they are for before and after a conversion-process, so I dont want to synchronize them. The Idea was to use rsync to get a list of differences in directories. Using -t would result in a list of all directories as all the timestamps are different. But the timestamps should only matter for those files which are newer in the pre-process direcntory.
    – bengt
    Jan 15 at 8:33
  • @bengt Are you looking for rsync -anv --ignore-existing? Jan 15 at 19:20
  • This command also returns all directories. Once I use -a or -t he does only see that there are differences and nothing else. Even with --ignore-existing he is not ignoring the existing and even with --update he still wants to change the directories which are newer on the receiving directory.
    – bengt
    Jan 18 at 9:06
0

You also need to specify -t:

--times, -t              preserve modification times

So this should work:

rsync -dvnt dir1/ dir2/
1
  • this results in a List of all directories because the timestamps dont match anywhere. He does not just update the older directories but all as he is synchronizing the timestamps
    – bengt
    Jan 15 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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