0

First of all I give my directory structure in detail below.

src
   1
     1_1.txt
        test_line1
     1_2.txt
        test_line1
   2
     2_1.txt
        test_line1
     2_2.txt
        test_line1

I synchronised this src directory with dst directory. So now dst directory is same as src directory using following rsync command.

rsync -av src/* dst/

Well. If I add a line any of the file existing in src directory, the same command copies the changes in that dst directory.

Now I adding extra text line in 1_2.txt at dst directory. In this case, src and dst directory are not same.

If I run the above rsync command I thought it will remove that extra line from that file at dst directory. But It didn't happen.

  1. What should I do to keep files are same as src directory with dst directory?

  2. When a new directory/file added in dst directory manually, It should prompt to remove that directory/file or not when I run rsync command. How can I do that?

3

I can not reproduce the same behaviour that you are seeing:

$ tree
.
|-- d
|   `-- file
`-- s
    `-- file

2 directories, 2 files
$ cat s/file
hello
$ rsync -av s/ d
sending incremental file list

sent 63 bytes  received 12 bytes  150.00 bytes/sec
total size is 6  speedup is 0.08

No data was transferred, the two files were already the same.

$ echo world >>d/file
$ cat d/file
hello
world
$ rsync -av s/ d
sending incremental file list
file

sent 116 bytes  received 35 bytes  302.00 bytes/sec
total size is 6  speedup is 0.04
$ cat d/file
hello

This shows that lines added to files in the destination directory will be removed by re-running rsync -a.


To delete files in the destination that are not available in the source directory, use --delete:

$ tree
.
|-- d
|   |-- extra_file
|   `-- file
`-- s
    `-- file

2 directories, 3 files
$ rsync -av --delete s/ d
sending incremental file list
deleting extra_file
./

sent 66 bytes  received 29 bytes  190.00 bytes/sec
total size is 6  speedup is 0.06
$ tree
.
|-- d
|   `-- file
`-- s
    `-- file

2 directories, 2 files

The rsync utility does not natively support interactive prompting for confirmation before deleting files on the destination. You may however run with --dry-run first to see what would happen if you ran the copy for real.


Also note that if you use rsync -av s/* d rather than rsync -av s/ d, then you will not copy any hidden file or directory in the source directory. This is because the * glob pattern by default does not expand to hidden filenames. Hidden files in the subdirectories of the source directory would still be copied though.

  • Thank you. If rsync can delete a directory/file in a destination directory, Could It do prompt a warning message saying 'Do you really want to delete this Folder or File?' before deleting that directory/file? – Smith Dwayne Sep 20 '18 at 8:56
  • 1
    @SmithDwayne I just added a bit of text about this. No, rsync does not support this. You can do a dry-run first though, to see what would happen. To do anything more than that would require wrapping rsync in some sort of script. This would be a too big project for me to undertake (and get right) at the moment. – Kusalananda Sep 20 '18 at 8:58

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.