LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 02-03-2017, 03:40 PM   #1
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
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/
 
Old 02-03-2017, 03:56 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
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.
Old 02-03-2017, 04:34 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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.
Old 02-03-2017, 07:35 PM   #4
Doug G
Member
 
Registered: Jul 2013
Posts: 749

Rep: Reputation: Disabled
I ususally use something like
Code:
rsync -a /foo/bar/directory /foo/bar/
 
Old 02-03-2017, 09:58 PM   #5
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Original Poster
Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
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 View Post
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
 
Old 02-03-2017, 10:08 PM   #6
Doug G
Member
 
Registered: Jul 2013
Posts: 749

Rep: Reputation: Disabled
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.
 
Old 02-03-2017, 10:37 PM   #7
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Original Poster
Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Quote:
Originally Posted by Doug G View Post
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 rsync -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 10:39 PM.
 
Old 02-04-2017, 11:50 PM   #8
Doug G
Member
 
Registered: Jul 2013
Posts: 749

Rep: Reputation: Disabled
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Rsync With SSH Syntax? NotAComputerGuy Linux - Newbie 2 06-13-2013 04:13 PM
rsync syntax for local copy hoover93 Linux - Software 3 12-21-2012 02:39 AM
Rsync syntax question alexpacio Linux - Software 5 07-23-2007 06:27 PM
help with rsync exclude syntax please JacekZ Linux - General 2 07-08-2007 01:23 PM
rsync Syntax Question for Connecting to Remote Windows Machine Linux31 Linux - Networking 3 10-09-2005 02:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration