3

Can I make rsync to move large amount of files instead of copying them when on local filesystem? I would like it to behave like improved mv command. I know about --remove-source-files option but manual does not say anything whether it actually moves files (desired) or just copies and then delete (not desired).

Edit What I want to achieve is that rsync (improved mv) moved files into directories merging existing directories -- same as rsync -r but moving not copying e.g.

$ tree d1 d2
d1
├── b
├── c
├── d
├── deep_house
│   └── dh1
└── sound
    └── sss
d2
├── a
└── deep_house
    └── dddx

3 directories, 7 files
$ rsync -r d1/ d2
$ tree d2
d2
├── a
├── b
├── c
├── d
├── deep_house
│   ├── dddx
│   └── dh1
└── sound
    └── sss

migrated from serverfault.com Apr 12 '15 at 10:16

This question came from our site for system and network administrators.

  • You say "improved mv" - what is it about mv that's deficient? Or, to put it another way, what quality of rsync do you wish to have in mv? I think if we understood what you are trying to achieve, this question would be easier to answer. – Tom Newton Apr 12 '15 at 9:39
  • @TomNewton I have added an example and explanation. – Trismegistos Apr 12 '15 at 10:07
3

Rsync copies files. That's what it does. Even if you tell it to remove source files, it still copies them first, it never moves them, even when the destination happens to be on the same filesystem.

The mv utility from GNU coreutils has an option -u to move files only if the destination is older than the source or doesn't exist yet. This is similar to rsync's -u.

If you want to move a directory tree into an existing one, you can use a recursive traversal that calls mv on every file. If you don't want to overwrite existing files:

cd d1
find . -depth -exec sh -c 'test -e "../d2/$0" || mv "$0" "../d2/$0"' {} \;

If you do want to overwrite existing files, you'll need to distinguish between existing directories and other files, and to decide what to do when the source has a regular file where the destination has a directory.

2

rsync copies files, it never moves them.

Combinations of find and mv will likely do what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.