Home » Linux » Does scp create the target folder if it does not exist [closed]

Does scp create the target folder if it does not exist [closed]

Posted by: admin November 29, 2017 Leave a comment

Questions:

I am wondering if scp will create the target folder if it does not exist on the remote server. For example, would this work?

scp -r /data/install/somefolder [email protected]:/data/install/somefolder

Here the folder /data/install/somefolder doesn’t exist on ftp server, so would this command create it?

N.B. I have read about rsync but am not really sure how it works or how to use it yet.

Answers:

To achieve the task with ssh and scp (instead of rsync):

Solution

Lets break into 2 steps :

1. Create directory if missing:

ssh [email protected] 'mkdir -p /data/install/somefolder'

2. Copy to it:

scp -r /data/install/somefolder [email protected]:/data/install/somefolder

Put them together

server="[email protected]"
destiny="/data/install/somefolder"
src="/data/install/somefolder"
ssh "$server" "mkdir -p $destiny" && scp -r "$src" "$server:$destiny"

Questions:
Answers:

Short answer: no.

…but rsync does, which is why i have aliased scp to rsync -Pravdtze ssh on my box. Yes, that’s a lot of switches, which in combination produces my preferred rsync behavior. As rsync does provide a very extensive set of switches and options, i suggest you do some research on it to see what fits your needs best. Manpage is a good place to start, but there are a lot of info easily available. Here’s a decent list of examples.

Edit: Actually, in that particular case you posted, the folder will be created, as that’s the folder you’re copying. However, if you’re trying to copy it to [email protected]:somenonexistentfolder/somefolder, then it will fail.

Leave a Reply

Your email address will not be published. Required fields are marked *