How to rsync a directory to a new directory with different name?

I’m doing a data transfer, the old file system relies deeply on a directory which now is on different path. This is a git directory which stores code online. I have no rights to move it or rename it.

So what I can do is rsync this directory to the same old path. But the directory name also changed.

Is there a easy way that I can rsync a directory to a target directory with a different name?

Asked By: Zen

||

I find an alternative solution now.

Using rysnc -r /origin/* /target/ can sync all the contents from origin directory to target directory.

Answered By: Zen

If you want to use rsync to recursively make the dest directory an exact copy of the src directory:

rsync -a src/ dest

The rsync man page explains how this works:

A trailing slash on the source […] avoid[s] creating an additional directory level at the destination. You can think of a trailing / on a source as meaning “copy the contents of this directory” as opposed to “copy the directory by name” […]

Answered By: Adrian

If the old and new filesystems are both accessible to your machine, then consider using a symbolic link instead of rsync. If your rsync command you’re using does not specify hosts (or if they are the same host), then this is the case. You’re trying to mirror two directories with a different name on the same host. Don’t use rsync for this, since you are duplicating the data, and you will have to run your rsync often to keep them in sync.

Using a symbolic link is a one-time fix that will mirror two directories (with different names) and requires almost no space. It’s like creating an alias directory that points to another directory.

Let’s say you had the old git in a directory called /old/path/old_git and now the code repository has moved to /new/path/new_git

You can do:

cd /old/path
rm -rf old_git     (if you still have it there, get rid of it, or move it out of the way)  
ln -s /new/path/new_git old_git

This will create a link as such:

/old/path/old_git -> /new/path/new_git

So that anything that is put into new_git will be instantly available the old way as well, without duplicating data, and without having to constantly sync them.

Answered By: jrel

Quick Start

Run:

rsync -av --exclude='path1/in/source' --exclude='path2/in/source' [source]/ [destination]

Notes

  • -avr will create a new directory named [destination].
  • source and source/ create different results:
    • source — copy the folder source into destination.
    • source/ — copy the contents of source into destination.
  • To exclude many files:
    • --exclude-from=FILEFILE is the name of a file containing other files or directories to exclude.
  • --exclude may also contain wildcards:
    • e.g. --exclude=*/.svn*

Modified from: https://stackoverflow.com/a/2194500/749232


Example

Starting folder structure:

.
├── destination
└── source
    ├── fileToCopy.rtf
    └── fileToExclude.rtf

Run:

rsync -av --exclude='fileToCopy.rtf' source/ destination

Ending folder structure:

.
├── destination
│   └── fileToExclude.rtf
└── source
    ├── fileToCopy.rtf
    └── fileToExclude.rtf
Answered By: Jack
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.