Rsync, also known as Remote Sync, is a popular open-source tool primarily used in Unix-based systems (Linux, MacOS) for file copying and synchronization. Rsync offers several advantages over traditional copy, such as incremental file transfer, ability to copy across remote servers, permissions preservation, copying of symbolic links, and more.
If you want to copy full directories rather than just their contents, the key lies in ending your source directory reference with a trailing slash (“/”). The absence or presence of a slash after the name of the directory in your rsync command changes the behavior of the tool.
Here’s an example of how it works:
- `rsync -av /source/directory /destination/directory`
With this command, rsync will create a directory named ‘directory’ within the destination and then copy the contents of ‘/source/directory’ into this new directory.
- `rsync -av /source/directory/ /destination/directory`
In this case, rsync will directly copy the contents of ‘directory’ into the ‘/destination/directory’ without creating a new directory.
Now, if you want to replicate the entire directory structure of the source to the destination you will need to exclude the trailing slash (“/”) at the end of the source directory.
Here is an example:
`rsync -av /source/directory /destination/`
This command copies the “directory” (not just its contents) from the source to the destination location, thus preserving the full path structure.
The “-a” flag in this command is an option that stands for “archive”. It preserves symbolic links, file permissions, user & group ownerships and timestamps during the transfer. It also allows recursive Copying.
The “-v” flag serves to provide more detailed information on the process (verbose mode), so you can understand more thoroughly what is happening. If you have very many files and you want the process to be less verbose, you don’t need to include this flag.
This information is well documented in the rsync man pages. The man pages are a good starting point for learning about the many options and nuances with the rsync command and they can be accessed by typing ‘man rsync’ in the terminal [1]. For more practical implementation, numerous online tutorials are available as well [2].
Sources:
[1] “Rsync man page”: https://linux.die.net/man/1/rsync
[2] “Linux rsync command help and examples”: https://www.computerhope.com/unix/rsync.htm