What are the rsync equivalent command/s to mv /a/b /c/d?

What are the rsync equivalent command/s to mv /a/b/ /c/d/, i.e, to move a directory tree from one location to another, possibly on another file system?

That is, not just copy, but delete the files from the original location. The reason I want to use rsync is that I want to be able to resume a partial move and be able to limit the bandwidth usage.

1 Answer

This command will sync all files from /a/b to /c/d, and will preserve permissions, modification dates, symlinks, and will recurse into directories, ('-a', or '--archive' does all that, it is same as using '-rlptgoD', which are separate options for all that ), -v is for verbose, and '--remove-source-files' will delete source files once they have been fully created on the destination.

If the directory d inside /c/ doesnt exist, it will be created.

rsync -av --remove-source-files /a/b /c/d

If you want to move to remote server then this will sync to remote server path, '-z' will compress file data during the transfer.

rsync -avz --remove-source-files /a/b user@hostname_or_ip:/c/d

Directories wont be removed by '--remove-source-files', so you need to run rm -rf /a/b after rsync to remove all directories inside /a/b/

In one line command you can put it all like this

rsync -av --remove-source-files /a/b /c/d; if find /a/b/ -type f | read; then echo "Not all files were synced"; else rm -rf /a/b; fi

This will run rsync, then check if there are any files left, to prevent deleting the folder if some files were not fully synced, and therefore were not deleted, and then either print that not all files were synced, or delete the folder if there are no files in it.

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like