Can Scp Create A Directory If It Doesn't Exist?


Answer :

This is one of the many things that rsync can do.

If you're using a version of rsync released in the past several years,¹ its basic command syntax is similar to scp

$ rsync -r local-dir remote-machine:path 

That will copy local-source and its contents to $HOME/path/local-dir on the remote machine, creating whatever directories are required.³

rsync does have some restrictions here that can affect whether this will work in your particular situation. It won't create multiple levels of missing remote directories, for example; it will only create up to one missing level on the remote. You can easily get around this by preceding the rsync command with something like this:

$ ssh remote-host 'mkdir -p foo/bar/qux' 

That will create the $HOME/foo/bar/qux tree if it doesn't exist. It won't complain or do anything else bad if it does already exist.

rsync sometimes has other surprising behaviors. Basically, you're asking it to figure out what you meant to copy, and its guesses may not match your assumptions. Try it and see. If it doesn't behave as you expect and you can't see why, post more details about your local and remote directory setups, and give the command you tried.


Footnotes:

  1. Before rsync 2.6.0 (1 Jan 2004), it required the -e ssh flag to make it behave like scp because it defaulted to the obsolete RSH protocol.

  2. scp and rsync share some flags, but there is only a bit of overlap.

  3. When using SSH as the transfer protocol, rsync uses the same defaults. So, just like scp, it will assume there is a user with the same name as your local user on the remote machine by default.


If you are copying a group of files, not really. If you are copying a directory and all the contents below it, yes. Given this command:

$ scp -pr /source/directory user@host:the/target/directory 

If directory does not exist in ~/the/target on host, it will be created. If, however, ~the/target does not exist, you will likely have issues - I don't remember the exact results of this case, but be prepared for the intended scp to fail.


As far as I know, scp itself cannot do that, no. However, you could just ssh to the target machine, create the directory and then copy. Something like:

ssh user@host "mkdir -p /target/path/" &&     scp /path/to/source user@host:/target/path/ 

Note that if you are copying entire directories, the above is not needed. For example, to copy the directory ~/foo to the remote host, you could use the -r (recursive) flag:

scp -r ~/foo/ user@host:~/ 

That will create the target directory ~/foo on the remote host. However, it can't create the parent directory. scp ~/foo user@host:~/bar/foo will fail unless the target directory bar exists. In any case, the -r flag won't help create a target directory if you are copying individual files.


Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable