LinuxQuestions.org
Visit Jeremy's Blog.
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, 04:40 PM   #1
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 607

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
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, 04:56 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,316
Blog Entries: 11

Rep: Reputation: 3286Reputation: 3286Reputation: 3286Reputation: 3286Reputation: 3286Reputation: 3286Reputation: 3286Reputation: 3286Reputation: 3286Reputation: 3286Reputation: 3286
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, 05:34 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137
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, 08: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, 10:58 PM   #5
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 607

Original Poster
Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
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, 11: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, 11:37 PM   #7
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 607

Original Poster
Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
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 11:39 PM.
 
Old 02-05-2017, 12:50 AM   #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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 05:13 PM
rsync syntax for local copy hoover93 Linux - Software 3 12-21-2012 03:39 AM
Rsync syntax question alexpacio Linux - Software 5 07-23-2007 07:27 PM
help with rsync exclude syntax please JacekZ Linux - General 2 07-08-2007 02:23 PM
rsync Syntax Question for Connecting to Remote Windows Machine Linux31 Linux - Networking 3 10-09-2005 03:55 PM

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

All times are GMT -5. The time now is 10:45 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration