3

I want to compare my local directory and remote directory.

Example: Consider 2 directories as local and remote directory

|-local
|  |--aaa
|  |   |--file1
|  |   |--file2
|  |
|  |--bbb
|  |   |--file1
|  |   |--file2
|  |-file1
|
|-remote
   |--ddd
       |--file1
       |--file2

If I try running rsync

rsync -avcn local/ remote/

I am getting following output

./
file1
aaa/
aaa/file1
aaa/file2
bbb/
bbb/file1
bbb/file2

Is there a way where I can only get folder name if all the files are also missing from local

Excepted output:

file1
aaa/
bbb/

Here file1 and file2 both are present in "aaa" and "bbb", so only folder name should be visible in output.

  • 1
    Please replace your screenshots with plain text. They are easier to read, take less space, can be searched, and copy/pasted. – Sparhawk Feb 20 '18 at 22:50
  • 1
    @Sparhawk I have updated the post. Thanks for the advice. – Vineet Palan Feb 21 '18 at 21:35
  • Thanks @Vineet. However, I think you have typos now. Where did ccc come from? It's not in the first code block. Did you mean bbb? Also, the output of rsync contains ./. By your logic, doesn't that also contain everything else? – Sparhawk Feb 22 '18 at 9:03
  • @Sparhawk I am really sorry about this. I have update again. – Vineet Palan Feb 22 '18 at 19:43
  • Vineet, did my answer help at all? – Sparhawk Feb 26 '18 at 9:06
1

You can pipe your command into the following awk script.

$ rsync -avcn local/ remote/ | awk 'BEGIN {prev="./"} substr($0, 0, (length(prev))) != prev {print $0; prev = $0}'
file1
aaa/
bbb/

Explanation

  • {prev="./"}: before we start reading the input, set the variable prev to ./ (which is the first line of the output). From here, we will use this variable to store the contents of the previous line.
  • substr($0, 0, (length(prev))): extract a substring of the whole line $0, starting at the first character 0, and with length as long as the previous line (length(prev)).
  • != prev: compare the substring of the current line to the previous line. Essentially, this tests if the current line (the substring) starts with the string from the previous line. We don't use a simple regex here (anchored to the beginning of the line), because we'd have to escape it.
  • {print $0; prev = $0}: if the current line does start with the previous line, then ignore it (i.e. don't print it), if it doesn't match, then it's a new directory, so print that whole line print $0, and store the fresh line in the prev variable prev = $0.
0

Your best bet, I Reckon, is using find <dir> -maxdepth 1 -type d against the 2 directories, then compare the sorted results. Similarly to Paul T's solution but with a depth limit and considering directories only:

$ cd <local_folder>; find . -type d -maxdepth 1 | sort > ~/local-list.txt
$ cd <remote_folder>; find . -type d -maxdepth 1 | sort > ~/remote-list.txt
$ diff -u ~/remote-list.txt ~/local-list.txt

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.