I have local file structure:/tmp/24    /dir1        /file1        /file2    /dir2        /file3        /file4and I want to upload it to remote compute. When I usersync /tmp/24 host:/targetit creates it inside target directory on the remote host (/target/24/dir1/file1, ...). But when I use it like thisrsync /tmp/24/ host:/targetit does what I want and that is to create them like this: /target/dir1/file1. However, scp does the first thing if the target folder already exists and second when it doesn't regardless of the path ends with / or not. How can I force scp to behave like rsync in the second example?
Get `scp path/` behave like `rsync path/`
directory;scp;slash
The trailing '/' on the source directory name is a subtlety of rsync. Pay attention to it.rsyncA trailing slash on the source effectively means copy the contents of this directory, not the directory itself. Without the trailing slash, it means copy the directory.So rsync -a tmp/24/ host:/target will copy the contents of /tmp/24/ into host:/target/.But rsync -a tmp/24 host:/target will copy the directory /tmp/24/ (and its contents) into host:/target/24/.It doesn't matter if host:/target/ doesn't already exist; it will be created if necessary and the results are the same either way.(Trailing slashes on the destination don't matter.) rsync                    target exists  target does not exist  rsync -a tmp/24 target   target/24/    target/24/            rsync -a tmp/24/ target  target/       target/              scpSlashes do not matter at all, only whether the target directory exists or not. If it exists, then the source directory is copied into the target directory, otherwise the target directory is created as a clone of the source. scp                    target exists  target does not exist  scp -r tmp/24 target   target/24/    target/               scp -r tmp/24/ target  target/24/    target/              So you're right, you should just do ssh host mkdir -p /target first, and then the behavior will be the same as for rsync.But why not just use rsync? It does so much more, such as partial transfers, interrupted transfers, and compressed data.rsync -azu tmp/24 host:/targetcpAnd for completeness: On Mac, the trailing '/' gives you rsync semantics as long as target already exists. cp (Mac)              target exists  target does not exist  cp -a tmp/24 target   target/24/    target/               cp -a tmp/24/ target  target/       target/              Under Linux, slashes do not matter at all, same as scp: cp (Linux)            target exists  target does not exist  cp -a tmp/24 target   target/24/    target/               cp -a tmp/24/ target  target/24/    target/              dittoAdding onditto(1) is a Mac OS tool to clone a directory. It makes as an exact a copy as possible.Slashes do not matter at all. Whether or not the target directory already exists does not matter. ditto tmp/24 target   target/ If target already exists, previously-existing files are overwritten unconditionally. Files in target which are not in the source are left alone.