Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
02-03-2017, 04:40 PM
|
#1
|
Member
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634
|
rsync syntax
Hello
This has always confused me and has made it difficult for me to predict rsync behavior.
I am routinely startled to discover something like this (with the subdirectory containing a full copy of the one above it, using a lot of bandwidth and space to transmit):
Code:
/foo/bar/DirectoryA/DirectoryA
What is the difference between these syntaxes?
If I plan on simply updating a directory, which one should I use?
Code:
rsync -a /foo/bar/directoryA hostname:/foo/bar/directoryA
rsync -a /foo/bar/directoryA/ hostname:/foo/bar/directoryA
rsync -a /foo/bar/directoryA hostname:/foo/bar/directoryA/
rsync -a /foo/bar/directoryA/ hostname:/foo/bar/directoryA/
|
|
|
02-03-2017, 04:56 PM
|
#2
|
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,315
|
I have wrestled with that from time to time myself.
But the man page is clear enough, if difficult to remember:
Code:
A trailing slash on the source changes this behavior to avoid creating an additional directory level at
the destination. You can think of a trailing / on a source as meaning "copy the contents of this direc‐
tory" as opposed to "copy the directory by name", but in both cases the attributes of the containing
directory are transferred to the containing directory on the destination. In other words, each of the
following commands copies the files in the same way, including their setting of the attributes of
/dest/foo:
rsync -av /src/foo /dest
rsync -av /src/foo/ /dest/foo
So the rule I have adopted which seems to work ( for me) is always use trailing '/' on source, never trailing '/' on dest, and it always does what I want.
|
|
1 members found this post helpful.
|
02-03-2017, 05:34 PM
|
#3
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
To expand on what was written above, the trailing slash on the destination does not matter. Whether it's there or not, rsync will copy the data into said directory (similar to having a trailing slash on the source).
|
|
1 members found this post helpful.
|
02-03-2017, 08:35 PM
|
#4
|
Member
Registered: Jul 2013
Posts: 749
Rep: 
|
I ususally use something like
Code:
rsync -a /foo/bar/directory /foo/bar/
|
|
|
02-03-2017, 10:58 PM
|
#5
|
Member
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634
Original Poster
|
Thanks, that did help clear it up.
If you want to sync directories, include the trailing slash on the source directory.
Quote:
Originally Posted by Doug G
I ususally use something like
Code:
rsync -a /foo/bar/directory /foo/bar/
|
I do that as well. However, it requires extra thinking to make sure it works - less you mess up and transmit the entire directory again.
I'm pretty sure most of my mess ups have been: rsync -a /foo/bar/directoryA hostname:/foo/bar/directoryA as that makes the most sense to me
So to sync /foo/bar/directoryA on local and /foo/bar/directoryA on remote, run
Code:
rsync -a /foo/bar/directoryA/ hostname:/foo/bar/directoryA

|
|
|
02-03-2017, 11:08 PM
|
#6
|
Member
Registered: Jul 2013
Posts: 749
Rep: 
|
Quote:
If you want to sync directories, include the trailing slash on the source directory.
|
I do just the opposite. My example above will sync /foo/bar/directory with target /foo/bar/directory.
It all has to do with how rsync matches filename patterns. I kind of saw the light (for me) when I was trying to use some exclude and include options, which never seemed to work as I expected.
|
|
|
02-03-2017, 11:37 PM
|
#7
|
Member
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634
Original Poster
|
Quote:
Originally Posted by Doug G
I do just the opposite. My example above will sync /foo/bar/directory with target /foo/bar/directory.
It all has to do with how rsync matches filename patterns. I kind of saw the light (for me) when I was trying to use some exclude and include options, which never seemed to work as I expected.
|
I wouldn't call it syncing, even though it has the same result in your case.
If I understand it correctly, it seems to have the behavior of:
"Copy /foo/bar to remote:/foo, if it contains a directory called bar, sync it. Otherwise copy the whole thing over."
However, what if the end directory is different?
Eg.
How do you sync local dir /foo/bar/directoryA and remote dir /foo/bar/directoryB?
rsync -a /foo/bar/directoryA/ remote:/foo/bar/directoryB will have the expected result, directoryA and directoryB contain the same content.
But if you do rsync -a /foo/bar/directoryA remote:/foo/bar/directoryB DirectoryB will contain DirectoryB/DirectoryA!
And by doing rs ync -a /foo/bar/directoryA remote:/foo/bar, you end up with /foo/bar/directoryA. Something you may not want or expect
I'm glad I created this thread, it is clearing up the behavior of rsync greatly for me!
Last edited by Sefyir; 02-03-2017 at 11:39 PM.
|
|
|
02-05-2017, 12:50 AM
|
#8
|
Member
Registered: Jul 2013
Posts: 749
Rep: 
|
Quote:
I'm glad I created this thread, it is clearing up the behavior of rsync greatly for me!
|
Me too. I always feel like the creators of rsync made a superior product but almost intentionally made it difficult to do some pretty simple tasks. Once you spend all the hours of research, rsync performs excellently. Just my $0.00000000002
|
|
|
All times are GMT -5. The time now is 02:43 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|